Skip to content

Commit

Permalink
Merge pull request #65 from libris/feature/update-sigel
Browse files Browse the repository at this point in the history
Add management command for updating sigel
  • Loading branch information
andersju authored May 14, 2021
2 parents 3997817 + 8fe00be commit 1534d08
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ För att köra script som bakgrundsprocess:

Filerna hamnar under [data/excel_exports] (data/excel_exports) (under /data/appl/excel_exports på servrarna)

### Uppdatera sigel

Om ett bibliotek bytt sigel kan kommandot `update_sigel` köras för att ändra i Bibstat:

$ python manage.py update_sigel --from="GAMMALT_SIGEL" --to="NYTT_SIGEL"

## Analytics

Tjänsten genererar data till Matomo på [http://analytics.kb.se](http://analytics.kb.se).
63 changes: 63 additions & 0 deletions libstat/management/commands/update_sigel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: UTF-8 -*-

from django.core.management.base import BaseCommand
from optparse import make_option
import logging
import sys

from libstat.models import OpenData, Survey, CachedReport

logger = logging.getLogger(__name__)


class Command(BaseCommand):
args = "--from=<old sigel> --to=<new sigel>"
help = "Updates a sigel."
help_text = "Usage: python manage.py update_sigel --from=<old sigel> --to=<new sigel>\n\n"

option_list = BaseCommand.option_list + (
make_option('--from', dest='from', type='string', help=u'Old sigel'),
make_option('--to', dest='to', type='string', help=u'New sigel'),
)

def handle(self, *args, **options):
old_sigel = options.get('from')
new_sigel = options.get('to')
changed = False

if not old_sigel or not new_sigel:
print(self.help_text)
sys.exit(1)

logger.info("Changing sigel {} to {}".format(old_sigel, new_sigel))

for s in Survey.objects.filter(library__sigel__iexact=old_sigel):
logger.info("Survey {} {} {}: sigel {} to {}".format(
s.id, s.library.name, s.sample_year, s.library.sigel, new_sigel))
if s.library.sigel in s.selected_libraries:
s.selected_libraries.remove(s.library.sigel)
s.selected_libraries.append(new_sigel)

s.library.sigel = new_sigel
s.save()
if s.is_published:
logger.info("Survey {} {} {}: republishing survey".format(
s.id, s.library.name, s.sample_year
))
s.publish()
changed = True

for o in OpenData.objects.filter(sigel__iexact=old_sigel):
if o.sigel.lower() == old_sigel.lower():
logger.info("OpenData {} {} {} {}: changing sigel {} to {}".format(
o.id, o.library_name, o.sample_year, o.variable_key, o.sigel,
new_sigel))
o.sigel = new_sigel
o.save()
changed = True

if changed:
logger.info("Dropping cached reports collection")
CachedReport.drop_collection()
else:
logger.info("Nothing to do.")

0 comments on commit 1534d08

Please sign in to comment.