Skip to content

Commit

Permalink
Resources tags (#429)
Browse files Browse the repository at this point in the history
* Init tags feature for styles

* Tags feature for styles works

* Add tags feature for geopackages

* Add tags feature for model

* Add tags feature for 3d model

* Add tags feature for layer definition

* Update resources unit tests

* Fix tags title for 3D Model and Layer Definition

* Clean print in plugins view

* Add more test cases for resources_tagcloud
  • Loading branch information
Xpirix authored Jun 19, 2024
1 parent d02f4ee commit 148aebe
Show file tree
Hide file tree
Showing 36 changed files with 603 additions and 5 deletions.
3 changes: 3 additions & 0 deletions qgis-app/base/models/processing_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import gettext_lazy as _
from taggit_autosuggest.managers import TaggableManager


class UnapprovedManager(models.Manager):
Expand Down Expand Up @@ -123,6 +124,8 @@ class Resource(models.Model):
unapproved_objects = UnapprovedManager()
requireaction_objects = RequireActionManager()

tags = TaggableManager(blank=True)

class Meta:
abstract = True

Expand Down
6 changes: 6 additions & 0 deletions qgis-app/base/views/processing_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ def get_context_data(self, **kwargs):
context["url_delete"] = "%s_delete" % self.resource_name_url_base
context["url_review"] = "%s_review" % self.resource_name_url_base
context["url_detail"] = "%s_detail" % self.resource_name_url_base
context["app_label"] = self.model._meta.app_label
context["model_name"] = self.model._meta.model_name
return context


Expand All @@ -244,6 +246,8 @@ def form_valid(self, form):
self.obj = form.save(commit=False)
self.obj.creator = self.request.user
self.obj.save()
# Without this next line the tags won't be saved.
form.save_m2m()
resource_notify(self.obj, resource_type=self.resource_name)
msg = _(self.success_message)
messages.success(self.request, msg, "success", fail_silently=True)
Expand Down Expand Up @@ -335,6 +339,8 @@ def form_valid(self, form):
obj.require_action = False
obj.approved = False
obj.save()
# Without this next line the tags won't be saved.
form.save_m2m()
resource_notify(obj, created=False, resource_type=self.resource_name)
msg = _("The %s has been successfully updated." % self.resource_name)
messages.success(self.request, msg, "success", fail_silently=True)
Expand Down
3 changes: 3 additions & 0 deletions qgis-app/geopackages/forms.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from base.forms.processing_forms import ResourceBaseCleanFileForm
from django import forms
from geopackages.models import Geopackage
from taggit.forms import TagField


class ResourceFormMixin(forms.ModelForm):
tags = TagField(required=False)
class Meta:
model = Geopackage
fields = [
"file",
"thumbnail_image",
"name",
"description",
"tags"
]


Expand Down
20 changes: 20 additions & 0 deletions qgis-app/geopackages/migrations/0010_geopackage_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.13 on 2024-06-12 06:04

from django.db import migrations
import taggit_autosuggest.managers


class Migration(migrations.Migration):

dependencies = [
('taggit', '0006_rename_taggeditem_content_type_object_id_taggit_tagg_content_8fc721_idx'),
('geopackages', '0009_alter_review_reviewer'),
]

operations = [
migrations.AddField(
model_name='geopackage',
name='tags',
field=taggit_autosuggest.managers.TaggableManager(blank=True, help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
),
]
6 changes: 6 additions & 0 deletions qgis-app/geopackages/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def test_upload_acceptable_size_file(self):
"description": "Test upload an acceptable gpkg size",
"thumbnail_image": uploaded_thumbnail,
"file": uploaded_gpkg,
"tags": "gpkg,project,test"
}
response = self.client.post(url, data, follow=True)
# should send email notify
Expand All @@ -166,6 +167,11 @@ def test_upload_acceptable_size_file(self):
)
gpkg = Geopackage.objects.first()
self.assertEqual(gpkg.name, "spiky polygons")
# Check the tags
self.assertEqual(
gpkg.tags.filter(
name__in=['gpkg', 'project', 'test']).count(),
3)
url = reverse("geopackage_detail", kwargs={"pk": gpkg.id})
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
Expand Down
2 changes: 2 additions & 0 deletions qgis-app/geopackages/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
GeopackageReviewView,
GeopackageUnapprovedListView,
GeopackageUpdateView,
GeopackageByTagView,
geopackage_nav_content,
)

