diff --git a/disqus/management/commands/disqus-import.py b/disqus/management/commands/disqus-import.py index 1ebf3be..31798c5 100644 --- a/disqus/management/commands/disqus-import.py +++ b/disqus/management/commands/disqus-import.py @@ -4,20 +4,15 @@ from disqus.models import Forum class Command(NoArgsCommand): - help = 'Import DISQUS data into local database' + help = 'Import DISQUS data into the local database' def handle_noargs(self, **options): from django.conf import settings - client = DisqusClient() - forums = client.get_forum_list(user_api_key=settings.DISQUS_API_KEY) + Forum.import_from_api() try: - forum = [f for f in forums if f['shortname'] == settings.DISQUS_WEBSITE_SHORTNAME][0] - except IndexError: + forum = Forum.objects.get(shortname=settings.DISQUS_WEBSITE_SHORTNAME) + except Forum.DoesNotExist: raise CommandError("Could not find forum with shortname '%s'. " \ "Check your 'DISQUS_WEBSITE_SHORTNAME' " \ "setting" % setting.DISQUS_WEBSITE_SHORTNAME) - forum_obj = Forum.objects.get_or_create(id=forum['id'], defaults=dict( - shortname=forum['shortname'], - name=forum['name'] - )) diff --git a/disqus/models.py b/disqus/models.py index 42520dc..80de820 100644 --- a/disqus/models.py +++ b/disqus/models.py @@ -1,6 +1,9 @@ +from django.conf import settings from django.db import models from django.utils.translation import ugettext_lazy as _ +from disqus.api import DisqusClient + class Forum(models.Model): id = models.CharField(primary_key=True, max_length=15, @@ -19,6 +22,18 @@ class Meta: def __unicode__(self): return self.name + @staticmethod + def import_from_api(): + client = DisqusClient() + forums = client.get_forum_list(user_api_key=settings.DISQUS_API_KEY) + for forum in forums: + f, created = Forum.objects.get_or_create(id=forum['id'], + defaults=dict(shortname=forum['shortname'], + name=forum['name'])) + if not created: + f.shortname = forum['shortname'] + f.name = forum['name'] + f.save() class Thread(models.Model): id = models.CharField(primary_key=True, max_length=15,