def course_add(request): """ FIXME: merge this and one below into two """ manipulator = Course.AddManipulator() if request.method == 'POST': # If data was POSTed, we're trying to create a new Place. new_data = request.POST.copy() # Check for errors. errors = manipulator.get_validation_errors(new_data) if not errors: # No errors. This means we can save the data! manipulator.do_html2python(new_data) new_course = manipulator.save(new_data) # Redirect to the object's "edit" page. Always use a redirect # after POST data, so that reloads don't accidently create # duplicate entires, and so users don't see the confusing # "Repost POST data?" alert box in their browsers. return HttpResponseRedirect("/score/course/%i/" % new_course.id) else: # No POST, so we want a brand new form without any data or errors. errors = new_data = {} # Create the FormWrapper, template, context, response. form = forms.FormWrapper(manipulator, new_data, errors) return render_to_response('scores/course_form.html', {'form': form})
def change_details(request): ''' Change details of existing users ''' manipulator = ChangeDetails(request.user.id) if request.method == 'POST': new_data = request.POST.copy() print 'By post', new_data errors = manipulator.get_validation_errors(new_data) if not errors: manipulator.do_html2python(new_data) new_user = manipulator.save(new_data) return render_to_response( 'simple_message.html', {'message': 'Your Details have been updated'}, RequestContext(request)) else: print 'Errors at change_details :', errors else: errors = {} new_data = manipulator.flatten_data() print new_data form = forms.FormWrapper(manipulator, new_data, errors) return render_to_response('change_profile.html', {'form': form}, RequestContext(request)) return render_to_response('register.html', {'form': form}, RequestContext(request))
def change_password(request): ''' Change Pasword View. Need I say more? ''' manipulator = ChangePassword() if request.POST: new_data = request.POST.copy() errors = manipulator.get_validation_errors(new_data) if not errors: manipulator.do_html2python(new_data) user = request.user if user.check_password(new_data['old_password']): user.set_password(new_data['password1']) user.save() return render_to_response('simple_message.html', {'message': 'Password Changed!'}, RequestContext(request)) else: errors['old_password'].append('Old Password is incorrect') else: print errors else: errors = new_data = {} form = forms.FormWrapper(manipulator, new_data, errors) return render_to_response('change_password.html', {'form': form}, RequestContext(request))
def main(request): shares = smartbox.samba.getShares() users = smartbox.core.getUsers() manipulator = manipulators.PermissionsManipulator(users, shares) shares = [shares[shareName] for shareName in shares] users = [users[username] for username in users] if request.POST: # Check for delete button. debugOutput(request.POST) new_data = request.POST.copy() errors = manipulator.get_validation_errors(new_data) if not errors: manipulator.do_html2python(new_data) manipulator.save(new_data) return HttpResponseRedirect("/") else: new_data = manipulator.flatten_data() errors = {} form = forms.FormWrapper(manipulator, new_data, errors) context = { "form": form, "users": users, "shares": shares, } return render_to_response("users.html", RequestContext(request, context))
def register(request): ''' creates an inactive account by using the manipulator for the non-existant user and sends a confirm link to the user''' if request.user.is_authenticated(): # They already have an account; don't let them register again return render_to_response('simple_message.html', { 'message': 'You are already registered.', }, RequestContext(request)) manipulator = RegistrationForm() if request.POST: new_data = request.POST.copy() #TODO: Clean up the user objects (at some point of time) to release accounts which were never activated errors = manipulator.get_validation_errors(new_data) if not errors: # Save the user manipulator.do_html2python(new_data) new_user = manipulator.save(new_data) # Build the activation key for their account salt = sha.new(str(random.random())).hexdigest()[:5] activation_key = sha.new(salt + new_user.username).hexdigest() key_expires = datetime.datetime.today() + datetime.timedelta(2) # Create and save their profile new_profile = UserProfile( user=new_user, activation_key=activation_key, key_expires=key_expires, ) new_profile.save() # Send an email with the confirmation link # TODO: Store this message in a template email_subject = 'Your new Hackzor account confirmation' email_body = ('Hello, %s, and thanks for signing up for an %s ' % (request.user.username, settings.CONTEST_NAME) + 'account!\n\nTo activate your account, click this' + 'link within 48 hours:\n\n ' + 'http://%s/accounts/confirm/%s' % (settings.CONTEST_URL, new_profile.activation_key)) send_mail(email_subject, email_body, settings.CONTEST_EMAIL, [new_user.email]) return render_to_response( 'simple_message.html', { 'message': 'A mail has been sent to ' + '%s. Follow the link in the mail to ' % (new_user.email) + 'activate your account', }, RequestContext(request)) #'user' : request.user}) else: print 'Errors' else: errors = new_data = {} form = forms.FormWrapper(manipulator, new_data, errors) return render_to_response('register.html', {'form': form}, RequestContext(request))
def activate_account(request): ''' creates an inactive account by using the manipulator for the non-existant user and sends a confirm link to the user''' if request.user.is_authenticated(): # They already have an account; don't let them register again return render_to_response('simple_message.html', { 'message': 'You are already logged in.', }, RequestContext(request)) manipulator = ActivateAccount() if request.POST: new_data = request.POST.copy() errors = manipulator.get_validation_errors(new_data) if not errors: # Save the user user = manipulator.get_user(new_data) # Build the activation key for their account salt = sha.new(str(random.random())).hexdigest()[:5] activation_key = sha.new(salt + user.username).hexdigest() key_expires = datetime.datetime.today() + datetime.timedelta(2) userprofile = user.userprofile userprofile.activation_key = activation_key userprofile.key_expires = key_expires userprofile.save() # Send an email with the confirmation link # TODO: Store the message in a seperate file or DB email_subject = 'Your new %s account confirmation' % ( settings.CONTEST_NAME) email_body = ( 'Hello %s,\n ' % (user.username) + 'You have requested for an activation mail at %s. You can activate your account by following the ' % (settings.CONTEST_NAME) + 'link below within 48 hours.\n\n ' + 'http://%s/accounts/confirm/%s \n' % (settings.CONTEST_URL, user.userprofile.activation_key) + '\n Best of luck for %s!\n\nRegards,\n%s Team' % (settings.CONTEST_NAME, settings.CONTEST_NAME)) send_mail(email_subject, email_body, settings.CONTEST_EMAIL, [user.email]) return render_to_response( 'simple_message.html', { 'message': 'A mail has been sent to ' + '%s. Follow the link in the mail to ' % (user.email) + 'activate your account<br />' + '<span class="bold">Note :</span>If you think your confirmation mail has not arrived, please check your Spam/Bulk' + ' before contacting us.', }, RequestContext(request)) #'user' : request.user}) else: print 'Errors' else: errors = new_data = {} form = forms.FormWrapper(manipulator, new_data, errors) return render_to_response('activate.html', {'form': form}, RequestContext(request))
def register(request): form = UserCreationForm() if request.method == 'POST': data = request.POST.copy() errors = form.get_validation_errors(data) if not errors: new_user = form.save(data) return HttpResponseRedirect("/set_rdi/") else: data, errors = {}, {} return render_to_response("registration/register.html", {'form': forms.FormWrapper(form, data, errors)})
def forgot_password(request): ''' Sends mail to user on reset passwords ''' if request.user.is_authenticated(): return render_to_response( 'simple_message.html', { 'message': 'You already know your password. Use Change password to change your existing password' }, RequestContext(request)) manipulator = ForgotPassword() if request.POST: import md5 new_data = request.POST.copy() errors = manipulator.get_validation_errors(new_data) if not errors: manipulator.do_html2python(new_data) # Build the activation key, for the reset password salt = sha.new(str(random.random())).hexdigest()[:5] new_password = md5.md5(str(random.random())).hexdigest()[:8] username = new_data['username'] # Create and save their profile u = User.objects.get(username=username) u.set_password(new_password) u.save() # Send an email with the password email_subject = 'Your new Hackzor account password reset' email_body = 'Hello, %s. A password reset was requested at %s. Your new_password is %s.' % ( request.user.username, settings.CONTEST_URL, new_password) send_mail(email_subject, email_body, settings.CONTEST_EMAIL, [u.email]) return render_to_response( 'simple_message.html', { 'message': 'A mail has been sent to %s with the new password' % (u.email) }, RequestContext(request)) else: print 'Errors' else: errors = new_data = {} form = forms.FormWrapper(manipulator, new_data, errors) return render_to_response('reset_password.html', {'form': form}, RequestContext(request))
def submit_code(request, problem_no=None): ''' Handles Submitting problem. Gets User identity from sessions. requires an authenticated user problem_no : The primary key(id) of the problem for which the code is being submitted ''' manipulator = SubmitSolution() if request.POST: new_data = request.POST.copy() new_data.update(request.FILES) errors = manipulator.get_validation_errors(new_data) if not errors: manipulator.do_html2python(new_data) content = request.FILES['file_path']['content'] question = get_object_or_404(Question, id=new_data['question_id']) if len(content) > question.source_limit: return render_to_response( 'simple_message.html', {'message': 'Source Limit Exceeded. Code was NOT saved.'}, RequestContext(request)) user = get_object_or_404(UserProfile, user=request.user) language = get_object_or_404(Language, id=new_data['language_id']) #TODO: Make all this a decorator and apply it only if defined in settings if question in [a.question for a in user.solved.all() ]: #If the user has already solved this problem return render_to_response( 'simple_message.html', { 'message': 'You have already solved this problem. The current submission is ignored' }, RequestContext(request)) if user.attempt_set.filter( question=question).count() > question.submission_limit: return render_to_response( 'simple_message.html', { 'message': 'You have exceeded your submission limit for this problem. The current submission is ignored' }, RequestContext(request)) attempt = Attempt(user=user, question=question, code=content, language=language, file_name=request.FILES['file_path']['filename']) attempt.error_status = 'Being Evaluated' attempt.result = False attempt.save() pending = ToBeEvaluated(attempt=attempt) pending.save() return render_to_response('simple_message.html', {'message': 'Code Submitted!'}, RequestContext(request)) else: print 'Errors at submit_code: ', errors else: errors = new_data = {} if problem_no: new_data = MultiValueDict({'question_id': [problem_no]}) form = forms.FormWrapper(manipulator, new_data, errors) return render_to_response('submit_code.html', {'form': form}, RequestContext(request))
if request.POST: # Check for delete button. debugOutput(request.POST) new_data = request.POST.copy() errors = manipulator.get_validation_errors(new_data) if not errors: manipulator.do_html2python(new_data) manipulator.save(new_data) return HttpResponseRedirect("/") else: new_data = manipulator.flatten_data() errors = {} form = forms.FormWrapper(manipulator, new_data, errors) context = { "share": share, "form": form, } return render_to_response("share.html", RequestContext(request, context)) def confirm(request, variable, message, redirect=None): confirmVariable = "%s-%s" % ("confirm", variable or "n") cancelVariable = "%s-%s" % ("cancel", variable or "n") if cancelVariable in request.POST: debugOutput("Canceling request")