def Login(request): IP_log(request) if request.method == 'POST': form = Login_form(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] if '@' in username: try: username = username.capitalize() query = User.objects.get(email=username, password=password) except ObjectDoesNotExist: query = None else: try: query = User.objects.get(username=username, password=password) except ObjectDoesNotExist: query = None if query is None or query.is_verified == False: Fail = True else: request.session['username'] = query.username request.session['uid'] = query.id request.session.modified = True is_login = True else: form = Login_form() return render_to_response('Login.html', locals(), RequestContext(request))
def Logout(request): IP_log(request) if 'username' in request.session: logout = True del request.session['username'] del request.session['uid'] request.session.modified = True return render_to_response('Logout.html', locals(), RequestContext(request)) logout = False return render_to_response('Logout.html', locals(), RequestContext(request))
def Verify(request): IP_log(request) email = request.GET['email'] code = request.GET['code'] verify = User.objects.filter(email=email, code=code) or None if verify is None: fail = True else: fail = False verify.update(verified_time=datetime.now(), is_verified=True) return render_to_response('Verify.html', locals())
def Register(request): IP_log(request) if request.method == 'POST': form = User_register_form(request.POST) if form.is_valid(): uname = form.cleaned_data['username'] password = form.cleaned_data['password'] email = form.cleaned_data['email'] student_id = email.split('@', 1)[0].upper() code = ''.join( random.choice( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ) for _ in range(15)) u = User(username=uname, password=password, email=email, is_verified=False, student_id=student_id, code=code) u.save() #send email: vlink = DOMAIN + "verify?email=" + email + "&code=" + code pass_code = "1559758205.6652055" data = { 'random': pass_code, 'link': vlink, 'email': email, 'user': uname, 'type': "Registration" } is_sent = True r = requests.post( 'https://www.csie.ntu.edu.tw/~b07902001/mail.php', data=data) return render_to_response('Register.html', locals(), RequestContext(request)) Error = True return render_to_response('Register.html', locals()) form = User_register_form() return render_to_response('Register.html', locals(), RequestContext(request))
def No2Name(request): IP_log(request) if 'username' in request.session: query_types = ['cname', 'no', 'dpt', 'ename', 'gender'] if request.method == 'GET' and 'query_type' in request.GET and 'query' in request.GET: qtype = request.GET['query_type'] if qtype not in query_types: raise Http404 query = request.GET['query'] if qtype == 'cname': student_list = ntu_student.objects.filter( chinese_name__icontains=query) elif qtype == 'no': student_list = ntu_student.objects.filter( student_no__icontains=query) elif qtype == 'dpt': student_list = ntu_student.objects.filter( department__icontains=query) elif qtype == 'ename': student_list = ntu_student.objects.filter( english_name__icontains=query) elif qtype == 'gender': student_list = ntu_student.objects.filter( gender__icontains=query) else: student_list = None has = True if student_list else False rows = len(student_list) if rows > 1000: toomuch = True user = User.objects.get(id=request.session['uid']) log = no2name_querylog(query=query, user=user, query_type=qtype) log.save() return render(request, "No2Name.html", locals()) elif request.method == 'GET': return render(request, "No2Name.html", locals()) not_login = True return render(request, "No2Name.html", locals())
def download(request): IP_log(request) if 'username' in request.session: if request.method == 'GET' and 'filename' in request.GET: filename = request.GET['filename'] f = Download_file.objects.get(filename=filename) response = HttpResponse(f.file, content_type='application/octet-stream') response[ 'Content-Disposition'] = 'attachment; filename=%s' % f.file.name.split( '/')[-1] f.download_counter += 1 f.save() user = User.objects.get(id=request.session['uid']) log = download_log(filename=f, user=user) log.save() return response files = Download_file.objects.all() return render(request, "Download.html", locals()) not_login = True # name_list, file_list = zip(*((file.name, file.file) for file in Download_file.objects.all())) # return render_to_response("Download.html",locals(),RequestContext(request)) return render(request, "Download.html", locals())
def home(request): IP_log(request) return render(request,'Home.html',locals())
def tech_blog(request): IP_log(request) return HttpResponsePermanentRedirect("https://tech-blog.jameshsu.csie.org")
def cool(request): IP_log(request) return HttpResponsePermanentRedirect("https://cool.jameshsu.csie.org")
def Renew_Password(request): IP_log(request) if 'username' in request.session: return HttpResponseRedirect('/') if request.method == "GET": if request.GET.get('email', None) is not None: if request.GET['token'] == User.objects.get( email=request.GET['email']).code: is_valid = True form = Reset_Password_form( initial={'email': request.GET['email']}) else: return HttpResponseRedirect('/') elif request.GET.get('student_id', None) is not None: form = Renew_Password_form(request.GET) if form.is_valid(): code = ''.join( random.choice( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ) for _ in range(15)) sid = form.cleaned_data['student_id'] try: u = User.objects.get(student_id=sid) except ObjectDoesNotExist: raise Http404 u.code = code u.save() email = sid + '@ntu.edu.tw' uname = u.username vlink = DOMAIN + "renew?email=" + email + "&token=" + code pass_code = "1559758205.6652055" data = { 'random': pass_code, 'link': vlink, 'user': uname, 'email': email, 'type': "Forgot_Password" } is_sent = True r = requests.post( 'https://www.csie.ntu.edu.tw/~b07902001/mail.php', data=data) else: form = Renew_Password_form() elif request.method == "POST": new_password = request.POST.get('new_password', None) if new_password is None: return HttpResponseRedirect('/') else: sid = request.POST['email'].split('@')[0] try: u = User.objects.get(student_id=sid) except ObjectDoesNotExist: raise Http404 u.password = new_password u.save() is_altered = True request.session['username'] = u.username request.session['uid'] = u.id request.session.modified = True return render_to_response('Reset_Password.html', locals(), RequestContext(request))
def Update_log(request): IP_log(request) log = update_log.objects.all() return render(request, 'Update_log.html', locals())
def External_link(request): try: url = request.POST['url'] except: url = "Error" IP_log(request, url)