def club_schedule(request, id):
	club = Club.objects.get(id=id)
	path = reverse("club_page", kwargs={"id":club.id})
	schedule_form = ScheduleForm(request.POST or None, prefix='schedule')

	if request.method == 'POST':
		if schedule_form.is_valid():
		    schedule_text = schedule_form.cleaned_data['text']
		    schedule_duedate = schedule_form.cleaned_data['duedate']
		    
		    new_form = Schedule()
		    new_form.user = request.user
		    new_form.club = club
		    new_form.text = schedule_text
		    new_form.duedate = schedule_duedate
		    new_form.choice = 'club'
		    new_form.save()
		    

		    response_data = {}
		    					
		    response_data['response'] = True


		    new_data = json.dumps(response_data)
		    
		    return HttpResponse(new_data, content_type='application/json')
		else:
			data = {}
			data['response'] = False
			new_data = json.dumps(data)
			return HttpResponse(new_data, content_type='application/json')
示例#2
0
def update_Schedule(request, pk):
    schedule = get_object_or_404(Schedules, pk=pk)
    form = ScheduleForm(request.POST or None, instance=schedule)
    if form.is_valid():
        form.save()
        return redirect('schedule')
    return render(request, "schedule/update_schedule.html", {'form': form})
示例#3
0
def create_schedule(request):
    form = ScheduleForm(request.POST or None)
    schedules = Schedule.objects.order_by('deadline_date')

    if form.is_valid():
        schedule = form.save(commit=False)

        people = request.POST.getlist('involved_people')

        start_date = datetime.strptime(request.POST['start_date'],
                                       '%Y/%m/%d %H:%M')
        end_date = datetime.strptime(request.POST['end_date'],
                                     '%Y/%m/%d %H:%M')

        if end_date < start_date:

            context = {
                'form': form,
                'error': "End date cannot be before the start date"
            }

            return render(request, 'schedule/create_schedule.html', context)

        if start_date < datetime.now() or end_date < datetime.now():
            context = {
                'form':
                form,
                'error':
                "Start dates and end dates cannot be past the current date"
            }

            return render(request, 'schedule/create_schedule.html', context)

        if helper.check_overlaps(people, start_date, end_date):

            context = {
                'form': form,
                'error':
                "Failed to add schedule. Overlap/s or conflict/s found"
            }

            return render(request, 'schedule/create_schedule.html', context)

        else:
            schedule.save()

            for p in people:
                schedule.involved_people.add(p)

            messages.success(request, "Schedule added successfully!")
            return render(request, 'schedule/index.html',
                          {'schedules': schedules})

    context = {'form': form}

    return render(request, 'schedule/create_schedule.html', context)
示例#4
0
def edit_schedule_post(request, pk):
    template = 'schedule/schedule_submit.html'
    schedule_post = get_object_or_404(SchedulePost, pk=pk) # getting the object
    if schedule_post.user == request.user:
        if request.method == 'POST':
            form = ScheduleForm(request.POST, request.FILES, instance=schedule_post)
            if schedule_post.user == request.user:
                if form.is_valid():
                    schedule_post = form.save(commit=False)
                    schedule_post.title = form.cleaned_data['title']
                    schedule_post.image = form.cleaned_data['image']
                    schedule_post.faculty = form.cleaned_data['faculty']
                    schedule_post.course_name = form.cleaned_data['course_name']
                    schedule_post.modules_taken = form.cleaned_data['modules_taken']
                    schedule_post.desc = form.cleaned_data['desc']
                    schedule_post.user = request.user
                    schedule_post.save()
                    messages.success(request, "Post Saved!")
                    form = ScheduleForm()
                    return redirect('view_schedule', pk)
        else:
            form = ScheduleForm(instance=schedule_post)
    else:
        raise messages.error(request, "You are not authorized to do that!")
        return redirect('view_schedule', pk)
    
    args = {'form': form, 'schedule_post':schedule_post}
    return render(request, template, args)
示例#5
0
 def get(self, request):
     if request.user.is_authenticated:
         form = ScheduleForm()
     else:
         messages.error(request, "You are not logged in!")
         return redirect('schedule')
     args = {'form':form,}
     return render(request, self.template_name, args)