Expand All @@ -35,6 +36,7 @@
GeopackageRequireActionListView.as_view(),
name="geopackage_require_action",
),
path("tags/<geopackage_tag>/", GeopackageByTagView.as_view(), name="geopackage_tag"),
# JSON
path("sidebarnav/", geopackage_nav_content, name="geopackage_nav_content"),
]
22 changes: 22 additions & 0 deletions qgis-app/geopackages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
)
from geopackages.forms import UpdateForm, UploadForm
from geopackages.models import Geopackage, Review
from django.utils.translation import gettext_lazy as _
from urllib.parse import unquote


class ResourceMixin:
Expand Down Expand Up @@ -67,6 +69,26 @@ class GeopackageReviewView(ResourceMixin, ResourceBaseReviewView):
class GeopackageDownloadView(ResourceMixin, ResourceBaseDownload):
"""Download a GeoPackage"""

class GeopackageByTagView(GeopackageListView):
"""Display GeopackageListView filtered on geopackage tag"""

def get_filtered_queryset(self, qs):
response = qs.filter(tagged_items__tag__slug=unquote(self.kwargs["geopackage_tag"]))
return response

def get_queryset(self):
qs = super().get_queryset()
return self.get_filtered_queryset(qs)

def get_context_data(self, **kwargs):
context = super(GeopackageByTagView, self).get_context_data(**kwargs)
context.update(
{
"title": _("Geopackage tagged with: %s") % unquote(self.kwargs["geopackage_tag"]),
"page_title": _("Tag: %s") % unquote(self.kwargs["geopackage_tag"])
}
)
return context

def geopackage_nav_content(request):
model = ResourceMixin.model
Expand Down
3 changes: 3 additions & 0 deletions qgis-app/layerdefinitions/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from django import forms
from layerdefinitions.file_handler import validator
from layerdefinitions.models import LayerDefinition
from taggit.forms import TagField


class ResourceFormMixin(forms.ModelForm):
tags = TagField(required=False)
class Meta:
model = LayerDefinition
fields = [
Expand All @@ -14,6 +16,7 @@ class Meta:
"url_metadata",
"description",
"license",
"tags"
]


Expand Down
20 changes: 20 additions & 0 deletions qgis-app/layerdefinitions/migrations/0003_layerdefinition_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.13 on 2024-06-12 06:04

from django.db import migrations
import taggit_autosuggest.managers


class Migration(migrations.Migration):

dependencies = [
('taggit', '0006_rename_taggeditem_content_type_object_id_taggit_tagg_content_8fc721_idx'),
('layerdefinitions', '0002_alter_review_reviewer'),
]

operations = [
migrations.AddField(
model_name='layerdefinition',
name='tags',
field=taggit_autosuggest.managers.TaggableManager(blank=True, help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
),
]
9 changes: 9 additions & 0 deletions qgis-app/layerdefinitions/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,18 @@ def setUp(self):
"thumbnail_image": self.uploaded_thumbnail,
"file": self.uploaded_file,
"license": "license",
"tags": "layerdefinition,test"
}
self.response = self.client.post(url, self.data, follow=True)

def test_tags_wavefront(self):
# Check the tags
qlr = LayerDefinition.objects.first()
self.assertEqual(
qlr.tags.filter(
name__in=['layerdefinition', 'test']).count(),
2)

def test_upload_file_succeed_send_notification(self):
self.assertEqual(self.response.status_code, 200)
# should send email notify
Expand Down
2 changes: 2 additions & 0 deletions qgis-app/layerdefinitions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
LayerDefinitionReviewView,
LayerDefinitionUnapprovedListView,
LayerDefinitionUpdateView,
LayerDefinitionByTagView,
layerdefinition_nav_content,
)

