From 2458a1838d45e33bc020e7fbcaab7e349c69df09 Mon Sep 17 00:00:00 2001 From: Sumandari Date: Fri, 24 Sep 2021 16:49:45 +0800 Subject: [PATCH] add slug --- .../plugins/migrations/0002_plugin_slug.py | 38 +++++++++++++++++++ qgis-app/plugins/models.py | 27 ++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 qgis-app/plugins/migrations/0002_plugin_slug.py diff --git a/qgis-app/plugins/migrations/0002_plugin_slug.py b/qgis-app/plugins/migrations/0002_plugin_slug.py new file mode 100644 index 00000000..a89d9ed8 --- /dev/null +++ b/qgis-app/plugins/migrations/0002_plugin_slug.py @@ -0,0 +1,38 @@ +# Generated by Django 2.2.24 on 2021-09-24 02:10 + +from django.db import migrations, models + +def default_slug(apps, schema_editor): + Plugin = apps.get_model('plugins', 'Plugin') + old_data = Plugin.objects.all() + for data in old_data: + data.slug = data.package_name + data.save(update_fields=['slug']) + + +class Migration(migrations.Migration): + + dependencies = [ + ('plugins', '0001_initial'), + ] + + operations = [ + migrations.SeparateDatabaseAndState( + database_operations=[ + migrations.AddField( + model_name='plugin', + name='slug', + field=models.CharField(default='', max_length=400, verbose_name='Slug'), + preserve_default=False, + ), + migrations.RunPython(default_slug), + ], + state_operations=[ + migrations.AlterField( + model_name='plugin', + name='slug', + field=models.CharField(max_length=400, unique=True, verbose_name='Slug') + ) + ], + ) + ] diff --git a/qgis-app/plugins/models.py b/qgis-app/plugins/models.py index f0e13322..b4f86387 100644 --- a/qgis-app/plugins/models.py +++ b/qgis-app/plugins/models.py @@ -237,6 +237,13 @@ class Plugin (models.Model): # True if the plugin has a server interface server = models.BooleanField(_('Server'), default=False, db_index=True) + # slug + slug = models.CharField( + _('Slug'), + max_length=400, + unique=True, + ) + # Managers objects = models.Manager() base_objects = BasePluginManager() @@ -350,9 +357,15 @@ def clean(self): """ from django.core.exceptions import ValidationError + message = 'must start with an ASCII letter and can contain only ASCII letters, digits and the - and _ signs.' + if not re.match(r'^[A-Za-z][A-Za-z0-9-_]+$', self.package_name): raise ValidationError( - _('Plugin package_name (which equals to the main plugin folder inside the zip file) must start with an ASCII letter and can contain only ASCII letters, digits and the - and _ signs.')) + _('Plugin package_name (which equals to the main plugin folder inside the zip file) ' + message)) + + if self.slug and not re.match(r'^[A-Za-z][A-Za-z0-9-_]+$', self.slug): + raise ValidationError( + _('Plugin slug (which will be in your Plugin URL) ' + message)) if self.pk: qs = Plugin.objects.filter( @@ -372,10 +385,20 @@ def clean(self): raise ValidationError( _('A plugin with a similar package_name (%s) already exists (the package_name only differs in case).') % qs.all()[0].package_name) + if self.pk: + qs = Plugin.objects.filter( + slug__iexact=self.slug).exclude(pk=self.pk) + else: + qs = Plugin.objects.filter(slug__iexact=self.slug) + if qs.exists(): + raise ValidationError( + _('A plugin with a similar slug (%s) already exists (the slug only differs in case).') % qs.first().slug) + def save(self, keep_date=False, *args, **kwargs): """ Soft triggers: * updates modified_on if keep_date is not set + * updates slug """ if self.pk and not keep_date: import logging @@ -383,6 +406,8 @@ def save(self, keep_date=False, *args, **kwargs): self.modified_on = datetime.datetime.now() if not self.pk: self.modified_on = datetime.datetime.now() + if not self.slug: + self.slug = self.package_name super(Plugin, self).save(*args, **kwargs)