WIP.
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing

This commit is contained in:
Víðir Valberg Guðmundsson 2024-01-14 12:49:33 +01:00
parent 922fd0254f
commit 69cfc38771
8 changed files with 140 additions and 37 deletions

View file

@ -51,7 +51,7 @@
</li> </li>
<li> <li>
<a href="/services" class="{% active_path "services" "current" %}"> <a href="{% url "services:list" %}" class="{% active_path "services:list" "current" %}">
Services Services
</a> </a>
</li> </li>

View file

@ -1,7 +1,5 @@
from django_view_decorator import view from django_view_decorator import view
from membership.models import ServiceAccess
from services.registry import ServiceRegistry
from utils.view_utils import render from utils.view_utils import render
@ -12,36 +10,3 @@ from utils.view_utils import render
) )
def index(request): def index(request):
return render(request, "index.html") return render(request, "index.html")
@view(
paths="services/",
name="services",
login_required=True,
)
def services_overview(request):
active_services = [
access.service_implementation
for access in ServiceAccess.objects.filter(
user=request.user,
)
]
active_service_classes = [service.__class__ for service in active_services]
services = [
service
for _, service in ServiceRegistry.get_items()
if service not in active_service_classes
]
context = {
"non_active_services": services,
"active_services": active_services,
}
return render(
request=request,
template_name="services_overview.html",
context=context,
)

View file

@ -1,3 +1,4 @@
from django import forms
from django_registries.registry import Interface from django_registries.registry import Interface
from django_registries.registry import Registry from django_registries.registry import Registry
@ -23,3 +24,17 @@ class ServiceInterface(Interface):
# - maybe a list of tuples with the field name and the type of the field # - maybe a list of tuples with the field name and the type of the field
# this could be used to generate a form for the service, and also to validate # this could be used to generate a form for the service, and also to validate
# the data saved in a JSONField on the ServiceAccess model # the data saved in a JSONField on the ServiceAccess model
subscribe_fields: list[tuple[str, forms.Field]] = []
def get_form(self) -> type:
"""Get the form for the service"""
print(self.subscribe_fields)
return type(
"ServiceForm",
(forms.Form,),
{
field_name: field_type
for field_name, field_type in self.subscribe_fields
},
)()

View file

@ -1,3 +1,5 @@
from django import forms
from .registry import ServiceInterface from .registry import ServiceInterface
@ -14,6 +16,10 @@ class MatrixService(ServiceInterface):
url = "https://matrix.data.coop" url = "https://matrix.data.coop"
description = "Matrix service for data.coop" description = "Matrix service for data.coop"
subscribe_fields = [
("username", forms.CharField()),
]
class MastodonService(ServiceInterface): class MastodonService(ServiceInterface):
slug = "mastodon" slug = "mastodon"

View file

@ -0,0 +1,9 @@
{% extends "base.html" %}
{% block content %}
<div class="content-view">
<h2>{{ service.name }}</h2>
</div>
{% endblock %}

View file

@ -0,0 +1,18 @@
{% extends "base.html" %}
{% block content %}
<div class="content-view">
<h2>Subscribe to {{ service.name }}</h2>
<form>
{{ form }}
<button type="submit">
Subscribe
</button>
</form>
</div>
{% endblock %}

View file

@ -28,11 +28,16 @@
<div class="description"> <div class="description">
<h3>{{ service.name }}</h3> <h3>{{ service.name }}</h3>
<p>{{ service.description }}</p> <p>{{ service.description }}</p>
<a href="{% url "services:detail" service_slug=service.slug %}">
Read more
</a>
|
<a href="{{ service.url }}" target="_blank"> <a href="{{ service.url }}" target="_blank">
Visit Visit
</a> </a>
</div> </div>
<a>Subscribe</a> <a href="{% url "services:subscribe" service_slug=service.slug %}">Subscribe</a>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>

View file

@ -1 +1,86 @@
# Create your views here. # Create your views here.
from django_view_decorator import namespaced_decorator_factory
from membership.models import ServiceAccess
from services.registry import ServiceRegistry
from utils.view_utils import render
services_view = namespaced_decorator_factory(
namespace="services",
base_path="services",
)
@services_view(
paths="",
name="list",
login_required=True,
)
def services_overview(request):
active_services = [
access.service_implementation
for access in ServiceAccess.objects.filter(
user=request.user,
)
]
active_service_classes = [service.__class__ for service in active_services]
services = [
service
for _, service in ServiceRegistry.get_items()
if service not in active_service_classes
]
context = {
"non_active_services": services,
"active_services": active_services,
}
return render(
request=request,
template_name="services/services_overview.html",
context=context,
)
@services_view(
paths="<str:service_slug>/",
name="detail",
login_required=True,
)
def service_detail(request, service_slug):
service = ServiceRegistry.get(slug=service_slug)
context = {
"service": service,
}
return render(
request=request,
template_name="services/service_detail.html",
context=context,
)
@services_view(
paths="<str:service_slug>/subscribe/",
name="subscribe",
login_required=True,
)
def service_subscribe(request, service_slug):
service = ServiceRegistry.get(slug=service_slug)
# TODO: add a form to subscribe to the service
context = {
"service": service,
"base_path": "services:list",
"form": service.get_form(),
}
return render(
request=request,
template_name="services/service_subscribe.html",
context=context,
)