Expand Down Expand Up @@ -49,6 +50,7 @@
LayerDefinitionRequireActionListView.as_view(),
name="layerdefinition_require_action",
),
path("tags/<layerdefinition_tag>/", LayerDefinitionByTagView.as_view(), name="layerdefinition_tag"),
# JSON
path(
"sidebarnav/", layerdefinition_nav_content, name="layerdefinition_nav_content"
Expand Down
28 changes: 27 additions & 1 deletion qgis-app/layerdefinitions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
from layerdefinitions.forms import UpdateForm, UploadForm
from layerdefinitions.license import zipped_with_license
from layerdefinitions.models import LayerDefinition, Review
from django.utils.translation import gettext_lazy as _
from urllib.parse import unquote


class ResourceMixin:
"""Mixin class for Geopackage."""
"""Mixin class for LayerDefinition."""

model = LayerDefinition

Expand All @@ -52,6 +54,8 @@ def form_valid(self, form):
obj.url_datasource = get_url_datasource(obj.file.file)
obj.provider = get_provider(obj.file.file)
obj.save()
# Without this next line the tags won't be saved.
form.save_m2m()
resource_notify(obj, resource_type=self.resource_name)
msg = _(self.success_message)
messages.success(self.request, msg, "success", fail_silently=True)
Expand Down Expand Up @@ -83,6 +87,8 @@ def form_valid(self, form):
obj.url_datasource = get_url_datasource(obj.file.file)
obj.provider = get_provider(obj.file.file)
obj.save()
# Without this next line the tags won't be saved.
form.save_m2m()
resource_notify(obj, created=False, resource_type=self.resource_name)
msg = _("The %s has been successfully updated." % self.resource_name)
messages.success(self.request, msg, "success", fail_silently=True)
Expand Down Expand Up @@ -111,6 +117,26 @@ class LayerDefinitionDeleteView(ResourceMixin, ResourceBaseDeleteView):
class LayerDefinitionReviewView(ResourceMixin, ResourceBaseReviewView):
"""Create a review."""

class LayerDefinitionByTagView(LayerDefinitionListView):
"""Display LayerDefinitionListView filtered on layerdefinition tag"""

def get_filtered_queryset(self, qs):
response = qs.filter(tagged_items__tag__slug=unquote(self.kwargs["layerdefinition_tag"]))
return response

def get_queryset(self):
qs = super().get_queryset()
return self.get_filtered_queryset(qs)

def get_context_data(self, **kwargs):
context = super(LayerDefinitionByTagView, self).get_context_data(**kwargs)
context.update(
{
"title": _("LayerDefinition tagged with: %s") % unquote(self.kwargs["layerdefinition_tag"]),
"page_title": _("Tag: %s") % unquote(self.kwargs["layerdefinition_tag"])
}
)
return context

class LayerDefinitionDownloadView(ResourceMixin, ResourceBaseDownload):
"""Download a Layer Definition File (.qlr)."""
Expand Down
3 changes: 3 additions & 0 deletions qgis-app/models/forms.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from base.forms.processing_forms import ResourceBaseCleanFileForm
from django import forms
from models.models import Model
from taggit.forms import TagField


class ResourceFormMixin(forms.ModelForm):
tags = TagField(required=False)
class Meta:
model = Model
fields = [
"file",
"thumbnail_image",
"name",
"description",
"tags"
]


Expand Down
20 changes: 20 additions & 0 deletions qgis-app/models/migrations/0008_model_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.13 on 2024-06-12 06:04

from django.db import migrations
import taggit_autosuggest.managers


class Migration(migrations.Migration):

dependencies = [
('taggit', '0006_rename_taggeditem_content_type_object_id_taggit_tagg_content_8fc721_idx'),
('models', '0007_alter_review_reviewer'),
]

operations = [
migrations.AddField(
model_name='model',
name='tags',
field=taggit_autosuggest.managers.TaggableManager(blank=True, help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
),
]
Loading

0 comments on commit 148aebe

Please sign in to comment.