def new_project(request): valid=0 if request.method == "POST": project_form=Projectform(request.POST) password_form=PasswordForm(request.POST) if project_form.is_valid(): sig=project_form.cleaned_data["SIG"] name=project_form.cleaned_data["Name"] des=project_form.cleaned_data["Description"] rep_link=project_form.cleaned_data["Report_link"] pos_link=project_form.cleaned_data["Poster_link"] project_obj=Projects.objects.create(sig_id=SIG.objects.get(pk=sig),name=name,description=des,report_link=rep_link,poster_link=pos_link) project_obj.save() messages.success(request, "Project successfully added") return redirect('/acm/') if password_form.is_valid(): if password_form.cleaned_data["key"] == "PASSWORD": valid = 1 else: messages.MessageFailure(request, 'Incorrect password') else : password_form=PasswordForm() project_form=Projectform() sigo = SIG.objects.all() context={ 'sigo':sigo, 'project_form':project_form, 'password_form':password_form ,'valid':valid } return render(request,'acm/projects_form.html',context)
def update_event(request,event_id): valid=0 if request.method=="POST": event_form=EventsForm(request.POST) password_form=PasswordForm(request.POST) if event_form.is_valid(): sig=event_form.cleaned_data["SIG"] name=event_form.cleaned_data["Name"] des=event_form.cleaned_data["Description"] event_obj=Events.objects.create(sig_id=SIG.objects.get(pk=sig),name=name,description=des) event_obj.save() Events.objects.get(pk=event_id).delete() messages.success(request, "Event successfully added") return redirect('/acm/') if password_form.is_valid(): if password_form.cleaned_data["key"] == "PASSWORD": valid = 1 else: messages.MessageFailure(request, 'Incorrect password') else : password_form=PasswordForm() sigo = SIG.objects.all() events = Events.objects.get(pk=event_id) s=[] s.append(events.name) s.append(events.description) event_form=EventsForm(initial={'SIG':events.sig_id}) context={ 'sigo':sigo, 'event_form':event_form, 'password_form':password_form , 'valid':valid, 's':s, } return render(request,'acm/event_form.html',context)
def book(request): if not request.user.is_manager: if request.method == "POST": form = bookings_form(request.POST) if form.is_valid(): date = form.cleaned_data["date"] time_slot = Time_slots.objects.get( pk=form.cleaned_data["time_slot"]) booking = [ i.room for i in Bookings.objects.filter(date=date).filter( slot_id=time_slot) ] for i in Room.objects.all(): if i not in booking: break obj = Bookings.objects.create(user=request.user, date=date, slot_id=time_slot, room=i) obj.save() messages.success(request, "Slot successfully added") return redirect('/book/') else: print(form) messages.MessageFailure(request, 'Not created') form = bookings_form() context = {'form': form, 'max': advance.objects.get(pk=1).no_days} return render(request, 'book/book_form.html', context) else: return HttpResponse('<h1>Manager</h1>')
def new_smp(request): validated = 0 static_fields = ['Name', 'Mentors', 'Overview', 'Platform'] dynammic_fields = ['Exercise', 'Prerequisite', 'Course-content'] if request.method == "POST": password_form = PasswordForm(request.POST) smp_form = SMPForm(request.POST) if smp_form.is_valid(): sig = smp_form.cleaned_data["SIG"] name = smp_form.cleaned_data["Name"] mentors = smp_form.cleaned_data["Mentors"] overview = smp_form.cleaned_data["Overview"] platform = smp_form.cleaned_data["Platform"] SMP_object = SMP.objects.create( sig_id=SIG.objects.get(pk=sig), name=name, mentors=mentors, overview=overview, platform_of_tutoring=platform) SMP_object.save() for field in dynammic_fields: for i in range(smp_form.cleaned_data[field.lower() + "_freq"]): des = smp_form.cleaned_data[field+'_' + str(i+1)] if des: SMP_des_object = SMP_des.objects.create( smp_id=SMP_object, sub_heading=field, sub_des=des) SMP_des_object.save() for i in range(smp_form.cleaned_data["week_freq"]): for j in range(smp_form.cleaned_data["week_"+str(i+1)+"_description_freq"]): des = smp_form.cleaned_data["Week_" + str(i+1)+"_Description_"+str(j+1)] if des: SMP_des_object = SMP_des.objects.create( smp_id=SMP_object, sub_heading="Week "+str(i+1), sub_des=des) SMP_des_object.save() messages.success(request, "SMP created") return redirect('acm:home_page') if password_form.is_valid(): # To be hidden somehow if password_form.cleaned_data["key"] == "PASSWORD": validated = 1 else: messages.MessageFailure(request, 'Incorrect password') else: password_form = PasswordForm() smp_form = SMPForm() sigo = SIG.objects.all() context = {'password_form': password_form, 'smp_form': smp_form, 'validated': validated, 'static_fields': static_fields, 'dynammic_fields': dynammic_fields, 'range_of_8': [1, 2, 3, 4, 5, 6, 7, 8], 'sigo': sigo, } return render(request, 'smp_form.html', context)
def about_view(response, *awrgs): if response.method == "POST": form = subscribe(response.POST) if form.is_valid(): form.save() return redirect("about.html") else: messages.MessageFailure() return render(response, "about.html")
def delete_component(request,type,id): valid=0 if request.method == "POST": password_form=PasswordForm(request.POST) if password_form.is_valid(): if password_form.cleaned_data["key"] == "PASSWORD": valid = 1 if type=="projects": try: Projects.objects.get(id=id).delete() messages.success(request, "Project deleted") except: messages.MessageFailure(request, "Couldn't delete project") elif type=="events": try: Events.objects.get(id=id).delete() messages.success(request, "Event deleted") except: messages.MessageFailure(request, "Couldn't delete event") elif type=="smp": try: SMP.objects.get(id=id).delete() messages.success(request, "SMP deleted") except: messages.MessageFailure(request, "Couldn't delete SMP") return redirect('acm:home_page') else: messages.MessageFailure(request, 'Incorrect password') else: messages.MessageFailure(request, 'Incorrect password') else: password_form=PasswordForm() sigo = SIG.objects.all() context={ 'sigo':sigo, 'password_form':password_form ,'valid':valid } return render(request,'acm/projects_form.html',context)
def UserReg(request): userform = UserCreationForm() if request.method == "POST": userform = UserCreationForm(request.POST) if userform.is_valid(): userform.save() messages.success(request, 'successfully created user account') else: messages.MessageFailure( request, 'Sorry your account is not created try again') context = {'userforms': userform} return render(request, 'user/reg.html', context)
def participate(request, contest_id): contest = get_object_or_404(Contest, pk=contest_id) current_user = request.user user = User.objects.get(username=current_user.username) try: ContestParticipant.objects.filter(participant_id=user.id).get(participate_to_id=contest.id) except exceptions.ObjectDoesNotExist: contest.participants.add(user) return HttpResponseRedirect('../') else: messages.MessageFailure('Failed') return HttpResponse('您已经参加此比赛,请不要重复参与')
def register(request): if request.method == 'POST': f = UserCreationForm(request.POST) if f.is_valid(): f.save() messages.success(request, 'Account created successfully') return HttpResponseRedirect('/web/register') else: messages.MessageFailure(request, 'Failed Tray Again') f = UserCreationForm() template = loader.get_template('web/add_worker.html') return HttpResponse(template.render({'form': f}, request))
def add_to_basket(request, pk): recipe = get_object_or_404(Recipe, pk=pk) basket, created = Basket.objects.get_or_create(request) # basketIngs = Basket.objects.get(request).conents currentRecipes = basket.recipes if recipe.title not in currentRecipes: basket.contents[pk] = recipe.ingredients basket.recipes[pk] = recipe.title basket.save() recipe.inCart = True recipe.save() messages.success(request, "Basket Updated!") else: messages.MessageFailure(request, "Basket not updated, duplicate item") return slb_index(request)
def edit(request, List_id): if request.method == 'POST': item = List.objects.get(pk=List_id) form = ListForm(request.POST or None, instance=item) if form.is_valid(): form.save() messages.success(request, ('item Has been Edited')) return redirect('index') else: item = List.objects.get(pk=List_id) messages.MessageFailure(request, ('Entry is empty')) return render(request, 'edit.html') else: item = List.objects.get(pk=List_id) return render(request, 'edit.html', {'item': item})
def remove_from_basket(request, pk): print("RMB") recipe = get_object_or_404(Recipe, pk=pk) basket, created = Basket.objects.get_or_create(request) print(basket.recipes) if str(pk) in basket.recipes: # if recipe in currentRecipes: print("IN") recipe.inCart = False recipe.save() basket.contents.pop(str(pk)) basket.recipes.pop(str(pk)) basket.save() messages.success(request, "Basket Updated!") else: messages.MessageFailure(request, "Item not in basket. Can't remove.") return slb_index(request)
def slot(request): if not request.user.is_manager: return HttpResponse('<h1>Customer</h1>') else: if request.method == "POST": form = time_slot_form(request.POST) if form.is_valid(): ini_time = form.cleaned_data["ini_time"] end_time = form.cleaned_data["end_time"] obj = Time_slots.objects.create(int_time=ini_time, end_time=end_time) obj.save() messages.success(request, "Slot successfully added") return redirect('/book/') else: messages.MessageFailure(request, 'Not created') form = time_slot_form() x = datetime.datetime.now().date() + datetime.timedelta( days=advance.objects.get(pk=1).no_days) context = {'form': form, 'x': x} return render(request, 'book/slot_form.html', context)