Skip to content

Commit

Permalink
Update clublog import to use ImportCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
elnappo committed Mar 8, 2019
1 parent 3b68f22 commit a8c7a5e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
7 changes: 4 additions & 3 deletions project_novis/callsign/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.utils import timezone
import requests

from ...models import CallSign, DataImport, CallsignBlacklist, Country
from ...models import Country, CallSign, DataImport, CallsignBlacklist
from ...utils import extract_callsign


Expand Down Expand Up @@ -35,6 +36,7 @@ def __init__(self, *args, **kwargs):
task=self.task,
description=self.help)
self.countries = dict(Country.objects.values_list("name", "id"))
self.session = requests.Session()
self.stdout.write(self.help)

def _warning(self, message: str):
Expand Down Expand Up @@ -64,7 +66,7 @@ def _handle_callsign(self, callsign: str, source: str = "", official: bool = Fal
self._invalid_callsign_counter += 1
raise ValueError(f"Invalid callsign {callsign}")

if raw_callsign in self._callsign_blacklist:
elif raw_callsign in self._callsign_blacklist:
self._blacklist_callsign_counter += 1
raise ValueError(f"Blacklisted callsign {callsign}")

Expand Down Expand Up @@ -94,7 +96,6 @@ def _finish(self, extra_message: str = None):
self._callsign_data_import_instance.invalid_callsigns = self._invalid_callsign_counter
self._callsign_data_import_instance.blacklisted_callsigns = self._blacklist_callsign_counter
self._callsign_data_import_instance.errors = self._error_counter
self._callsign_data_import_instance.success = True
self._callsign_data_import_instance.stop = timezone.now()
self._callsign_data_import_instance.save()

Expand Down
35 changes: 11 additions & 24 deletions project_novis/callsign/management/commands/import_clublog.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import zipfile
import json
import zipfile
from io import BytesIO

from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
import requests

from ...models import CallSign, ClublogUser
from ...utils import extract_callsign
from . import ImportCommand
from ...models import ClublogUser


class Command(BaseCommand):
class Command(ImportCommand):
help = 'Import ClubLog user data'
source = 'clublog.org'
task = "callsign_import_clublog"

def add_arguments(self, parser):
parser.add_argument('url', nargs='?', type=str, default="https://secure.clublog.org/clublog-users.json.zip")

def handle(self, *args, **options):
counter = 0
new_counter = 0
r = requests.get(options['url'], stream=False)

if r.ok:
Expand All @@ -27,21 +25,12 @@ def handle(self, *args, **options):
with z.open("clublog_users.json") as clublog_data:
data = json.load(clublog_data)
for key, value in data.items():
counter += 1
callsign = extract_callsign(key)
if not callsign:
self.stdout.write(f"Invalid callsign { key }")
try:
call_sign_instance, _ = self._handle_callsign(key)
except (ValueError,):
self._warning(f"Invalid callsign {key} {value}")
continue

call_sign_instance, new_call_sign = CallSign.objects.get_or_create(name=callsign,
defaults={"name": callsign,
"created_by": get_user_model().objects.get(id=1)})

if new_call_sign:
call_sign_instance.set_default_meta_data()
call_sign_instance.save()
new_counter += 1

# TODO add UTC timezone
clublog_user_data = {"callsign": call_sign_instance,
"clublog_first_qso": value.get("firstqso", None),
Expand All @@ -56,6 +45,4 @@ def handle(self, *args, **options):
setattr(clublog_instance, attr, a_value)
clublog_instance.save()


self.stdout.write(self.style.SUCCESS('Successfully imported %d users from which %d are new from %s' % (
counter, new_counter, options['url'])))
self._finish()

0 comments on commit a8c7a5e

Please sign in to comment.