def index(request): params = {} params.update(csrf(request)) if request.user.is_authenticated(): return xrender(request, 'index.html', params) else: return xrender(request, 'login.html', params)
def set_phone(request): if request.user.profile.is_phone_set: return redirecterror(request, 'Your phone is already set.') if request.method == 'POST': if 'type' not in request.POST or request.POST['type'] not in ['get_random_code', 'submit', ]: return JsonError('Invalid request.') if request.POST['type'] == 'get_random_code': if 'cellphone' not in request.POST or not request.POST['cellphone']: return JsonError('Must provide cell phone number.') cellphone = request.POST['cellphone'] if not cellphonevalid(cellphone): return JsonError('Cell phone format incorrect: must be 10 digit number without slashes.') request.session['cellphone'] = cellphone send_random_code(request, cellphone) return JsonSuccess('Random code sent.') else: if 'randomcode' not in request.POST or not request.POST['randomcode']: return JsonError('Must provide randomcode.') randomcode = request.POST['randomcode'] cellphone = request.session.get('cellphone') code_dict = request.session.get('code_dict') if not cellphone or not code_dict: return JsonError('Phone number not found. Please retype your phone number and submit again.') cell_mail = code_dict.get(randomcode) if not cell_mail: return JsonError('Code incorrect or we do not support your phone service.') request.user.profile.set_phone(cellphone, cell_mail) del request.session['code_dict'] del request.session['cellphone'] return JsonSuccess('Your phone is set up.') else: params = {} params['cellphone'] = request.session.get('cellphone', '') params.update(csrf(request)) return xrender(request, 'set_phone.html', params)
def set_email(request): if request.user.profile.is_email_set: return redirecterror(request, 'Your email is already set.') if request.method == 'POST': if 'type' not in request.POST or request.POST['type'] not in ['get_random_code', 'submit', ]: return JsonError('Invalid request.') if request.POST['type'] == 'get_random_code': if 'email' not in request.POST or not request.POST['email']: return JsonError('Must provide email address.') email = request.POST['email'] if not emailvalid(email): return JsonError('Invalid email address.') send_email_verification(request, email) return JsonSuccess('Random code sent.') else: if 'randomcode' not in request.POST or not request.POST['randomcode']: return JsonError('Must provide randomcode.') randomcode = request.POST['randomcode'] if randomcode == request.user.profile.email_short_code: request.user.profile.set_email() return JsonSuccess('Your email is set up.') else: return JsonError('Incorrect code.') else: params = {} params.update(csrf(request)) return xrender(request, 'set_email.html', params)
def login(request): if request.method == 'POST': if request.user.is_authenticated(): return JsonError("Already logged in.") try: username = request.POST['username'].lower() password = request.POST['password'] except Exception: return JsonError("Must provide both username and password.") if not username or not password: return JsonError("Must provide both username and password.") if '@' in username: return JsonError("Please use your username instead of email address.") auth_successful = login_ninjacourses(username=username, password=password) if not auth_successful: return JsonError("We failed to authenticate your account on ninjacourses. Either the information is incorrect or the ninjacourses server is down.") user = auth.authenticate(username=username, password=password) auth.login(request, user) return JsonSuccess() else: params = {} try: params['next'] = request.GET['next'] except Exception: params['next'] = '/' params.update(csrf(request)) if request.user.is_authenticated(): return redirect('index') else: return xrender(request, 'login.html', params)
def compare_schedule(request): cmp_data = fetch_compare_data(request.user.profile) cmp_result = '<div class="form-header">According to your friend list, you will attend...</div><div class="comparelist">'; for course_name, friend_list in cmp_data: cmp_result += '<div class="compareitem"><span class="coursename">%s</span> with %s.</div>' % (course_name, get_friend_expression(friend_list)) cmp_result += '</div>' return xrender(request, 'compare_schedule.html', {'cmp_result': cmp_result})
def manage_monitor_course(request): if not request.user.profile.is_contact_set: return redirecterror( request, 'You need to set up your cell phone or email first.') params = {} params.update(csrf(request)) return xrender(request, 'manage_monitor_course.html', params)
def edit_shortlink(request, shortname): link_obj_list = ShortLink.objects.filter(user_profile=request.user.profile, shortname=shortname) if request.method == 'POST': try: if not request.POST.get('shortname') or not request.POST.get( 'url'): return JsonError('Must provide both shortname and url.') if not link_obj_list.count(): return JsonError('Shortlink not found.') if link_obj_list.count() > 1: return JsonError('Multiple ShortLink instances found.') url = request.POST['url'] shortname = request.POST['shortname'] linkobj = link_obj_list[0] print url if not urlvalid(url): return JsonError( 'Url format incorrect (common mistake: must start with http or https)' ) if linkobj.shortname != shortname and ShortLink.objects.filter( shortname=shortname, user_profile=request.user.profile).count(): return JsonError('Name already in use.') linkobj.shortname = shortname linkobj.url = url linkobj.save() return JsonSuccess('Updated.') except Exception as e: print repr(e) return JsonError('Unknown error.') else: if not link_obj_list.count(): return redirecterror(request, 'Shortlink not found.') if link_obj_list.count() > 1: return redirecterror(request, 'Multiple ShortLink instances found.') linkobj = link_obj_list[0] params = {} params['form'] = ShortLinkForm(instance=linkobj) params['shortlink'] = linkobj params.update(csrf(request)) return xrender(request, 'edit_shortlink.html', params)
def edit_shortlink(request, shortname): link_obj_list = ShortLink.objects.filter(user_profile=request.user.profile, shortname=shortname) if request.method == 'POST': try: if not request.POST.get('shortname') or not request.POST.get('url'): return JsonError('Must provide both shortname and url.') if not link_obj_list.count(): return JsonError('Shortlink not found.') if link_obj_list.count() > 1: return JsonError('Multiple ShortLink instances found.') url = request.POST['url'] shortname = request.POST['shortname'] linkobj = link_obj_list[0] print url if not urlvalid(url): return JsonError('Url format incorrect (common mistake: must start with http or https)') if linkobj.shortname != shortname and ShortLink.objects.filter(shortname=shortname, user_profile=request.user.profile).count(): return JsonError('Name already in use.') linkobj.shortname = shortname linkobj.url = url linkobj.save() return JsonSuccess('Updated.') except Exception as e: print repr(e) return JsonError('Unknown error.') else: if not link_obj_list.count(): return redirecterror(request, 'Shortlink not found.') if link_obj_list.count() > 1: return redirecterror(request, 'Multiple ShortLink instances found.') linkobj = link_obj_list[0] params = {} params['form'] = ShortLinkForm(instance=linkobj) params['shortlink'] = linkobj params.update(csrf(request)) return xrender(request, 'edit_shortlink.html', params)
def manage_monitor_course_page(request): if not request.user.profile.is_contact_set: return redirecterror(request, "You need to set up your cell phone or email first.") params = {} params.update(csrf(request)) return xrender(request, "manage_monitor_course_page.html", params)
def manage_shortlink(request): params = {} params['form'] = ShortLinkForm() params.update(csrf(request)) return xrender(request, 'manage_shortlink.html', params)