-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
499 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from drf_yasg import openapi | ||
from drf_yasg.utils import swagger_auto_schema | ||
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
|
||
|
||
class CountModelMixin: | ||
"""Count a queryset.""" | ||
|
||
@swagger_auto_schema(manual_parameters=[], responses={200: openapi.Response('')}) | ||
@action(detail=False, filter_backends=[], pagination_class=None) | ||
def count(self, request, *args, **kwargs): | ||
queryset = self.filter_queryset(self.get_queryset()) | ||
content = {'count': queryset.count()} | ||
return Response(content) | ||
|
||
|
||
class CreateListMixin: | ||
"""Allows bulk creation of a resource.""" | ||
|
||
def get_serializer(self, *args, **kwargs): | ||
if isinstance(kwargs.get('data', {}), list): | ||
kwargs['many'] = True | ||
|
||
return super().get_serializer(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import csv | ||
from collections import Counter | ||
from datetime import date | ||
|
||
from progressbar import progressbar | ||
from titlecase import titlecase | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from core.models import Entry | ||
from core.skosmos import get_preflabel | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Return usage of keywords of published entries' | ||
|
||
def handle(self, *args, **options): | ||
keywords_list = [] | ||
for e in Entry.objects.filter(published=True): | ||
if e.keywords: | ||
for kw in e.keywords: | ||
if kw.get('source'): | ||
keywords_list.append(kw['source']) | ||
else: | ||
keywords_list.append(kw['label']['en']) | ||
|
||
counter = Counter(keywords_list) | ||
|
||
today = date.today().strftime('%d-%m-%Y') | ||
|
||
with open(f'export/{today}_keywords_usage.csv', mode='w') as csvfile: | ||
csv_writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) | ||
csv_writer.writerow( | ||
[ | ||
'count', | ||
'source', | ||
'label_en', | ||
'label_de', | ||
] | ||
) | ||
for kw, num in progressbar(counter.most_common()): | ||
if kw.startswith('http'): | ||
graph, concept = kw.rsplit('/', 1) | ||
if 'disciplines' in kw: | ||
project = 'disciplines' | ||
else: | ||
project = 'basekw' | ||
label_en = titlecase(get_preflabel(concept, project=project, graph=f'{graph}/', lang='en')) | ||
label_de = get_preflabel(concept, project=project, graph=f'{graph}/', lang='de') | ||
|
||
csv_writer.writerow( | ||
[ | ||
num, | ||
kw, | ||
label_en, | ||
label_de, | ||
] | ||
) | ||
else: | ||
csv_writer.writerow( | ||
[ | ||
num, | ||
'', | ||
kw, | ||
kw, | ||
] | ||
) | ||
|
||
self.stdout.write(self.style.SUCCESS('Successfully exported keywords usage')) |
Oops, something went wrong.