Skip to content

Commit

Permalink
🚧 [#1776] added task components
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Oct 6, 2023
1 parent de1a533 commit 9942955
Show file tree
Hide file tree
Showing 11 changed files with 411 additions and 25 deletions.
28 changes: 27 additions & 1 deletion src/open_inwoner/cms/objects/cms_plugins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
from django.utils.translation import ugettext_lazy as _

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool

from .models import ObjectsList
from .models import ComponentChoices, ObjectsList


@plugin_pool.register_plugin
Expand All @@ -12,3 +13,28 @@ class ObjectsListPlugin(CMSPluginBase):
name = _("Object List Plugin")
render_template = "cms/objects/objects_list.html"
cache = False

def _get_render_template(self, context, instance, placeholder):
default_template = super()._get_render_template(context, instance, placeholder)
if component := instance.component:
match component:
case ComponentChoices.link:
return "cms/objects/objects_list.html"
case ComponentChoices.map:
return "cms/objects/objects_map.html"

return default_template

def render(self, context, instance, placeholder):
request = context["request"]
context["instance"] = instance
context["objects"] = []

if request.user.is_authenticated and (bsn := request.user.bsn):
objects = instance.get_objects_by_bsn(bsn)
match instance.component:
case ComponentChoices.link:
context["objects"] = instance.convert_objects_to_actiondata(objects)
case ComponentChoices.map:
context["objects"] = instance.convert_objects_to_geodata(objects)
return context
80 changes: 80 additions & 0 deletions src/open_inwoner/cms/objects/migrations/0002_auto_20231006_1047.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Generated by Django 3.2.20 on 2023-10-06 08:47

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("objects", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="objectslist",
name="bsn_path",
field=models.CharField(
blank=True,
help_text="The python path to the bsn value",
max_length=250,
verbose_name="BSN Path",
),
),
migrations.AddField(
model_name="objectslist",
name="component",
field=models.CharField(
choices=[("link", "Link"), ("map", "Map")],
default="link",
max_length=30,
verbose_name="Component",
),
),
migrations.AddField(
model_name="objectslist",
name="date_order",
field=models.CharField(
choices=[("+", "Ascend"), ("-", "Descent")],
default="-",
max_length=1,
verbose_name="Date Order",
),
),
migrations.AddField(
model_name="objectslist",
name="object_date",
field=models.CharField(
default="start_at", max_length=250, verbose_name="Object Date"
),
),
migrations.AddField(
model_name="objectslist",
name="object_link",
field=models.CharField(
default="data.formulier.value",
max_length=250,
verbose_name="Object Link",
),
),
migrations.AddField(
model_name="objectslist",
name="object_title",
field=models.CharField(
default="data.title", max_length=250, verbose_name="Object Title"
),
),
migrations.AddField(
model_name="objectslist",
name="status",
field=models.CharField(
blank=True,
choices=[
("open", "Open"),
("ingediend", "Submitted"),
("verwerkt", "Processed"),
("gesloten", "Closed"),
],
max_length=250,
verbose_name="Status",
),
),
]
114 changes: 112 additions & 2 deletions src/open_inwoner/cms/objects/models.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,130 @@
import os

import json
from glom import glom
from glom.core import PathAccessError, PathAssignError
from django.db import models
from django.utils.translation import ugettext_lazy as _

from djchoices import ChoiceItem, DjangoChoices

from cms.models import CMSPlugin
from objectsapiclient.models import Configuration, ObjectTypeField


class StatusChoices(DjangoChoices):
open = ChoiceItem("open", _("Open"))
submitted = ChoiceItem("ingediend", _("Submitted"))
processed = ChoiceItem("verwerkt", _("Processed"))
closed = ChoiceItem("gesloten", _("Closed"))


class DateOrderChoices(DjangoChoices):
ascend = ChoiceItem("+", _("Ascend"))
descent = ChoiceItem("-", _("Descent"))


class ComponentChoices(DjangoChoices):
link = ChoiceItem("link", _("Link"))
map = ChoiceItem("map", _("Map"))


class ObjectsList(CMSPlugin):
title = models.CharField(_("Title"), max_length=250)
object_type = ObjectTypeField() # Stores the UUID of the selected object_type
status = models.CharField(
_("Status"),
max_length=250,
choices=StatusChoices.choices,
blank=True,
)
component = models.CharField(
_("Component"),
max_length=30,
choices=ComponentChoices.choices,
default=ComponentChoices.link,
)
date_order = models.CharField(
_("Date Order"),
max_length=1,
choices=DateOrderChoices.choices,
default=DateOrderChoices.descent,
)
bsn_path = models.CharField(
_("BSN Path"),
max_length=250,
help_text=_("The python path to the bsn value"),
blank=True,
)
object_title = models.CharField(
_("Object Title"), max_length=250, default="data.title"
)
object_date = models.CharField(_("Object Date"), max_length=250, default="start_at")
object_link = models.CharField(
_("Object Link"), max_length=250, default="data.formulier.value"
)

