]> git.parisson.com Git - teleforma.git/commitdiff
fix loader tests, add json serializer for professors (input and output)
authoryomguy <yomguy@parisson.com>
Fri, 13 Jul 2012 14:28:19 +0000 (16:28 +0200)
committeryomguy <yomguy@parisson.com>
Fri, 13 Jul 2012 14:28:19 +0000 (16:28 +0200)
teleforma/management/commands/teleforma-export-professors.py [new file with mode: 0644]
teleforma/management/commands/teleforma-import-professors.py [new file with mode: 0644]
teleforma/models.py
teleforma/templatetags/teleforma_tags.py

diff --git a/teleforma/management/commands/teleforma-export-professors.py b/teleforma/management/commands/teleforma-export-professors.py
new file mode 100644 (file)
index 0000000..88bddea
--- /dev/null
@@ -0,0 +1,42 @@
+from optparse import make_option
+from django.conf import settings
+from django.core.management.base import BaseCommand, CommandError
+from django.contrib.auth.models import User
+from django.template.defaultfilters import slugify
+from telemeta.models import *
+from telemeta.util.unaccent import unaccent
+from teleforma.models import *
+import logging
+import json
+
+class Command(BaseCommand):
+    help = "Export professors to a JSON file"
+    args = "path"
+    admin_email = 'webmaster@parisson.com'
+
+    def export(self):
+        list = []
+        professors = Professor.objects.all()
+        for professor in professors:
+            user = professor.user
+            courses = [course.code for course in professor.courses.all()]
+            list.append({'username': 'aa'+user.username,
+                         'first_name': user.first_name,
+                         'last_name': user.last_name,
+                         'email': user.email,
+                         'courses': courses,
+                         })
+            print 'exported: ' + user.first_name + ' ' + user.last_name + ' ' + user.username
+        return json.dumps(list)
+
+
+    def handle(self, *args, **options):
+        file = args[0]
+        json = self.export()
+        f = open(file, 'w')
+        f.write(json)
+        f.close()
+
+
+
+
diff --git a/teleforma/management/commands/teleforma-import-professors.py b/teleforma/management/commands/teleforma-import-professors.py
new file mode 100644 (file)
index 0000000..a69bd9d
--- /dev/null
@@ -0,0 +1,43 @@
+from optparse import make_option
+from django.conf import settings
+from django.core.management.base import BaseCommand, CommandError
+from django.contrib.auth.models import User
+from django.template.defaultfilters import slugify
+from telemeta.models import *
+from telemeta.util.unaccent import unaccent
+from teleforma.models import *
+import logging
+import json
+
+
+class Command(BaseCommand):
+    help = "Import professors from a JSON file"
+    args = "path"
+    admin_email = 'webmaster@parisson.com'
+
+
+    def import_professors(self, data):
+        professors = json.loads(data)
+        for professor in professors:
+            user, c = User.objects.get_or_create(username=professor['username'],
+                                                 first_name=professor['first_name'],
+                                                 last_name=professor['last_name'],
+                                                 email=professor['email'])
+            if c:
+                p = Professor(user=user)
+                p.save()
+                for code in professor['courses']:
+                    course = Course.objects.get(code=code)
+                    p.courses.add(course)
+                print 'imported: ' + user.first_name + ' ' + user.last_name + ' ' + user.username
+
+    def handle(self, *args, **options):
+        file = args[0]
+        f = open(file, 'r')
+        data = f.read()
+        f.close()
+        self.import_professors(data)
+
+
+
+
index 5a41e6b6a2f6fbfc6ccfb5986ae8095ef4a58ac3..0c97b4aefe0f8f598520b8c5bfb4964d0f476ee6 100755 (executable)
@@ -161,8 +161,10 @@ class Course(Model):
 
 class Professor(Model):
 
-    user            = ForeignKey(User, related_name='professor', verbose_name=_('user'), unique=True)
-    courses         = ManyToManyField('Course', related_name="professor", verbose_name=_('courses'),
+    user            = ForeignKey(User, related_name='professor',
+                                 verbose_name=_('user'), unique=True)
+    courses         = ManyToManyField('Course', related_name="professor",
+                                        verbose_name=_('courses'),
                                         blank=True, null=True)
 
     def __unicode__(self):
index 63da4f990a6d3050422b70193372c35eede23e9f..d6fcf96d76e66656e1922eb8b8aaf7e8ef30969f 100644 (file)
@@ -165,7 +165,6 @@ def get_all_courses():
 
 @register.assignment_tag
 def get_telecaster():
-    print settings.INSTALLED_APPS
     return 'telecaster' in settings.INSTALLED_APPS
 
 @register.assignment_tag