diff --git a/fedireads/migrations/0005_auto_20200221_1645.py b/fedireads/migrations/0005_auto_20200221_1645.py new file mode 100644 index 00000000..6682d12a --- /dev/null +++ b/fedireads/migrations/0005_auto_20200221_1645.py @@ -0,0 +1,32 @@ +# Generated by Django 3.0.3 on 2020-02-21 16:45 +import re +from django.db import migrations, models + + +def populate_identifiers(app_registry, schema_editor): + db_alias = schema_editor.connection.alias + tags = app_registry.get_model('fedireads', 'Tag') + for tag in tags.objects.using(db_alias): + tag.identifier = re.sub(r'\W+', '-', tag.name).lower() + tag.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('fedireads', '0004_tag'), + ] + + operations = [ + migrations.AddField( + model_name='tag', + name='identifier', + field=models.CharField(max_length=100, null=True), + ), + migrations.AlterField( + model_name='tag', + name='name', + field=models.CharField(max_length=100), + ), + migrations.RunPython(populate_identifiers), + ] diff --git a/fedireads/migrations/0006_auto_20200221_1702.py b/fedireads/migrations/0006_auto_20200221_1702.py new file mode 100644 index 00000000..9b02a43f --- /dev/null +++ b/fedireads/migrations/0006_auto_20200221_1702.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.3 on 2020-02-21 17:02 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('fedireads', '0005_auto_20200221_1645'), + ] + + operations = [ + migrations.AlterField( + model_name='tag', + name='identifier', + field=models.CharField(max_length=100), + ), + ] diff --git a/fedireads/models/activity.py b/fedireads/models/activity.py index bff660ad..4e3daf04 100644 --- a/fedireads/models/activity.py +++ b/fedireads/models/activity.py @@ -3,6 +3,7 @@ from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models from django.dispatch import receiver from model_utils.managers import InheritanceManager +import re from fedireads.utils.models import FedireadsModel @@ -60,7 +61,14 @@ class Tag(FedireadsModel): ''' freeform tags for books ''' user = models.ForeignKey('User', on_delete=models.PROTECT) book = models.ForeignKey('Book', on_delete=models.PROTECT) - name = models.CharField(max_length=140) + name = models.CharField(max_length=100) + identifier = models.CharField(max_length=100) + + def save(self, *args, **kwargs): + if not self.id: + # add identifiers to new tags + self.identifier = re.sub(r'\W+', '-', self.name).lower() + super().save(*args, **kwargs) class Meta: unique_together = ('user', 'book', 'name') diff --git a/fedireads/templates/snippets/tag.html b/fedireads/templates/snippets/tag.html index c3465b5e..e298039c 100644 --- a/fedireads/templates/snippets/tag.html +++ b/fedireads/templates/snippets/tag.html @@ -1,5 +1,5 @@