diff --git a/ARC/ARC/urls.py b/ARC/ARC/urls.py index 95a056d..907375b 100644 --- a/ARC/ARC/urls.py +++ b/ARC/ARC/urls.py @@ -15,9 +15,16 @@ """ from django.contrib import admin from django.urls import path -from main import views +from main import views, actions +# from main.views import single_option_CDC_redirect + urlpatterns = [ - path('', admin.site.urls), + path('mapsuccess', views.map_options), + path('', admin.site.urls), + path('cdcsuccess', views.single_option_CDC_redirect), ] +# urlpatterns += patterns('myview.views', +# url(r'^(?P\w+)/', 'myview', name='myurl'), # I can't think of a better name +# ) diff --git a/ARC/main/actions.py b/ARC/main/actions.py index 7641a14..206789d 100644 --- a/ARC/main/actions.py +++ b/ARC/main/actions.py @@ -1,56 +1,65 @@ import re -from main.models import CDC, CourseSlot, Output -from main.views import map_options +from main.models import * +# from main.views import map_options from django.shortcuts import render from django.http import HttpResponse from django.template.response import TemplateResponse -from main.forms import MapsForm +from main.forms import MapsForm, SingleCDCForm +from django import forms def single_option_CDC(modeladmin, request, queryset): - for student in queryset: - branches = get_branch(student.CAMPUS_ID) - print(branches) - year = 2 - sem = 2 + form = SingleCDCForm() + form.fields["students"].initial=queryset + context = modeladmin.admin_site.each_context(request) + context["form"] = form + context['opts'] = modeladmin.model._meta + return TemplateResponse(request, "maps/single_option_CDC_select.html", context) + +single_option_CDC.short_description = "Generate single option CDC data." + +def single_option_CDC_logic(students, year, sem): + + for student in students: + # i=i+1 + stuYear = year + # print(student.CAMPUS_ID) + branches = get_branch(student.CAMPUS_ID) + # print(branches) for branch in branches: - CDCs = get_cdcs(branch, year, sem) + CDCs = get_cdcs(branch, stuYear, sem) # print(CDCs) for cdc in CDCs: if check_if_single_option_CDC(str(int(float(cdc.comp_codes)))): - print (CourseSlot.objects.filter( - course_id__endswith=str(int(float(cdc.comp_codes))))) generate_output(cdc.comp_codes, student, cdc) - else: - print("Not single option") - - -single_option_CDC.short_description = "Generate single option CDC data." + stuYear -= 1 def apply_maps(modeladmin, request, queryset): - print("BBB") - if request.method == 'POST': - form = MapsForm(request.POST) - # Do something - if form.is_valid(): - maps = form.cleaned_data["Course_Map"] - else: - print("not valid") - else: - form = MapsForm() - + form = MapsForm() + form.fields["students"].initial = queryset context = modeladmin.admin_site.each_context(request) context["form"] = form context['opts'] = modeladmin.model._meta - return TemplateResponse(request, "maps/map_options.html", context) -apply_maps.short_description = "Apple pre-defined maps" +apply_maps.short_description = "Apply pre-defined maps" + +# def get_maps(maps): +# map_names = [map.name] +# for val in CourseSlot.objects.all(): +# if if val in Map.courseSlots.all() +def apply_maps_logic(students, maps): + + for student in students: + for m in maps.all(): + print(m) + for course in m.courseSlots.all(): + generate_output_maps(course, student) def get_branch(CAMPUS_ID): branches = [] @@ -61,8 +70,9 @@ def get_branch(CAMPUS_ID): def get_cdcs(branch, year, sem): + print ("get_cdcs") return CDC.objects.filter( - tag=branch + "CDC").filter(year__contains=year).filter(sem__contains=sem) + tag=branch + "CDC").filter(year__icontains=year).filter(sem__icontains=sem) def check_if_single_option_CDC(comp_codes): @@ -71,15 +81,14 @@ def check_if_single_option_CDC(comp_codes): # course_slots = get_course_slots(comp_codes) nP = len(CourseSlot.objects.filter( - course_id__contains=comp_codes).filter(section__startswith="P")) + course_id__icontains=comp_codes).filter(section__startswith="P")) nL = len(CourseSlot.objects.filter( - course_id__contains=comp_codes).filter(section__startswith="L")) + course_id__icontains=comp_codes).filter(section__startswith="L")) nG = len(CourseSlot.objects.filter( - course_id__contains=comp_codes).filter(section__startswith="G")) + course_id__icontains=comp_codes).filter(section__startswith="G")) print(nP, nL, nG) if nP <= 1 and nL <= 1 and nG <= 1: - return True return False @@ -92,9 +101,9 @@ def get_course_slots(comp_codes): def generate_output(comp_codes, student, cdc): # print(123) # , comp_codes, cdc) - print(comp_codes[:2]) + # print(comp_codes[:2]) courseslots = get_course_slots(comp_codes) - print(courseslots) + # print(courseslots) for slot in courseslots: # print(slot) output = Output(EMPLID=student.id, @@ -105,5 +114,23 @@ def generate_output(comp_codes, student, cdc): DESCR=cdc.course_name, CLASS_NBR=int(float(slot.class_nbr)), CLASS_SECTION=slot.section) - print(output) + # print(output) output.save() + +def generate_output_maps(courseslot, student): + # print(123) # , comp_codes, cdc) + # print(comp_codes[:2]) + # courseslots = get_course_slots(comp_codes) + # print(courseslots) + # for slot in courseslots: + # print(slot) + output = Output(EMPLID=student.id, + CAMPUS_ID=student.CAMPUS_ID, + CRSE_ID=int(float(courseslot.course_id[1:])), + SUBJECT=courseslot.subject, + CATALOG_NBR=re.split('\W+', courseslot.catalog)[0], + DESCR=courseslot.course_title, + CLASS_NBR=int(float(courseslot.class_nbr)), + CLASS_SECTION=courseslot.section) + print(output) + output.save() diff --git a/ARC/main/admin.py b/ARC/main/admin.py index 16cd2c2..2fece68 100644 --- a/ARC/main/admin.py +++ b/ARC/main/admin.py @@ -4,7 +4,7 @@ # Register your models here. -from main.models import Student, CDC, CourseSlot, Output, Map +from main.models import * from django_admin_listfilter_dropdown.filters import DropdownFilter, RelatedDropdownFilter diff --git a/ARC/main/forms.py b/ARC/main/forms.py index 048ed54..67537b3 100644 --- a/ARC/main/forms.py +++ b/ARC/main/forms.py @@ -1,5 +1,5 @@ from django import forms -from main.models import Map +from main.models import * class MapsForm(forms.Form): @@ -8,3 +8,29 @@ class MapsForm(forms.Form): required=True, help_text='Which pre-defined map to apply?', ) + students = forms.ModelMultipleChoiceField( + queryset=Student.objects.all(), + required=True, + help_text='Which Students to apply to?', + ) + +class SingleCDCForm(forms.Form): + # YEAR_CHOICES=(('1','1'),('2','2'),('3','3'),('4','4'),('5','5')) + # SEM_CHOICES=(('1','1'),('2','2')) + Year = forms.IntegerField( + # choices=SEM_CHOICES, + required=True, + help_text='Which year CDCs to apply?', + ) + + Sem = forms.IntegerField( + # choices=SEM_CHOICES, + required=True, + help_text='Which sem CDCs to apply?', + ) + + students = forms.ModelMultipleChoiceField( + queryset=Student.objects.all(), + required=True, + help_text='Which Students to apply to?', + ) diff --git a/ARC/main/migrations/0004_auto_20180409_2316.py b/ARC/main/migrations/0004_auto_20180409_2316.py new file mode 100644 index 0000000..636f363 --- /dev/null +++ b/ARC/main/migrations/0004_auto_20180409_2316.py @@ -0,0 +1,28 @@ +# Generated by Django 2.0.1 on 2018-04-09 17:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('main', '0003_auto_20180222_1520'), + ] + + operations = [ + migrations.AddField( + model_name='courseslot', + name='catalog', + field=models.CharField(max_length=4, null=True), + ), + migrations.AddField( + model_name='courseslot', + name='course_title', + field=models.CharField(max_length=40, null=True), + ), + migrations.AddField( + model_name='courseslot', + name='subject', + field=models.CharField(max_length=5, null=True), + ), + ] diff --git a/ARC/main/models.py b/ARC/main/models.py index f54f540..8622ed3 100644 --- a/ARC/main/models.py +++ b/ARC/main/models.py @@ -35,34 +35,16 @@ class CourseSlot(models.Model): # CDC = models.ForeignKey(CDC, on_delete=models.PROTECT, null=True) course_id = models.CharField(max_length=6, blank=True) - # subject = models.CharField(max_length=5, null=True) - # catalog = models.CharField(max_length=4, ) + subject = models.CharField(max_length=5, null=True) + catalog = models.CharField(max_length=4, null=True) class_nbr = models.CharField(max_length=4, primary_key=True, blank=True) section = models.CharField(max_length=2, null=True) + course_title = models.CharField(max_length=40, null=True) def __str__(self): return "%s %s %s %s" % (self.course_id, self.section, self.class_nbr, get_cdc(self.course_id[1:])) - # room = models.CharField(max_length=10, null=True) - - # Class Pattern - # class_pattern = models.CharField(max_length=4, null=True) - # TODO: Make more robust - - # mtg_start = models.TimeField(null=True) - # end_time = models.TimeField(null=True) - - # instructor_ID = models.CharField(max_length=20, null=True) - # instructor_name = models.CharField(max_length=30, null=True) - # instructor_role = models.CharField(max_length=2, null=True) - - # exam_tm_cd = models.CharField(max_length=5, null=True) - # exam_date = models.DateField(null=True) - - # course_admin = models.CharField(max_length=5, null=True) - - # days = models.CharField(max_length=10, choices=DAYS) - # time_slot = models.CahrField() + class Output(models.Model): @@ -86,7 +68,12 @@ class Map(models.Model): def __str__(self): return "%s" % (self.name) +# class Students(models.Model): +# # name = models.CharField(max_length=20) +# students = models.ManyToManyField(Student) +# def __str__(self): +# return "%s" % (self.name) # helper functions def get_cdc(comp_code): diff --git a/ARC/main/resources.py b/ARC/main/resources.py index 0c20900..131783d 100644 --- a/ARC/main/resources.py +++ b/ARC/main/resources.py @@ -44,57 +44,10 @@ class Meta: import_id_fields = ['class_nbr'] fields = [ 'course_id', - 'class_nbr', - 'section', ] - # 'CDC__comp_codes', - # 'course', - # 'room', - # 'class_pattern', - # 'mtg_start', - # 'end_time', - # 'instructor_ID', - # 'instructor_name', - # 'instructor_role', - # 'exam_tm_cd', - # # 'exam_date', - # 'course_admin', - - # mtg_start = fields.Field( - # column_name="mtg_start", - # widget=TimeWidget(), - # ) - - # end_time = fields.Field( - # column_name="end_time", - # widget=TimeWidget(), - # ) - - # exam_date = fields.Field( - # column_name="exam_date", - # widget=DateWidget(), - # ) - - # def skip_row(self, instance, original): - # """ - # Returns ``True`` if ``row`` importing should be skipped. - # """ - # print("Enter") - # if not self._meta.skip_unchanged: - # # print("enter2") - # return False - # for field in self.get_import_fields(): - # print(field.column_name) - # try: - # # For fields that are models.fields.related.ManyRelatedManager - # # we need to compare the results - # if list(field.get_value(instance).all()) != list(field.get_value(original).all()): - # return False - # except AttributeError: - # if field.get_value(instance) != field.get_value(original): - # return False - # if field.column_name == "course_id":# and field.get_value(instance)==None: - # print("############",field.get_value(instance)) - # print("YAY") - # return False - - # return True + 'class_nbr', #CRSE_ID + 'section', #CLASS_SECTION + 'subject', #SUBJECT + 'catalog', #CATALOG_NBR + 'course_title', #DESCR + ] + \ No newline at end of file diff --git a/ARC/main/views.py b/ARC/main/views.py index 2190f7d..736c405 100644 --- a/ARC/main/views.py +++ b/ARC/main/views.py @@ -2,8 +2,38 @@ from django.http import HttpResponse from django.contrib.auth.models import User from django.contrib.auth.decorators import login_required +from main.forms import * +from main.actions import * +from django.template import RequestContext @login_required def map_options(request): + if request.method == 'POST': + form = MapsForm(request.POST) + if form.is_valid(): + maps = form.cleaned_data["Course_Map"] + students = form.cleaned_data["students"] + # for m in maps: + # print(m.courseSlots) + # for s in students: + # print(s) + apply_maps_logic(students, maps) + else: + print("not valid") + + return HttpResponse(":(") + return HttpResponse("Maps Generated Successfully!") - return render(request, "/maps/map_options.html") + +@login_required +def single_option_CDC_redirect(request): + if request.method == 'POST': + form = SingleCDCForm(request.POST) + if form.is_valid(): + year = form.cleaned_data["Year"] + sem = form.cleaned_data["Sem"] + students=form.cleaned_data["students"] + single_option_CDC_logic(students, year, sem) + else: + print("not valid") + return HttpResponse("CDCs Generated Successfully!") \ No newline at end of file diff --git a/ARC/requirements.txt b/ARC/requirements.txt new file mode 100644 index 0000000..7463402 --- /dev/null +++ b/ARC/requirements.txt @@ -0,0 +1,14 @@ +diff-match-patch==20121119 +Django==2.0.1 +django-admin-list-filter-dropdown==1.0.1 +django-import-export==0.7.0 +et-xmlfile==1.0.1 +jdcal==1.3 +odfpy==1.3.6 +openpyxl==2.5.1 +pytz==2018.3 +PyYAML==3.12 +tablib==0.12.1 +unicodecsv==0.14.1 +xlrd==1.1.0 +xlwt==1.3.0 diff --git a/ARC/templates/maps/map_options.html b/ARC/templates/maps/map_options.html index b6b3aab..38a3bf0 100644 --- a/ARC/templates/maps/map_options.html +++ b/ARC/templates/maps/map_options.html @@ -5,7 +5,8 @@
-
+ + {% csrf_token %} {% if form.non_field_errors|length > 0 %} diff --git a/ARC/templates/maps/single_option_CDC_select.html b/ARC/templates/maps/single_option_CDC_select.html new file mode 100644 index 0000000..5038b2b --- /dev/null +++ b/ARC/templates/maps/single_option_CDC_select.html @@ -0,0 +1,40 @@ +{% extends "admin/change_form.html" %} +{% load i18n admin_static admin_modify %} + +{% block content %} + +
+

In form

+ + {% csrf_token %} + + {% if form.non_field_errors|length > 0 %} +

+ "Please correct the errors below." +

+ {{ form.non_field_errors }} + {% endif %} + +
+ {% for field in form %} +
+ {{ field.errors }} + {{ field.label_tag }} + {{ field }} + {% if field.field.help_text %} +

+ {{ field.field.help_text|safe }} +

+ {% endif %} +
+ {% endfor %} +
+ +
+ +
+ + +
+ +{% endblock %} diff --git a/ARC/templates/maps/single_option_CDC_select_success.html b/ARC/templates/maps/single_option_CDC_select_success.html new file mode 100644 index 0000000..fad0a34 --- /dev/null +++ b/ARC/templates/maps/single_option_CDC_select_success.html @@ -0,0 +1,10 @@ +{% extends "admin/change_form.html" %} +{% load i18n admin_static admin_modify %} + +{% block content %} + +
+

Single option CDC succesfully applied

+
+ +{% endblock %}