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

Slugs #106

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Slugs #106

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
2 changes: 1 addition & 1 deletion FAIRshakeAPI/assessments/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Assessment:
def perform(target, rubric):
want = ['metric:%d' % (metric.id) for metric in rubric.metrics.all()]
want = ['metric:%s' % (metric.slug) for metric in rubric.metrics.all()]
have = dict({
'target:%s' % (attr): v
for attr, v in target.attrs().items()
Expand Down
20 changes: 19 additions & 1 deletion FAIRshakeAPI/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@ 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 any([
self.Meta.model.objects.current.get(slug=slug) != self.instance,
self.Meta.model.objects.get(id=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 @@ -44,6 +58,7 @@ class Meta:
'image',
'tags',
'type',
'slug',
'digital_objects',
'authors',
)
Expand All @@ -58,6 +73,7 @@ class Meta:
'image',
'tags',
'type',
'slug',
'rubrics',
'authors',
)
Expand All @@ -72,6 +88,7 @@ class Meta:
'image',
'tags',
'type',
'slug',
'license',
'metrics',
'authors',
Expand All @@ -87,6 +104,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),
),
]
14 changes: 14 additions & 0 deletions FAIRshakeAPI/migrations/0011_merge_20181211_2221.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.0.7 on 2018-12-11 22:21

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('FAIRshakeAPI', '0010_remove_answer_answer_tmp'),
('FAIRshakeAPI', '0010_auto_20180912_2054'),
]

operations = [
]
9 changes: 5 additions & 4 deletions FAIRshakeAPI/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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 Down Expand Up @@ -55,7 +56,7 @@ def attrs(self):
'tags': self.tags_as_list(),
'type': self.type,
}

def has_permission(self, user, perm):
if perm in ['list', 'retrieve', 'stats']:
return True
Expand Down Expand Up @@ -188,7 +189,7 @@ def save(self, *args, **kwargs):

def invalidate_cache(self):
if self.target is not None:
k = '#target={pk}'.format(pk=self.target.pk)
k = '#target={slug}'.format(slug=self.target.slug)
l = list(
set(
json.loads(
Expand All @@ -198,7 +199,7 @@ def invalidate_cache(self):
)
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 = list(
set(
json.loads(
Expand All @@ -208,7 +209,7 @@ def invalidate_cache(self):
)
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 = list(
set(
json.loads(
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