def _return_self(self):
return f"{self.title}, {self.object_type}, {self.bsn_path}"

def get_objects(self):
return Configuration.get_solo().client.get_objects(
object_type_uuid=self.object_type
)

def get_objects_by_bsn(self, bsn):
data_attrs = []

if self.object_type and self.bsn_path:
data_attrs.append(self.bsn_path + "__exact__" + str(bsn))
if self.status:
data_attrs.append("status__exact__" + self.status)

return Configuration.get_solo().client.get_objects_by_bsn(
object_type_uuid=self.object_type,
ordering=self.date_order + "record__startAt",
data_attrs=",".join(data_attrs),
)

def convert_objects_to_actiondata(self, objects):
object_list = []
for object in objects:
try:
object_list.append(
{
"title": glom(object.record, self.object_title),
"date": glom(object.record, self.object_date),
"link": glom(object.record, self.object_link),
}
)
except (PathAccessError, PathAssignError):
pass

return object_list

def convert_objects_to_geodata(self, objects):
features = []
for object in objects:
if geometry := object.record.get("geometry"):
try:
features.append(
{
"type": "Feature",
"geometry": geometry,
"properties": {
"name": glom(object.record, self.object_title),
"date": glom(object.record, self.object_date),
"link": glom(object.record, self.object_link),
},
}
)
except (PathAccessError, PathAssignError):
pass

return json.dumps(
{
"type": "FeatureCollection",
"features": features,
}
)

def __str__(self):
return self.title
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% load l10n i18n %}

<a href="{{link}}" class="denhaag-action denhaag-action--single">
<div class="denhaag-action__content">{{title}}</div>
<div class="denhaag-action__details">
<div class="denhaag-action__date">
<time datetime="2023-09-28T19:47:36.593Z">{{date}}</time>
</div>
<div class="denhaag-action__actions">
<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" class="denhaag-action__link-icon" focusable="false" aria-hidden="true" shape-rendering="auto">
<path d="M12.293 5.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L16.586 13H5a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z" fill="currentColor"></path>
</svg>
</div>
</div>
</a>
11 changes: 11 additions & 0 deletions src/open_inwoner/components/templatetags/denhaag_action_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django import template

register = template.Library()


@register.inclusion_tag("components/DenhaagAction/DenhaagAction.html")
def denhaag_action(title, date, link, **kwargs):
kwargs["title"] = title
kwargs["date"] = date
kwargs["link"] = link
return kwargs
38 changes: 28 additions & 10 deletions src/open_inwoner/js/components/map/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class Map {
address_line_2,
phonenumber,
email,
date,
link,
...properties
} = feature.properties

Expand All @@ -103,6 +105,8 @@ class Map {
const displayAddress2 = escapeVariableText(address_line_2)
const displayPhonenumber = escapeVariableText(phonenumber)
const displayEmail = escapeVariableText(email)
const displayDate = escapeVariableText(date)
const buttonLink = escapeVariableText(link)
let title = ''

if (locationDetailView) {
Expand All @@ -115,23 +119,37 @@ class Map {
title = displayName
}

const phonenumberElement = `
<a href="tel:${displayPhonenumber}" class="link link--secondary" aria-label=${displayPhonenumber} title=${displayPhonenumber}>
<span class="link__text">${displayPhonenumber}</span>
</a>
`
const emailElement = `
<a href="mailto:${displayEmail}" class="link link--secondary" aria-label=${displayEmail} title=${displayEmail}>
<span class="link__text">${displayEmail}</span>
</a>
`
const buttonLinkElement = `
<a href="${buttonLink}" class="button button--bordered button--primary" aria-label=${buttonLink} title=${buttonLink}">
<span class="button__text">Action</span>
</a>
`

return `
<div class="leaflet-content-name">
<h4 class="h4">
${title}
</h4>
</div>
<div class="leaflet-content-details p--no-margin">
<p class="p">${displayAddress1}</p>
<p class="p">${displayAddress2}</p>
<a href="tel:${displayPhonenumber}" class="link link--secondary" aria-label=${displayPhonenumber} title=${displayPhonenumber}>
<span class="link__text">${displayPhonenumber}</span>
</a>
<a href="mailto:${displayEmail}" class="link link--secondary" aria-label=${displayEmail} title=${displayEmail}>
<span class="link__text">${displayEmail}</span>
</a>
</div>
`
+ '<div class="leaflet-content-details p--no-margin">'
+ (displayAddress1 ? `<p class="p">${displayAddress1}</p>` : "")
+ (displayAddress2 ? `<p class="p">${displayAddress2}</p>` : "")
+ (displayDate ? `<p class="p">${displayDate}</p>` : "")
+ (displayPhonenumber ? phonenumberElement : "")
+ (displayEmail ? emailElement : "")
+ (buttonLink ? buttonLinkElement : "")
+ '</div>'
}
}

Expand Down
Loading

0 comments on commit 9942955

Please sign in to comment.