Skip to content

Commit

Permalink
Brought slugs support to master
Browse files Browse the repository at this point in the history
Fixes #65
  • Loading branch information
u8sand committed Sep 24, 2018
1 parent aefe0a9 commit 97b837a
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 65 deletions.
17 changes: 16 additions & 1 deletion FAIRshakeAPI/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ def __init__(self, *args, **kwargs):
help_text=None,
initial=getattr(self.instance, child).all() if self.instance and self.instance.id else [],
)


def clean_slug(self):
slug = self.cleaned_data.get('slug')
try:
if self.Meta.model.objects.get(slug=slug) != self.instance:
raise forms.ValidationError(
'Slug was already taken, please try something different.'
)
except self.Meta.model.DoesNotExist:
pass
return slug

def save(self, *args, commit=True, **kwargs):
''' Explicitly add children for children in the reverse direction.
'''
Expand All @@ -43,6 +54,7 @@ class Meta:
'image',
'tags',
'type',
'slug',
'digital_objects',
'authors',
)
Expand All @@ -57,6 +69,7 @@ class Meta:
'image',
'tags',
'type',
'slug',
'rubrics',
'authors',
)
Expand All @@ -71,6 +84,7 @@ class Meta:
'image',
'tags',
'type',
'slug',
'license',
'metrics',
'authors',
Expand All @@ -86,6 +100,7 @@ class Meta:
'image',
'tags',
'type',
'slug',
'license',
'rationale',
'principle',
Expand Down
38 changes: 38 additions & 0 deletions FAIRshakeAPI/migrations/0008_auto_20180912_2050.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 2.0.7 on 2018-09-12 20:50

import builtins
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('FAIRshakeAPI', '0007_auto_20180924_1435'),
]

operations = [
migrations.AddField(
model_name='digitalobject',
name='slug',
field=models.CharField(max_length=255, default=None, null=True),
preserve_default=False,
),
migrations.AddField(
model_name='metric',
name='slug',
field=models.CharField(max_length=255, default=None, null=True),
preserve_default=False,
),
migrations.AddField(
model_name='project',
name='slug',
field=models.CharField(max_length=255, default=None, null=True),
preserve_default=False,
),
migrations.AddField(
model_name='rubric',
name='slug',
field=models.CharField(max_length=255, default=None, null=True),
preserve_default=False,
),
]
36 changes: 36 additions & 0 deletions FAIRshakeAPI/migrations/0009_auto_20180912_2051.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 2.0.7 on 2018-09-12 20:51

from django.db import migrations

def migrate(apps, schema_editor):
Project = apps.get_model('FAIRshakeAPI', 'Project')
for project in Project.objects.all():
project.slug = project.id
project.save()

DigitalObject = apps.get_model('FAIRshakeAPI', 'DigitalObject')
for obj in DigitalObject.objects.all():
obj.slug = obj.id
obj.save()

Rubric = apps.get_model('FAIRshakeAPI', 'Rubric')
for rubric in Rubric.objects.all():
rubric.slug = rubric.id
rubric.save()

Metric = apps.get_model('FAIRshakeAPI', 'Metric')
for metric in Metric.objects.all():
metric.slug = metric.id
metric.save()

class Migration(migrations.Migration):

dependencies = [
('FAIRshakeAPI', '0008_auto_20180912_2050'),
]

operations = [
migrations.RunPython(
migrate
)
]
33 changes: 33 additions & 0 deletions FAIRshakeAPI/migrations/0010_auto_20180912_2054.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 2.0.7 on 2018-09-12 20:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('FAIRshakeAPI', '0009_auto_20180912_2051'),
]

operations = [
migrations.AlterField(
model_name='digitalobject',
name='slug',
field=models.CharField(max_length=255, unique=True),
),
migrations.AlterField(
model_name='metric',
name='slug',
field=models.CharField(max_length=255, unique=True),
),
migrations.AlterField(
model_name='project',
name='slug',
field=models.CharField(max_length=255, unique=True),
),
migrations.AlterField(
model_name='rubric',
name='slug',
field=models.CharField(max_length=255, unique=True),
),
]
11 changes: 6 additions & 5 deletions FAIRshakeAPI/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class IdentifiableModelMixin(models.Model):

title = models.CharField(max_length=255, blank=False)
url = models.TextField(blank=True, null=False, default='')
slug = models.CharField(max_length=255, unique=True, blank=False, null=False)
description = models.TextField(blank=True, null=False, default='')
image = models.CharField(max_length=255, blank=True, null=False, default='')
tags = models.CharField(max_length=255, blank=True, null=False, default='')
Expand All @@ -30,7 +31,7 @@ def urls_as_list(self):

def tags_as_list(self):
return self.tags.split()

def model_name(self):
return self._meta.verbose_name_raw

Expand All @@ -43,7 +44,7 @@ def attrs(self):
'tags': self.tags,
'type': self.type,
}

def has_permission(self, user, perm):
if perm in ['list', 'retrieve', 'stats']:
return True
Expand Down Expand Up @@ -166,19 +167,19 @@ def has_permission(self, user, perm):

def save(self, *args, **kwargs):
if self.target is not None:
k = '#digital_object={pk}'.format(pk=self.target.pk)
k = '#digital_object={slug}'.format(slug=self.target.slug)
l = cache.get(k)
l = json.loads(l) if l else []
l += [k]
cache.delete_many(l)
if self.rubric is not None:
k = '#rubric={pk}'.format(pk=self.rubric.pk)
k = '#rubric={slug}'.format(slug=self.rubric.slug)
l = cache.get(k)
l = json.loads(l) if l else []
l += [k]
cache.delete_many(l)
if self.project is not None:
k = '#project={pk}'.format(pk=self.project.pk)
k = '#project={slug}'.format(slug=self.project.slug)
l = cache.get(k)
l = json.loads(l) if l else []
l += [k]
Expand Down
1 change: 1 addition & 0 deletions FAIRshakeAPI/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def update(self, instance, validated_data):

class Meta:
abstract = True
lookup_field = 'slug'

read_only_fields = (
'id',
Expand Down
Loading

0 comments on commit 97b837a

Please sign in to comment.