Skip to content

Commit

Permalink
Issue #5 - آپدیت لیست دروس با management command "updatecourses" mana…
Browse files Browse the repository at this point in the history
…gement command added
  • Loading branch information
Khedesh committed Aug 6, 2016
1 parent b5376d3 commit bcc3540
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Empty file added course/management/__init__.py
Empty file.
Empty file.
59 changes: 59 additions & 0 deletions course/management/commands/updatecourses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import json

# import urllib2
from django.core.management import BaseCommand

try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied

from base.models import EducationalYear

from course.models import Professor, Course, OfferedCourse


def get_current_year():
return EducationalYear.objects.get_or_create(year=settings.CURRENT_YEAR)[0]


class Command(BaseCommand):
def handle(self, *args, **options):
url = "http://term.inator.ir/courses/list/38/"
data = urlopen(url)
courses_list = json.loads(data.read().decode('utf8'))
for item in courses_list:
grp = item['course_id'].split('-')[-1]
exam_time = item['exam_time']
prf = item['instructor']
prf = Professor.objects.get_or_create(name=prf)
crs = item['course_number']
crs_name = item['name']
crs = Course.objects.get_or_create(course_number=crs)
dsc = item['info']
capacity = int(item['capacity'])
self.stdout.write('Professor: %s' % item['instructor'])
try:
obj = OfferedCourse.objects.get(group_number=int(grp),
course=crs[0],
professor=prf[0],
term=settings.CURRENT_TERM,
year=get_current_year(), )
obj.capacity = capacity
obj.details = dsc
obj.exam_time = exam_time
except OfferedCourse.DoesNotExist:
obj = OfferedCourse.objects.create(group_number=int(grp),
course=crs[0],
professor=prf[0],
term=settings.CURRENT_TERM,
year=get_current_year(),

capacity=capacity,
details=dsc,
exam_time=exam_time, )
self.stdout.write(self.style.SUCCESS('Success!'))

0 comments on commit bcc3540

Please sign in to comment.