示例#6
0
def schedule_view(request):
    booker = UserProfile.objects.get(user=request.user)
    form = ScheduleForm(request.POST or None)
    if form.is_valid():
        form = form.clean()
        rendezvous = form['rendezvous']
        date = form['date']
        time = form['time']
        details = form['details']
        # Create and save new `Appointment` object.
        appt = Appointment.objects.create(rendezvous=rendezvous,
                                          date=date,
                                          time=time,
                                          details=details)
        booker.appointments.add(appt)
        return redirect(reverse('success_schedule'))
    else:
        messages.error(request, "error")
    return render(request, 'schedule/schedule_form.html', {'form': form})
示例#7
0
def start_One_Schedule(request):
    form = ScheduleForm()
    if request.method == 'POST':
        form = ScheduleForm(request.POST)

        if form.is_valid():
            obj = form.save(commit=False)
            obj.initiated_by = request.user.username
            obj.save()
            return render(request, 'schedule/schedule.html', {})
        else:
            print(form.errors)

    return render(request, 'schedule/start_one_schedule.html', {'form': form})
示例#8
0
 def post(self, request):
     form = ScheduleForm(request.POST, request.FILES)
     if form.is_valid():
         schedule_post = form.save(commit=False)
         schedule_post.title = form.cleaned_data['title']
         schedule_post.image = form.cleaned_data['image']
         schedule_post.faculty = form.cleaned_data['faculty']
         schedule_post.year = form.cleaned_data['year']
         schedule_post.semester = form.cleaned_data['semester']
         schedule_post.course_name = form.cleaned_data['course_name']
         schedule_post.modules_taken = str(form.cleaned_data['modules_taken'])
         schedule_post.desc = form.cleaned_data['desc']
         schedule_post.user = request.user
         schedule_post.save()
         form = ScheduleForm()
         return redirect('schedule')
     args = {'form': form}
     return render(request, self.template_name, args)
示例#9
0
def schedule(request):
    if request.method == 'POST':
        form = ScheduleForm(request.POST)
        if form.is_valid():
            term = form.cleaned_data['term']
    else:
        if 'term' in request.GET:
            term = Term.objects.get(value=request.GET['term'])
        else:
            profile = Profile.objects.get(user=request.user)
            if profile.default_term:
                term = profile.default_term
            else:
                term = Term.objects.all()[0]
        form = ScheduleForm()
        form.fields['term'].initial = term
    query = ScheduleEntry.objects.filter(user=request.user, term=term)
    courses = schedule_get_courses(query)
    hash = hashlib.md5(b'%s:%s' % (str(request.user.username), str(term.name))).hexdigest()[:15]
    share_url = request.build_absolute_uri('/schedule/' + hash + '/')

    credits_min = 0
    credits_max = 0
    invalid_courses = []
    if len(query) > 0:
        term = query[0].term
        user = query[0].user
        for entry in query:
            course = schedule_get_course(entry)
            if course is None:
                invalid_courses.append(entry)
                courses.remove(course)
            else:
                value = course.hours
                credits_min += int(value[:1])
                if len(value) > 1:
                    credits_max += int(value[4:])
    if credits_max > 0:
        credits_max = credits_min + credits_max

    has_exams = False
    try:
        ExamSource.objects.get(term=term)
        has_exams = True
    except ExamSource.DoesNotExist:
        has_exams = False

    table = ScheduleTable(courses)
    print_table = SchedulePrintTable(courses)
    RequestConfig(request).configure(table)
    RequestConfig(request).configure(print_table)
    context = {
        'table': table,
        'print_table': print_table,
        'form': form,
        'term': term,
        'authenticated': True,
        'by_id': False,
        'identifier': hash,
        'share': len(query) > 0,
        'share_url': share_url,
        'credits_min': credits_min,
        'credits_max': credits_max,
        'invalid_courses': invalid_courses,
        'has_exams': has_exams,
    }
    return render(request, 'schedule/course_schedule.html', context)
