-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #5 - آپدیت لیست دروس با management command "updatecourses" mana…
…gement command added
- Loading branch information
Khedesh
committed
Aug 6, 2016
1 parent
b5376d3
commit bcc3540
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!')) |