Skip to content

Commit

Permalink
Fix form redirect and single option CDC output generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sharanry committed Apr 9, 2018
1 parent 7553751 commit 537dcb0
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 82 deletions.
9 changes: 6 additions & 3 deletions ARC/ARC/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
from django.contrib import admin
from django.urls import path
from main import views, actions
from main.views import single_option_CDC_redirect
# from main.views import single_option_CDC_redirect


urlpatterns = [
path('mapsuccess', views.map_options),
path('', admin.site.urls),
path('/singleoptionsuccess', single_option_CDC_redirect),

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
# )
81 changes: 28 additions & 53 deletions ARC/main/actions.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,48 @@
import re
from main.models import CDC, CourseSlot, Output
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, SingleCDCForm
from django import forms


def single_option_CDC(modeladmin, request, queryset):

# if request.method == 'POST':
# form = SingleCDCForm(request.POST)
# if form.is_valid():
# year = form.cleaned_data["Year"]
# sem = form.cleaned_data["Sem"]
# context["year"]=year
#return redirect
# print(year, sem)
# else:
# print("not valid")
# else:
form = SingleCDCForm()

form.fields["students"].initial=queryset
context = modeladmin.admin_site.each_context(request)
context["students"]=queryset
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(queryset):
for student in queryset:
branches = get_branch(student.CAMPUS_ID)
for branch in branches:
CDCs = get_cdcs(branch, year, sem)
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")
year=year-1
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, stuYear, sem)
# print(CDCs)
for cdc in CDCs:
if check_if_single_option_CDC(str(int(float(cdc.comp_codes)))):
generate_output(cdc.comp_codes, student, cdc)
stuYear -= 1


def apply_maps(modeladmin, request, queryset):
if request.method == 'POST':
form = MapsForm(request.POST)
# Do something
if form.is_valid():
print("maps")
maps = form.cleaned_data["Course_Map"]
context["maps"]=maps
context["students"]=queryset
#return redirect

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
context = modeladmin.admin_site.each_context(request)
return TemplateResponse(request, "maps/map_options.html", context)


Expand All @@ -92,7 +68,7 @@ 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 @@ -101,15 +77,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 @@ -122,9 +97,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 @@ -135,5 +110,5 @@ 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()
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
23 changes: 17 additions & 6 deletions 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,18 +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(
Year = forms.IntegerField(
# choices=SEM_CHOICES,
required=True,
help_text='Year',
help_text='Which year CDCs to apply?',
)

Sem = forms.IntegerField(
Sem = forms.IntegerField(
# choices=SEM_CHOICES,
required=True,
help_text='Sem',
)
help_text='Which sem CDCs to apply?',
)

students = forms.ModelMultipleChoiceField(
queryset=Student.objects.all(),
required=True,
help_text='Which Students to apply to?',
)
5 changes: 5 additions & 0 deletions ARC/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,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
52 changes: 38 additions & 14 deletions ARC/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,61 @@
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required

from main.actions import check_if_single_option_CDC, get_branch, generate_output
from main.forms import *
from main.actions import *
from django.template import RequestContext

@login_required
def map_options(request):
if request.method == 'POST':
print("YES IT REACHED")
form = MapsForm(request.POST)
# myForm.cleaned_data.get('description')
# print(form.cleaned_data["students"])
# return HttpResponse(form.cleaned_data.get('students'))
if form.is_valid():
maps = form.cleaned_data["Course_Map"]
students = form.cleaned_data["students"]

# Apply logic

# return HttpResponse(form.cleaned_data["students"])
# print(maps, students)
# print(RequestContext(request))
else:
print("not valid")

return HttpResponse(":(")
return HttpResponse("WORKS!")

return render(request, "/maps/map_options.html")


@login_required
def single_option_CDC_redirect(request):

# single_option_CDC_logic(students)
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"]
# context["year"]=year
students=form.cleaned_data["students"]
# print(year, " ", sem, " ", students[0] )
single_option_CDC_logic(students, year, sem)
# return HttpResponse("IF LOOP")

# context["year"]=year
# return redirect
print(year, sem)
# print(year, sem)
else:
print("not valid")
return HttpResponse("WORKS!")
return HttpResponse("Outputs Generated Successfully!")

# return render(request, "maps/single_option_CDC_select_success.html", context)
# return render(request, "maps/single_option_CDC_select_success.html", context)


@login_required
def apply_maps_redirect(modeladmin, request, queryset):
single_option_CDC_logic(students)
# @login_required
# def apply_maps_redirect(modeladmin, request, queryset):
# single_option_CDC_logic(students)

return render(request, "maps/single_option_CDC_select_success.html", context)
# return render(request, "maps/single_option_CDC_select_success.html", context)
3 changes: 2 additions & 1 deletion ARC/templates/maps/map_options.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

<div id="content-main">

<form action="" method="POST">
<!-- <form action="/mapsuccess" method="POST"> -->
<form action="/mapsuccess" method="POST">
{% csrf_token %}

{% if form.non_field_errors|length > 0 %}
Expand Down
6 changes: 2 additions & 4 deletions ARC/templates/maps/single_option_CDC_select.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

{% block content %}



<div id="content-main">
<h1>In form</h1>
<form action="{% url 'views.single_option_CDC_redirect' %}" method="POST">
<form action="/cdcsuccess" method="POST">
{% csrf_token %}

{% if form.non_field_errors|length > 0 %}
Expand All @@ -33,7 +31,7 @@ <h1>In form</h1>
</fieldset>

<div class="submit-row">
<input type="submit" class="default" method="post" value="Submit">
<input type="submit" class="default" value="Submit">
</div>

</form>
Expand Down
2 changes: 2 additions & 0 deletions ARC/templates/maps/single_option_CDC_select_success.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

{% block content %}

<div id="content-main">
<h1>Single option CDC succesfully applied</h1>
</div>

{% endblock %}

0 comments on commit 537dcb0

Please sign in to comment.