def schedule(request):

    if request.method == 'GET':

        if (Schedule.objects.last() is None) or (Schedule.objects.last().date != datetime.date.today()):

            form_1  = ScheduleForm(initial=get_default_json(1))
            form_2  = ScheduleForm(initial=get_default_json(2))
            form_3  = ScheduleForm(initial=get_default_json(3))
            form_4  = ScheduleForm(initial=get_default_json(4))
            form_5  = ScheduleForm(initial=get_default_json(5))
            form_6  = ScheduleForm(initial=get_default_json(6))
            form_7  = ScheduleForm(initial=get_default_json(7))
            form_8  = ScheduleForm(initial=get_default_json(8))
            form_9  = ScheduleForm(initial=get_default_json(9))
            form_10 = ScheduleForm(initial=get_default_json(10))
            form_11 = ScheduleForm(initial=get_default_json(11))
            form_12 = ScheduleForm(initial=get_default_json(12))
            form_13 = ScheduleForm(initial=get_default_json(13))
            form_14 = ScheduleForm(initial=get_default_json(14))
            form_15 = ScheduleForm(initial=get_default_json(15))
            form_16 = ScheduleForm(initial=get_default_json(16))
            form_17 = ScheduleForm(initial=get_default_json(17))
            form_18 = ScheduleForm(initial=get_default_json(18))
            form_19 = ScheduleForm(initial=get_default_json(19))
            form_20 = ScheduleForm(initial=get_default_json(20))
            form_21 = ScheduleForm(initial=get_default_json(21))
            form_22 = ScheduleForm(initial=get_default_json(22))
            form_23 = ScheduleForm(initial=get_default_json(23))
            form_24 = ScheduleForm(initial=get_default_json(24))
            form_25 = ScheduleForm(initial=get_default_json(25))
            form_26 = ScheduleForm(initial=get_default_json(26))
            form_27 = ScheduleForm(initial=get_default_json(27))
            form_28 = ScheduleForm(initial=get_default_json(28))
            form_29 = ScheduleForm(initial=get_default_json(29))
            form_30 = ScheduleForm(initial=get_default_json(30))
            form_31 = ScheduleForm(initial=get_default_json(31))
            form_32 = ScheduleForm(initial=get_default_json(32))
            form_33 = ScheduleForm(initial=get_default_json(33))
            form_34 = ScheduleForm(initial=get_default_json(34))
            form_35 = ScheduleForm(initial=get_default_json(35))
            form_36 = ScheduleForm(initial=get_default_json(36))
            form_37 = ScheduleForm(initial=get_default_json(37))
            form_38 = ScheduleForm(initial=get_default_json(38))
            form_39 = ScheduleForm(initial=get_default_json(39))
            form_40 = ScheduleForm(initial=get_default_json(40))
            form_41 = ScheduleForm(initial=get_default_json(41))
            form_42 = ScheduleForm(initial=get_default_json(42))
            form_43 = ScheduleForm(initial=get_default_json(43))
            form_44 = ScheduleForm(initial=get_default_json(44))
            form_45 = ScheduleForm(initial=get_default_json(45))
            form_46 = ScheduleForm(initial=get_default_json(46))
            form_47 = ScheduleForm(initial=get_default_json(47))
            form_48 = ScheduleForm(initial=get_default_json(48))

        else:
            schedule_object = Schedule.objects.all().order_by('-date')[:1][0]

            objects = [schedule_object.slot_1, schedule_object.slot_2, schedule_object.slot_3, schedule_object.slot_4,
                       schedule_object.slot_5, schedule_object.slot_6, schedule_object.slot_7, schedule_object.slot_8,
                       schedule_object.slot_9, schedule_object.slot_10, schedule_object.slot_11, schedule_object.slot_12,
                       schedule_object.slot_13, schedule_object.slot_14, schedule_object.slot_15, schedule_object.slot_16,
                       schedule_object.slot_17, schedule_object.slot_18, schedule_object.slot_19, schedule_object.slot_20,
                       schedule_object.slot_21, schedule_object.slot_22, schedule_object.slot_23, schedule_object.slot_24,
                       schedule_object.slot_25, schedule_object.slot_26, schedule_object.slot_27, schedule_object.slot_28,
                       schedule_object.slot_29, schedule_object.slot_30, schedule_object.slot_31, schedule_object.slot_32,
                       schedule_object.slot_33, schedule_object.slot_34, schedule_object.slot_35, schedule_object.slot_36,
                       schedule_object.slot_37, schedule_object.slot_38, schedule_object.slot_39, schedule_object.slot_40,
                       schedule_object.slot_41, schedule_object.slot_42, schedule_object.slot_43, schedule_object.slot_44,
                       schedule_object.slot_45, schedule_object.slot_46, schedule_object.slot_47, schedule_object.slot_48, ]

            form_1  = ScheduleForm(initial=objects[0])
            form_2  = ScheduleForm(initial=objects[1])
            form_3  = ScheduleForm(initial=objects[2])
            form_4  = ScheduleForm(initial=objects[3])
            form_5  = ScheduleForm(initial=objects[4])
            form_6  = ScheduleForm(initial=objects[5])
            form_7  = ScheduleForm(initial=objects[6])
            form_8  = ScheduleForm(initial=objects[7])
            form_9  = ScheduleForm(initial=objects[8])
            form_10 = ScheduleForm(initial=objects[9])
            form_11 = ScheduleForm(initial=objects[10])
            form_12 = ScheduleForm(initial=objects[11])
            form_13 = ScheduleForm(initial=objects[12])
            form_14 = ScheduleForm(initial=objects[13])
            form_15 = ScheduleForm(initial=objects[14])
            form_16 = ScheduleForm(initial=objects[15])
            form_17 = ScheduleForm(initial=objects[16])
            form_18 = ScheduleForm(initial=objects[17])
            form_19 = ScheduleForm(initial=objects[18])
            form_20 = ScheduleForm(initial=objects[19])
            form_21 = ScheduleForm(initial=objects[20])
            form_22 = ScheduleForm(initial=objects[21])
            form_23 = ScheduleForm(initial=objects[22])
            form_24 = ScheduleForm(initial=objects[23])
            form_25 = ScheduleForm(initial=objects[24])
            form_26 = ScheduleForm(initial=objects[25])
            form_27 = ScheduleForm(initial=objects[26])
            form_28 = ScheduleForm(initial=objects[27])
            form_29 = ScheduleForm(initial=objects[28])
            form_30 = ScheduleForm(initial=objects[29])
            form_31 = ScheduleForm(initial=objects[30])
            form_32 = ScheduleForm(initial=objects[31])
            form_33 = ScheduleForm(initial=objects[32])
            form_34 = ScheduleForm(initial=objects[33])
            form_35 = ScheduleForm(initial=objects[34])
            form_36 = ScheduleForm(initial=objects[35])
            form_37 = ScheduleForm(initial=objects[36])
            form_38 = ScheduleForm(initial=objects[37])
            form_39 = ScheduleForm(initial=objects[38])
            form_40 = ScheduleForm(initial=objects[39])
            form_41 = ScheduleForm(initial=objects[40])
            form_42 = ScheduleForm(initial=objects[41])
            form_43 = ScheduleForm(initial=objects[42])
            form_44 = ScheduleForm(initial=objects[43])
            form_45 = ScheduleForm(initial=objects[44])
            form_46 = ScheduleForm(initial=objects[45])
            form_47 = ScheduleForm(initial=objects[46])
            form_48 = ScheduleForm(initial=objects[47])

        context = { 'date': datetime.date.today(),
                    'form_dict': {'form_1' : form_1,
                                  'form_2' : form_2,
                                  'form_3' : form_3,
                                  'form_4' : form_4,
                                  'form_5' : form_5,
                                  'form_6' : form_6,
                                  'form_7' : form_7,
                                  'form_8' : form_8,
                                  'form_9' : form_9,
                                  'form_10': form_10,
                                  'form_11': form_11,
                                  'form_12': form_12,
                                  'form_13': form_13,
                                  'form_14': form_14,
                                  'form_15': form_15,
                                  'form_16': form_16,
                                  'form_17': form_17,
                                  'form_18': form_18,
                                  'form_19': form_19,
                                  'form_20': form_20,
                                  'form_21': form_21,
                                  'form_22': form_22,
                                  'form_23': form_23,
                                  'form_24': form_24,
                                  'form_25': form_25,
                                  'form_26': form_26,
                                  'form_27': form_27,
                                  'form_28': form_28,
                                  'form_29': form_29,
                                  'form_30': form_30,
                                  'form_31': form_31,
                                  'form_32': form_32,
                                  'form_33': form_33,
                                  'form_34': form_34,
                                  'form_35': form_35,
                                  'form_36': form_36,
                                  'form_37': form_37,
                                  'form_38': form_38,
                                  'form_39': form_39,
                                  'form_40': form_40,
                                  'form_41': form_41,
                                  'form_42': form_42,
                                  'form_43': form_43,
                                  'form_44': form_44,
                                  'form_45': form_45,
                                  'form_46': form_46,
                                  'form_47': form_47,
                                  'form_48': form_48, }}

        return render(request, 'schedule.html', context=context)

    elif request.method == 'POST':

        if 'notify' in request.POST:

            encoder = { 1: date.month * date.day * 217,
                        2: date.month * date.day * 94,
                        3: date.month * date.day * 33,
                        4: date.month * date.day * 65,
                        5: date.month * date.day * 210,
                        6: date.month * date.day * 38,
                        7: date.month * date.day * 51,
                        8: date.month * date.day * 176,
                        9: date.month * date.day * 154,
                        10: date.month * date.day * 77,
                        11: date.month * date.day * 66,
                        12: date.month * date.day * 114,
                        13: date.month * date.day * 177,
                        14: date.month * date.day * 111,
                        15: date.month * date.day * 86,
                        16: date.month * date.day * 74,
                        17: date.month * date.day * 104,
                        18: date.month * date.day * 186,
                        19: date.month * date.day * 78,
                        20: date.month * date.day * 37,
                        21: date.month * date.day * 222,
                        22: date.month * date.day * 12,
                        23: date.month * date.day * 75,
                        24: date.month * date.day * 55,
                        25: date.month * date.day * 46,
                        26: date.month * date.day * 57,
                        27: date.month * date.day * 95,
                        28: date.month * date.day * 60,
                        29: date.month * date.day * 59,
                        30: date.month * date.day * 63,
                        31: date.month * date.day * 99,
                        32: date.month * date.day * 170,
                        33: date.month * date.day * 44,
                        34: date.month * date.day * 32,
                        35: date.month * date.day * 61,
                        36: date.month * date.day * 151,
                        37: date.month * date.day * 189,
                        38: date.month * date.day * 147,
                        39: date.month * date.day * 80,
                        40: date.month * date.day * 234,
                        41: date.month * date.day * 85,
                        42: date.month * date.day * 35,
                        43: date.month * date.day * 43,
                        44: date.month * date.day * 202,
                        45: date.month * date.day * 81,
                        46: date.month * date.day * 93,
                        47: date.month * date.day * 228,
                        48: date.month * date.day * 24, }

            schedule_object = Schedule.objects.all().order_by('-date')[:1][0]

            json_dicts = [schedule_object.slot_1, schedule_object.slot_2, schedule_object.slot_3, schedule_object.slot_4,
                       schedule_object.slot_5, schedule_object.slot_6, schedule_object.slot_7, schedule_object.slot_8,
                       schedule_object.slot_9, schedule_object.slot_10, schedule_object.slot_11, schedule_object.slot_12,
                       schedule_object.slot_13, schedule_object.slot_14, schedule_object.slot_15, schedule_object.slot_16,
                       schedule_object.slot_17, schedule_object.slot_18, schedule_object.slot_19, schedule_object.slot_20,
                       schedule_object.slot_21, schedule_object.slot_22, schedule_object.slot_23, schedule_object.slot_24,
                       schedule_object.slot_25, schedule_object.slot_26, schedule_object.slot_27, schedule_object.slot_28,
                       schedule_object.slot_29, schedule_object.slot_30, schedule_object.slot_31, schedule_object.slot_32,
                       schedule_object.slot_33, schedule_object.slot_34, schedule_object.slot_35, schedule_object.slot_36,
                       schedule_object.slot_37, schedule_object.slot_38, schedule_object.slot_39, schedule_object.slot_40,
                       schedule_object.slot_41, schedule_object.slot_42, schedule_object.slot_43, schedule_object.slot_44,
                       schedule_object.slot_45, schedule_object.slot_46, schedule_object.slot_47, schedule_object.slot_48, ]

            slot     = int(request.POST['form_indicator'][1:]) # Slice it because it begins with '_'
            name     = request.POST['name']
            modality = request.POST['modality']
            phone    = request.POST['phone']
            email    = request.POST['email']

            # Change the indicator to a semi-random number, the 48-set of which change every day, so that no one can meddle
            slot_encoded = encoder[slot]

            # Use regex to limit spaces to 1, split the resulting string on whitespaces, then capitalize each segment
            name = ' '.join([x.capitalize() for x in re.sub('\s{2,}', ' ', name).split(' ')])

            # Keep 'there' lower-case when no name is provided in the form.
            if name == 'There':
                name = 'there'

            slot_json = json_dicts[slot - 1]

            slot_json['status'] = 'Sent'

            schedule_object.save()


            def send_email():

                email_body = """\
    <html>
      <head></head>
      <body style="border-radius: 20px; padding: 1rem; color: black; font-size: 1.1rem; background-color: #d5e9fb">
        <h2>Hello {0},</h2>
        <h3>We are now ready for you!</h3> </br>
        <p>Please use the button below to indicate that you are on your way. Thank you!</p> </br>
        <a href="appointmentalert.herokuapp.com/{1}/confirm"><input type="button" style="border-radius: 9px; padding: 1rem; margin: 1rem 0; color: white; background-color: #1F45FC;" value="Confirm Appointment"></a>
        </br>
      </body>
    </html>
    """.format(name, slot_encoded)
                email_message = EmailMessage('Ready for your appointment!', email_body, from_email='NBRHC Alerts <*****@*****.**>', to=[email]) # It wants a tuple or a list
                email_message.content_subtype = "html" # this is the crucial part
                email_message.send()


            def send_text():
                to = '+1' + phone
                client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
                response = client.messages.create(
                    body='\nHello {0},\n\nWe are now ready for you!\n\nPlease use the link below to indicate that you are on your way.\n\nappointmentalert.herokuapp.com/{1}/confirm\n\nThank you!'.format(name, slot_encoded),
                    to=to,
                    from_=settings.TWILIO_PHONE_NUMBER)

            if modality == 'Email':
                send_email()
            elif modality == 'Phone':
                send_text()
            elif modality == 'Phone & Email':
                send_email()
                send_text()

        else:

            if Schedule.objects.last() is None:
                schedule_object = Schedule()
                set_default_slots = True

            elif Schedule.objects.last().date == datetime.date.today():
                schedule_object = Schedule.objects.all().order_by('-date')[:1][0]
                set_default_slots = False

            else:
                schedule_object = Schedule()
                set_default_slots = True

            json_dicts = [schedule_object.slot_1, schedule_object.slot_2, schedule_object.slot_3, schedule_object.slot_4,
                       schedule_object.slot_5, schedule_object.slot_6, schedule_object.slot_7, schedule_object.slot_8,
                       schedule_object.slot_9, schedule_object.slot_10, schedule_object.slot_11, schedule_object.slot_12,
                       schedule_object.slot_13, schedule_object.slot_14, schedule_object.slot_15, schedule_object.slot_16,
                       schedule_object.slot_17, schedule_object.slot_18, schedule_object.slot_19, schedule_object.slot_20,
                       schedule_object.slot_21, schedule_object.slot_22, schedule_object.slot_23, schedule_object.slot_24,
                       schedule_object.slot_25, schedule_object.slot_26, schedule_object.slot_27, schedule_object.slot_28,
                       schedule_object.slot_29, schedule_object.slot_30, schedule_object.slot_31, schedule_object.slot_32,
                       schedule_object.slot_33, schedule_object.slot_34, schedule_object.slot_35, schedule_object.slot_36,
                       schedule_object.slot_37, schedule_object.slot_38, schedule_object.slot_39, schedule_object.slot_40,
                       schedule_object.slot_41, schedule_object.slot_42, schedule_object.slot_43, schedule_object.slot_44,
                       schedule_object.slot_45, schedule_object.slot_46, schedule_object.slot_47, schedule_object.slot_48, ]

            # Run this code if a new object has been created
            if set_default_slots == True:

                slot_counter = 0

                for slot_json in json_dicts:
                    slot_json['slot'] = slots[slot_counter]
                    slot_counter+=1

            # Get the specific slot to update based on the POST request
            slot_json = json_dicts[int(request.POST['form_indicator'][1:]) - 1]

            slot_json['slot'] = request.POST['slot']
            slot_json['name'] = request.POST['name'].capitalize()
            slot_json['modality'] = request.POST['modality']
            slot_json['phone'] = request.POST['phone']
            slot_json['email'] = request.POST['email']
            slot_json['status'] = request.POST['status']

            schedule_object.save()

        return HttpResponse()
