Skip to content

Commit

Permalink
Merge 763b336 into db52c2d
Browse files Browse the repository at this point in the history
  • Loading branch information
arielpontes committed Jan 18, 2021
2 parents db52c2d + 763b336 commit af48bc7
Show file tree
Hide file tree
Showing 13 changed files with 588 additions and 348 deletions.
8 changes: 4 additions & 4 deletions gemet/thesaurus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
DISTANCE_NUMBER = 9
DEFAULT_LANGCODE = 'en'

PENDING = 0
PUBLISHED = 1
DELETED = 2
DELETED_PENDING = 3
PENDING = 'pending'
PUBLISHED = 'published'
DELETED = 'deleted'
DELETED_PENDING = 'deleted_pending'

SEARCH_SEPARATOR = '\t'
SEARCH_FIELDS = ['prefLabel', 'altLabel', 'notation', 'hiddenLabel']
Expand Down
57 changes: 56 additions & 1 deletion gemet/thesaurus/admin.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,69 @@
from django.contrib import admin
from django.urls import reverse
from django.utils.html import mark_safe

from gemet.thesaurus import models


class PropertyInline(admin.TabularInline):
model = models.Property
extra = 0

def get_queryset(self, request):
qs = super(PropertyInline, self).get_queryset(request)
return qs.filter(language_id='en')


class SourceRelationInline(admin.TabularInline):
model = models.Relation
extra = 0
fk_name = 'source'
raw_id_fields = ('target',)


class TargetRelationInline(admin.TabularInline):
model = models.Relation
extra = 0
fk_name = 'target'
raw_id_fields = ('source',)


class ConceptAdmin(admin.ModelAdmin):
search_fields = ('code',)
list_display = ('code', 'label', 'namespace', 'status', 'version_added')
list_filter = ('version_added__identifier', 'status', 'namespace')

inlines = [
PropertyInline, SourceRelationInline, TargetRelationInline
]


class RelationAdmin(admin.ModelAdmin):
search_fields = ('source__properties__value', 'target__properties__value')
list_display = (
'id', 'source_label', 'property_type', 'target_label', 'status',
'version_added_identifier'
)
list_filter = ('version_added__identifier', 'status')

def source_label(self, obj):
return mark_safe('<a href="{}">{}</a>'.format(
reverse("admin:thesaurus_concept_change", args=(obj.source.pk,)),
obj.source.label
))
source_label.short_description = 'Source'

def target_label(self, obj):
return mark_safe('<a href="{}">{}</a>'.format(
reverse("admin:thesaurus_concept_change", args=(obj.target.pk,)),
obj.target.label
))
target_label.short_description = 'Target'

def version_added_identifier(self, obj):
return obj.version_added.identifier
version_added_identifier.short_description = 'Version'


class ForeignRelationAdmin(admin.ModelAdmin):
search_fields = ('code',)
Expand Down Expand Up @@ -100,7 +155,7 @@ def admin_status(self, obj):
admin.site.register(models.Concept, ConceptAdmin)
admin.site.register(models.Property, PropertiesAdmin)
admin.site.register(models.Language)
admin.site.register(models.Relation)
admin.site.register(models.Relation, RelationAdmin)
admin.site.register(models.ForeignRelation, ForeignRelationAdmin)
admin.site.register(models.Theme, ThemeAdmin)
admin.site.register(models.Group, GroupAdmin)
Expand Down
28 changes: 14 additions & 14 deletions gemet/thesaurus/import_spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ def _create_concepts(self, sheet):
namespace=self.concept_ns,
version_added=self.version,
status=PENDING,
date_entered=timezone.now(),
)
print(u'Concept added: {}'.format(label))

Expand All @@ -192,10 +191,13 @@ def _create_row_relations(self, i, row):
source_label = row.get("Term") # aka prefLabel
source = self.concepts[source_label.lower()]

has_broader = False

for property_type in self.property_types:

# Look for columns specifying relationships
if property_type.name == 'broader':
has_broader = True
target_labels = [
row[key] for key in row.keys()
if 'Broader concept' in key and row[key]
Expand All @@ -212,15 +214,13 @@ def _create_row_relations(self, i, row):
if 'Theme' in key and row[key]
]

if not target_labels:
# If it doesn't exist, there is no relation to be created
else:
print(
(
'Row {} has no relationship columns '
'(i.e. "Broader concept", "Group", or "Theme").'
).format(i)
'Row {} ({}) has no "{}" relation.'.format(
i, source_label, property_type.name
)
)
return
continue

for target_label in target_labels:
target = Concept.objects.filter(
Expand Down Expand Up @@ -259,13 +259,13 @@ def _create_row_relations(self, i, row):
reverse_relation
)
)

