def recover_password_confirm(request, user_id, hash): context = { 'TITLE': 'Recover Password', } try: password_hash = LostPasswordHash.objects.get(user=user_id, hash=hash) if not password_hash.is_valid: password_hash.delete() raise LostPasswordHash.DoesNotExist user = password_hash.user except LostPasswordHash.DoesNotExist: tpl = 'accounts/recover/failure.html' else: tpl = 'accounts/recover/confirm.html' if request.POST: form = ChangePasswordRecoverForm(request.POST) if form.is_valid(): user.set_password(form.cleaned_data['password']) user.save() # Ugly way of doing this, but Django requires the backend be set user = authenticate( username=user.username, password=form.cleaned_data['password'], ) login(request, user) password_hash.delete() return HttpResponseRedirect(url('accounts')) else: form = ChangePasswordRecoverForm() context['form'] = form return render_to_response(tpl, context, request)
def view_paste(request, id, syntax=None): try: paste = Paste.objects.get(pk=id) except Paste.DoesNotExist: raise Http404("Paste not found") if paste.status < 0 and not (request.user.is_superuser or request.user == paste.author): raise Http404("Paste not found") context = { 'PAGE': 'view', 'paste': paste, } if paste.type == PASTE_TYPE_TEXT: if syntax: try: syntax = Syntax.objects.get(slug=syntax) except Syntax.DoesNotExist: return HttpResponseRedirect(paste.get_absolute_url()) else: syntax = paste.syntax context['parsed'], context['css'] = paste.get_parsed(syntax) context['syntax'] = syntax context['syntax_list'] = Syntax.objects.all().order_by('name') return render_to_response('pastes/view.html', context, request)
def show_login(request): # TODO: clean this logic up default_url = url('accounts') next_url = request.build_absolute_uri(get_next_url(request, default_url)) login_url = request.build_absolute_uri(url('accounts.login')) register_url = request.build_absolute_uri(url('accounts.register')) if next_url.startswith(login_url) or next_url.startswith(register_url): next_url = default_url if request.user.is_authenticated(): return HttpResponseRedirect(next_url) if request.path.startswith(url('accounts.register')): active_form = 'register' else: active_form = 'login' context = handle_login(request) if context['logged_in']: return HttpResponseRedirect(next_url) context.update({ 'active_form': active_form, 'next_url': next_url, 'TITLE': 'Login or Register', }) return render_to_response('accounts/login.html', context, request)
def recent_pastes(request): paste_list = Paste.objects.filter(status=1, group=request.group)\ .select_related('author').order_by('-post_date') context = { 'PAGE': 'recent', 'paste_list': paste_list, } return render_to_response('pastes/recent.html', context, request)
def show_dashboard(request): # XXX: Lazy hack for now from pastethat.pastes.templatetags.pastes import * paste_list = get_recent_pastes(request.user) css, parsed = get_parser_summary_cache(paste_list) TITLE = 'Your Account' context = locals() return render_to_response('accounts/index.html', context, request)
def view_children(request, id): try: paste = Paste.objects.get(pk=id) except Paste.DoesNotExist: raise Http404("Paste not found") child_list = paste.paste_set.all().order_by('-post_date') context = { 'PAGE': 'children', 'paste': paste, 'child_list': child_list, } return render_to_response('pastes/children.html', context, request)
def show_change_password(request): if request.POST: if request.POST.get('save'): form = ChangePasswordForm(request.user, request.POST) if form.is_valid(): request.user.set_password(form.cleaned_data['password']) request.user.save() return HttpResponseRedirect(url('accounts.password') + '?success=1') elif request.POST.get('cancel'): return HttpResponseRedirect(url('accounts')) else: form = ChangePasswordForm(request.user) context = { 'form': form, 'TITLE': 'Change Password', } return render_to_response('accounts/change_password.html', context, request)
def show_settings(request): if request.POST: if request.POST.get('save'): form = SettingsForm(request.POST, instance=request.user) if form.is_valid(): try: User.objects.exclude(pk=request.user.id).get(email=form.cleaned_data['email']) except User.DoesNotExist: pass else: form.errors['email'] = 'That email address is already registered with another account.' if form.is_valid(): form.commit() return HttpResponseRedirect(url('accounts.settings') + '?success=1') else: form = SettingsForm(instance=request.user) context = { 'form': form, 'TITLE': 'Settings', } return render_to_response('accounts/settings.html', context, request)
def recover_password(request): if request.POST: form = RecoverPasswordForm(request.POST) if form.is_valid(): password_hash, created = LostPasswordHash.objects.get_or_create( user=form.cleaned_data['email'] ) if not password_hash.is_valid: created = True password_hash.date_added = datetime.datetime.now() password_hash.set_hash() if not created: form.errors['__all__'] = 'A password reset was already attempted for this account within the last 24 hours.' if form.is_valid(): context = context_processors.default(request) context.update({ 'user': password_hash.user, 'url': request.build_absolute_uri(password_hash.get_absolute_url()), }) data = render_to_string('accounts/recover/emails/recover.txt', context) send_mail('[PasteThat] Password Recovery', data, settings.EMAIL_FROM_ADDRESS, [password_hash.user.email], fail_silently=True) form = RecoverPasswordForm() else: form = RecoverPasswordForm() breadcrumbs = ( ('Account', url('accounts')), ('Recover Password', url('accounts.password.recover')), ) context = { 'form': form, 'TITLE': 'Recover Password', 'BREADCRUMBS': breadcrumbs, } return render_to_response('accounts/recover/index.html', context, request)
else: PAGE = 'new' title = 'New Paste' form_url = url('pastes.new') context = { 'PAGE': PAGE, 'title': title, 'paste': paste, 'form_url': form_url, 'text_form': text_form, 'file_form': file_form, 'link_form': link_form, } return render_to_response('pastes/new.html', context, request) def download_paste(request, id): try: paste = Paste.objects.get(pk=id) except Paste.DoesNotExist: raise Http404("Paste not found") if not paste.file and paste.type == PASTE_TYPE_TEXT: response = HttpResponse(paste.text) response['Content-Type'] = 'text/plain' return response return HttpResponseRedirect(paste.file.url) f = open(paste.file.path, 'rb')
def show_pastes(request): context = { 'paste_list': request.user.paste_set.all().order_by('-post_date'), 'TITLE': 'Your Pastes', } return render_to_response('accounts/pastes.html', context, request)