Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add slug field #201

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions qgis-app/plugins/migrations/0002_plugin_slug.py
Original file line number Diff line number Diff line change
@@ -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')
)
],
)
]
27 changes: 26 additions & 1 deletion qgis-app/plugins/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -372,17 +385,29 @@ 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
logging.debug('Updating modified_on for the Plugin instance')
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)


Expand Down