Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix form redirect #7

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions ARC/ARC/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<user>\w+)/', 'myview', name='myurl'), # I can't think of a better name
# )
101 changes: 64 additions & 37 deletions ARC/main/actions.py
Original file line number Diff line number Diff line change
@@ -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 = []
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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()
2 changes: 1 addition & 1 deletion ARC/main/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 27 additions & 1 deletion ARC/main/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from main.models import Map
from main.models import *


class MapsForm(forms.Form):
Expand All @@ -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?',
)
28 changes: 28 additions & 0 deletions ARC/main/migrations/0004_auto_20180409_2316.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
31 changes: 9 additions & 22 deletions ARC/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
61 changes: 7 additions & 54 deletions ARC/main/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
]

Loading