Commit ed88708d by Carlos Andrés Rocha

Renamed models and commands for importing serial numbers.

parent 24d5c916
......@@ -7,20 +7,18 @@ from django.core.management.base import BaseCommand, CommandError
from xmodule.modulestore.django import modulestore
from licenses.models import Software, StudentLicense
from licenses.models import CourseSoftware, UserLicense
class Command(BaseCommand):
help = """Imports serial numbers for software used in a course.
Usage: import_serials course_id software_id serial_file
Usage: import_serial_numbers <course_id> <software_name> <filename>
serial_file is a text file that list one available serial number per line.
Example:
import_serials.py MITx/6.002x/2012_Fall matlab /tmp/matlab-serials.txt
django-admin.py import_serial_numbers MITx/6.002x/2012_Fall matlab /tmp/matlab-serials.txt
"""
args = "course_id software_id serial_file"
def handle(self, *args, **options):
......@@ -28,8 +26,8 @@ class Command(BaseCommand):
"""
course_id, software_name, filename = self._parse_arguments(args)
software = self._find_software(course_id, software_name)
software, _ = CourseSoftware.objects.get_or_create(course_id=course_id,
name=software_name)
self._import_serials(software, filename)
def _parse_arguments(self, args):
......@@ -51,27 +49,21 @@ class Command(BaseCommand):
return course_id, software_name, filename
def _find_software(self, course_id, software_name):
try:
software = Software.objects.get(course_id=course_id, name=software_name)
except Software.DoesNotExist:
software = Software(name=software_name, course_id=course_id)
software.save()
return software
def _import_serials(self, software, filename):
print "Importing serial numbers for {0} {1}".format(
software.name, software.course_id)
print "Importing serial numbers for {0}.".format(software)
serials = set(unicode(l.strip()) for l in open(filename))
known_serials = set(l.serial for l in StudentLicense.objects.filter(software=software))
# remove serial numbers we already have
licenses = UserLicense.objects.filter(software=software)
known_serials = set(l.serial for l in licenses)
if known_serials:
serials = serials.difference(known_serials)
count = 0
serials = list(l.strip() for l in open(filename))
for s in serials:
if s not in known_serials:
license = StudentLicense(software=software, serial=s)
license.save()
count += 1
# add serial numbers them to the database
for serial in serials:
license = UserLicense(software=software, serial=serial)
license.save()
print "{0} new serial numbers imported.".format(count)
print "{0} new serial numbers imported.".format(len(serials))
"""
"""
from django.db import models
from student.models import User
class Software(models.Model):
class CourseSoftware(models.Model):
name = models.CharField(max_length=255)
full_name = models.CharField(max_length=255)
url = models.CharField(max_length=255)
course_id = models.CharField(max_length=255)
def __unicode__(self):
return u'{0} for {1}'.format(self.name, self.course_id)
class StudentLicense(models.Model):
software = models.ForeignKey(Software, db_index=True)
class UserLicense(models.Model):
software = models.ForeignKey(CourseSoftware, db_index=True)
user = models.ForeignKey(User, null=True)
serial = models.CharField(max_length=255)
user = models.ForeignKey(User, null=True, blank=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment