def join(request): """ Create a django user and a user for the database Handles post requests for joins """ data = request.POST username = data['username'] # hash the password pwhash = bcrypt.hashpw(data['password'].encode('utf-8'), bcrypt.gensalt()) faculty = data['faculty'] email = data['email'] name = data['name'] phonenumber = data['phonenumber'] print('Join user') if data['kind'] == 'student': # Only student accounts have a year year = data['year'] student = Student(username=username, pwhash=pwhash, year=year, faculty=faculty, email=email, name=name, phonenumber=phonenumber) student.insert() # Create a django user user = User.objects.create_user('s' + username, email, data['password']) user.save() print(student) else: instructor = Instructor(username=username, pwhash=pwhash, faculty=faculty, email=email, name=name, phonenumber=phonenumber) instructor.insert() # Create a django user user = User.objects.create_user('i' + username, email, data['password']) user.save() print(instructor) return HttpResponseRedirect('/login')
def updateequipment(request): """ Handles updating an owned equipment from a student """ if request.user.is_authenticated: username = request.user.username[1:] stud = Student.get(username) data = request.POST equipmentid = data['equipmentid'] quantity = data['quantity'] #tradeable = data['tradeable'] checkbox = request.POST.get('tradeable', False) print('checkbox: ', checkbox == 'on') if checkbox == 'on': tradeable = 1 else: tradeable = 0 try: stud.updateOwnedEquipment(equipmentid=equipmentid, quantity=quantity, tradeable=tradeable) except IntegrityError: return HttpResponseRedirect('/student/' + username) return HttpResponseRedirect('/student/' + username) else: return HttpResponseRedirect('/')
def enroll(request): """ Handles enrolling in a class """ if request.user.is_authenticated: username = request.user.username[1:] stud = Student.get(username) data = request.POST classinfo = data['class'].split('-') print(classinfo) faculty = classinfo[0] classnum = classinfo[1] term = classinfo[2] print(faculty, classnum, term) try: stud.enroll(faculty=faculty, classnum=classnum, term=term) except IntegrityError: print("student already taking class") return HttpResponseRedirect('/student/' + username) return HttpResponseRedirect('/student/' + username) else: return HttpResponseRedirect('/')
def confirm(request): """ Handles confirming a pending trade request """ if request.user.is_authenticated: username = request.user.username[1:] stud = Student.get(username) data = request.POST tradeid = data['tradeid'] by = data['by'] # Check if the requester or responder confirmed trade occurred try: if by == 'request': # Requester confirmed trade res = stud.updatePendingTrade(tradeid=tradeid, requestconfirm=1) else: # Responder confirmed trade res = stud.updatePendingTrade(tradeid=tradeid, responseconfirm=1) print("Updated pending trade ", res) res = res[0] if int(res['responseConfirm']) + int(res['requestConfirm']) == 2: # Add to confirmed trades once both confirmed ConfirmedTrade.add(pendingtradeid=tradeid, requestusername=res['requestUsername'], responseusername=res['responseUsername'], requestequipid=res['requestEquipID'], responseequipid=res['responseEquipID']) # Remove from pending trades PendingTrade.remove(pendingtradeid=tradeid) # Decrement quantity from students, delete if no more quant = StudentHasEquipment.decrementQuantity(username=res['requestUsername'], equipid=res['requestEquipID']) quant=quant[0] print("requesting quantity", quant) if quant['quantity'] == 0: print("deleting requesting user's items") StudentHasEquipment.remove(username=res['requestUsername'], equipid=res['requestEquipID']) quant = StudentHasEquipment.decrementQuantity(username=res['responseUsername'], equipid=res['responseEquipID']) quant = quant[0] print("responding quantity", quant) if quant['quantity'] == 0: print("deleting requesting user's items") StudentHasEquipment.remove(username=res['responseUsername'], equipid=res['responseEquipID']) # Increment quantity of traded item from students, add if it doesn't exist StudentHasEquipment.addOrIncrement(username=res['requestUsername'], equipid=res['responseEquipID']) StudentHasEquipment.addOrIncrement(username=res['responseUsername'], equipid=res['requestEquipID']) except IntegrityError: return HttpResponseRedirect('/student/' + username) return HttpResponseRedirect('/student/' + username) else: return HttpResponseRedirect('/')
def cancel(request): """ Handles canceling a pending trade """ if request.user.is_authenticated: username = request.user.username[1:] stud = Student.get(username) data = request.POST tradeid = data['tradeid'] try: PendingTrade.remove(pendingtradeid=tradeid) except IntegrityError: return HttpResponseRedirect('/student/' + username) return HttpResponseRedirect('/student/' + username) else: return HttpResponseRedirect('/')
def addequipment(request): """ Handles adding an equipment """ if request.user.is_authenticated: username = request.user.username[1:] stud = Student.get(username) data = request.POST equipid = data['kind'] quantity = data['quantity'] try: stud.addOwnEquipment(equipmentid=equipid, quantity=quantity) except IntegrityError: print("student already has item") return HttpResponseRedirect('/student/' + username) return HttpResponseRedirect('/student/' + username) else: return HttpResponseRedirect('/')
def deleteequipment(request): """ Handles removing an owned equipment from a student """ if request.user.is_authenticated: username = request.user.username[1:] stud = Student.get(username) data = request.POST equipmentid = data['equipmentid'] try: stud.removeEquipment(equipmentid=equipmentid) except IntegrityError: print("student already has item") return HttpResponseRedirect('/student/' + username) return HttpResponseRedirect('/student/' + username) else: return HttpResponseRedirect('/')
def login(request): """ Handles put requests for logins """ data = request.POST username = data['username'] print(data['kind']) if data['kind'] == 'student': res = Student.get(username) user = authenticate(username='******' + data['username'], password=data['password']) print(user) if user is not None: djangologin(request, user) return HttpResponseRedirect('/student/' + res.username) else: error = "Invalid username or password" return render(request, 'swap/login.html', {'error': error}) else: res = Instructor.get(username) user = authenticate(username='******' + data['username'], password=data['password']) if user is not None: djangologin(request, user) return HttpResponseRedirect('/instructor/' + res.username) else: error = "Invalid username or password" return render(request, 'swap/login.html', {'error': error})
def drop(request): """ Handles removing a class """ if request.user.is_authenticated: username = request.user.username[1:] stud = Student.get(username) data = request.POST print("Dropping class") faculty = data['faculty'] classnum = data['classnum'] term = data['term'] try: stud.drop(faculty=faculty, classnum=classnum, term=term) except IntegrityError: return HttpResponseRedirect('/student/' + username) return HttpResponseRedirect('/student/' + username) else: return HttpResponseRedirect('/')