created = source.inherit_groups_and_themes_from_broader()
print(
'Inherited groups and themes from: {}'.format(
', '.join(broader_labels)
if has_broader:
created = source.inherit_groups_and_themes_from_broader()
print(
'Inherited groups and themes from: {}'.format(
', '.join(broader_labels)
)
)
)

def _add_translations(self, sheet):
for row in row_dicts(sheet):
Expand Down
56 changes: 56 additions & 0 deletions gemet/thesaurus/migrations/0013_timetrack_concept.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2021-01-15 15:16
from __future__ import unicode_literals

from django.db import migrations, models
import django.utils.timezone


def forwards_func(apps, schema_editor):
Concept = apps.get_model("thesaurus", "Concept")
db_alias = schema_editor.connection.alias
for concept in Concept.objects.using(db_alias).all():
if concept.date_entered:
Concept.objects.filter(pk=concept.pk).update(
created_at=concept.date_entered
)
if concept.date_changed:
Concept.objects.filter(pk=concept.pk).update(
created_at=concept.date_changed
)


def reverse_func(apps, schema_editor):
pass


class Migration(migrations.Migration):

dependencies = [
('thesaurus', '0012_import'),
]

operations = [
migrations.AddField(
model_name='concept',
name='created_at',
field=models.DateTimeField(
auto_now_add=True, default=django.utils.timezone.now
),
preserve_default=False,
),
migrations.AddField(
model_name='concept',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.RunPython(forwards_func, reverse_func),
migrations.RemoveField(
model_name='concept',
name='date_changed',
),
migrations.RemoveField(
model_name='concept',
name='date_entered',
),
]
110 changes: 110 additions & 0 deletions gemet/thesaurus/migrations/0014_string_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2021-01-18 09:46
from __future__ import unicode_literals

from django.db import migrations, models


def forwards_func(apps, schema_editor):
Concept = apps.get_model("thesaurus", "Concept")
Property = apps.get_model("thesaurus", "Property")
Relation = apps.get_model("thesaurus", "Relation")
ForeignRelation = apps.get_model("thesaurus", "ForeignRelation")
str_values = {
0: 'pending',
1: 'published',
2: 'deleted',
3: 'deleted_pending',
}
db_alias = schema_editor.connection.alias
for Model in [Concept, Property, Relation, ForeignRelation]:
for int_value, str_value in str_values.items():
Model.objects.using(db_alias).filter(status=int_value).update(
string_status=str_value
)


def reverse_func(apps, schema_editor):
Concept = apps.get_model("thesaurus", "Concept")
Property = apps.get_model("thesaurus", "Property")
Relation = apps.get_model("thesaurus", "Relation")
ForeignRelation = apps.get_model("thesaurus", "ForeignRelation")
int_values = {
'pending': 0,
'published': 1,
'deleted': 2,
'deleted_pending': 3,
}
db_alias = schema_editor.connection.alias
for Model in [Concept, Property, Relation, ForeignRelation]:
for str_value, int_value in int_values.items():
Model.objects.using(db_alias).filter(
string_status=str_value
).update(status=int_value)


class Migration(migrations.Migration):

dependencies = [
('thesaurus', '0013_timetrack_concept'),
]

operations = [
migrations.AddField(
model_name='concept',
name='string_status',
field=models.CharField(choices=[(b'pending', b'pending'), (b'published', b'published'), (b'deleted', b'deleted'), (b'deleted_pending', b'deleted pending')], default=b'pending', max_length=64),
),
migrations.AddField(
model_name='foreignrelation',
name='string_status',
field=models.CharField(choices=[(b'pending', b'pending'), (b'published', b'published'), (b'deleted', b'deleted'), (b'deleted_pending', b'deleted pending')], default=b'pending', max_length=64),
),
migrations.AddField(
model_name='property',
name='string_status',
field=models.CharField(choices=[(b'pending', b'pending'), (b'published', b'published'), (b'deleted', b'deleted'), (b'deleted_pending', b'deleted pending')], default=b'pending', max_length=64),
),
migrations.AddField(
model_name='relation',
name='string_status',
field=models.CharField(choices=[(b'pending', b'pending'), (b'published', b'published'), (b'deleted', b'deleted'), (b'deleted_pending', b'deleted pending')], default=b'pending', max_length=64),
),
migrations.RunPython(forwards_func, reverse_func),
migrations.RemoveField(
model_name='concept',
name='status',
),
migrations.RemoveField(
model_name='property',
name='status',
),
migrations.RemoveField(
model_name='relation',
name='status',
),
migrations.RemoveField(
model_name='foreignrelation',
name='status',
),
migrations.RenameField(
model_name='concept',
old_name='string_status',
new_name='status'
),
migrations.RenameField(
model_name='property',
old_name='string_status',
new_name='status'
),
migrations.RenameField(
model_name='relation',
old_name='string_status',
new_name='status'
),
migrations.RenameField(
model_name='foreignrelation',
old_name='string_status',
new_name='status'
),
]

0 comments on commit af48bc7

Please sign in to comment.