示例#11
0
def schedule(request):
    term = Term.objects.all()[0]
    profile = Profile.objects.get(user=request.user)
    if request.method == 'POST':
        form = ScheduleForm(request.user, request.POST)
        if form.is_valid():
            term = form.cleaned_data['term']
    else:
        if 'term' in request.GET:
            term = Term.objects.get(value=request.GET['term'])
        else:
            term = Term.objects.all()[0]
            if profile.default_term:
                term = profile.default_term
        form = ScheduleForm(request.user)
        form.fields['term'].initial = term

    options_form = ScheduleOptionsForm()
    options_form.fields['show_colors'].initial = profile.show_colors_schedule
    options_form.fields['show_details'].initial = profile.show_details_schedule

    query = ScheduleEntry.objects.filter(user=request.user, term=term).order_by('course_crn')
    if len(query) is 0:
        temp = ScheduleEntry.objects.filter(user=request.user)
        if len(temp) > 0:
            term = schedule_get_course(temp[0]).term
            query = ScheduleEntry.objects.filter(user=request.user, term=term).order_by('course_crn')
    courses = schedule_get_courses(query)
    hash = get_identifier(request.user, term)
    share_url = request.build_absolute_uri('/schedule/' + hash + '/')

    credits_min = 0
    credits_max = 0
    time_min = datetime.time(8, 0, 0)
    time_max = datetime.time(18, 0, 0)
    show_sat = False
    invalid_courses = []
    deleted_courses = []
    if len(query) > 0:
        term = query[0].term
        user = query[0].user
        for entry in query:
            course = schedule_get_course(entry)
            if course is None:
                invalid_courses.append(entry)
                courses.remove(course)
            else:
                if course.deleted:
                    deleted_courses.append(course)
                value = course.hours
                credits_min += int(value[:1])
                meeting_time = course.primary_meeting_time
                if meeting_time is not None:
                    if meeting_time.start_time < time_min:
                        time_min = meeting_time.start_time
                    if meeting_time.end_time > time_max:
                        time_max = meeting_time.end_time
                    if 'S' in meeting_time.days:
                        show_sat = True
                if len(value) > 1:
                    credits_max += int(value[4:])
    if credits_max > 0:
        credits_max = credits_min + credits_max
    time_min = time_min.strftime('%H:%M:%S')
    time_max = time_max.strftime('%H:%M:%S')
    has_exams = False
    try:
        source = ExamSource.objects.get(term=term)
        has_exams = source.active
    except ExamSource.DoesNotExist:
        has_exams = False

    colors = get_colors_for_courses(courses)
    print_table = SchedulePrintTable(courses)
    RequestConfig(request).configure(print_table)
    context = {
        'courses': courses,
        'course_colors': colors,
        'print_table': print_table,
        'form': form,
        'options_form': options_form,
        'term': term,
        'authenticated': True,
        'identifier': hash,
        'has_courses': len(query) > 0,
        'share_url': share_url,
        'credits_min': credits_min,
        'credits_max': credits_max,
        'time_min': time_min,
        'time_max': time_max,
        'show_sat': show_sat,
        'invalid_courses': invalid_courses,
        'deleted_courses': deleted_courses,
        'has_exams': has_exams,
    }
    return render(request, 'schedule/schedule.html', context)