def edit_talk(request, scope, id, template_name='talk/edit-talk.html'): """Allows users that submitted a talk to edit it until the talk is approved. """ talk = Talk.objects.get(pk=id) if talk.approved == True: redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'Sorry but you cannot edit the talk once'\ + ' it has been accepted.') if talk.speaker != request.user: redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'Redirected to account because the talk you selected' \ + ' is not your own.') if request.method == 'POST': form = TalkEditForm(data=request.POST) if form.is_valid(): talk.slug = slugify(form.data.get('title')) talk.authors_bio = form.data.get('authors_bio') talk.contact = form.data.get('contact') talk.title = form.data.get('title') talk.abstract = form.data.get('abstract') talk.topic = form.data.get('topic') talk.duration = form.data.get('duration') talk.audience = form.data.get('audience') talk.save() # Saved.. redirect redirect_to = reverse('scipycon_edit_talk', kwargs={ 'scope': scope, 'id': talk.id }) return set_message_cookie(redirect_to, msg=u'Your changes have been saved.') else: form = TalkEditForm( initial={ 'id': id, 'authors_bio': talk.authors_bio, 'contact': talk.contact, 'title': talk.title, 'abstract': talk.abstract, 'topic': talk.topic, 'duration': talk.duration, 'audience': talk.audience, }) context = locals() context['params'] = {'scope': scope} return render_to_response(template_name, RequestContext(request, context))
def edit_talk(request, scope, id, template_name='talk/edit-talk.html'): """Allows users that submitted a talk to edit it until the talk is approved. """ talk = Talk.objects.get(pk=id) if talk.approved == True: redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'Sorry but you cannot edit the talk once'\ + ' it has been accepted.') if talk.speaker != request.user: redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'Redirected to account because the talk you selected' \ + ' is not your own.') if request.method == 'POST': form = TalkEditForm(data=request.POST) if form.is_valid(): talk.slug = slugify(form.data.get('title')) talk.authors_bio = form.data.get('authors_bio') talk.contact = form.data.get('contact') talk.title = form.data.get('title') talk.abstract = form.data.get('abstract') talk.topic = form.data.get('topic') talk.duration = form.data.get('duration') talk.audience = form.data.get('audience') talk.save() # Saved.. redirect redirect_to = reverse('scipycon_edit_talk', kwargs={'scope': scope, 'id': talk.id}) return set_message_cookie(redirect_to, msg = u'Your changes have been saved.') else: form = TalkEditForm(initial={ 'id' : id, 'authors_bio' : talk.authors_bio, 'contact' : talk.contact, 'title' : talk.title, 'abstract' : talk.abstract, 'topic' : talk.topic, 'duration' : talk.duration, 'audience' : talk.audience, }) context = locals() context['params'] = {'scope': scope} return render_to_response(template_name, RequestContext(request, context))
def logout(request, scope): """Custom method to logout a user. The reason to use a custom logout method is just to provide a login and a logoutmethod on one place. """ from django.contrib.auth import logout logout(request) redirect_to = '/%s' % (scope) return set_message_cookie(redirect_to, msg = u"You have been logged out.")
def edit_profile(request, scope, template_name="user/editprofile.html"): """Allows user to edit profile """ user = request.user profile = user.get_profile() if request.method == "POST": form = EditProfileForm(data=request.POST, files=request.FILES) if form.is_valid(): photo = request.FILES.get('photo', None) filename= None if photo: filename = handle_uploaded_photo(user, request.FILES['photo']) if filename: profile.photo = filename user.email = form.data.get("email") user.first_name = form.data.get("first_name") user.last_name = form.data.get("last_name") user.save() profile.url = form.data.get("url") profile.about = form.data.get("about") profile.save() redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'Your profile has been changed.') else: form = EditProfileForm( initial={ 'email' : user.email, 'email2' : user.email, # hidden field 'first_name' : user.first_name, 'last_name' : user.last_name, 'url' : profile.url, 'about' : profile.about, }) return render_to_response(template_name, RequestContext(request, { 'params': {'scope': scope}, 'form': form }))
def password(request, scope, template_name='user/password.html'): """Changes the password of current user. """ if request.method == 'POST': form = PasswordChangeForm(request.user, request.POST) if form.is_valid(): form.save() redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'Your password has been changed.') else: form = PasswordChangeForm(request.user) return render_to_response(template_name, RequestContext(request, { 'params': {'scope': scope}, 'form' : form }))
def username(request, scope, template_name='user/username.html'): """Saves the username from the data form. """ if request.method == 'POST': username_form = UsernameForm( initial={'username' : request.user.username}, data=request.POST) if username_form.is_valid(): request.user.username = username_form.cleaned_data.get("username") request.user.save() redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u"Your username has been changed.") else: username_form = UsernameForm(initial={"username" : request.user.username}) return render_to_response(template_name, RequestContext(request, { 'params': {'scope': scope}, 'form': username_form }))
def submit_talk(request, scope, template_name='talk/submit-talk.html'): """Allows user to edit profile """ scope_entity = Event.objects.get(scope=scope) user = request.user if user.is_authenticated(): try: profile = user.get_profile() except: profile, new = UserProfile.objects.get_or_create( user=user, scope=scope_entity) if new: profile.save() message = None if request.method == 'POST': talk_form = TalkSubmitForm(data=request.POST) register_form = RegisterForm(data=request.POST, files=request.FILES) if request.POST.get('action', None) == 'login': login_form = AuthenticationForm(data=request.POST) if login_form.is_valid(): from django.contrib.auth import login login(request, login_form.get_user()) redirect_to = reverse('scipycon_submit_talk', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'You have been logged in.') if request.POST.get('action', None) == 'register': # add the new user if register_form.is_valid(): user = scipycon_createuser(request, register_form.data) if talk_form.is_valid(): if user.is_authenticated(): title = talk_form.data.get('title') talk = Talk.objects.create( slug = slugify(title), scope = scope_entity, speaker = User.objects.get(pk=user.id), authors_bio = talk_form.data.get('authors_bio'), contact = talk_form.data.get('contact'), title = talk_form.data.get('title'), abstract = talk_form.data.get('abstract'), topic = talk_form.data.get('topic'), duration = talk_form.data.get('duration'), audience = talk_form.data.get('audience'), approved = False, # tags = talk_form.data.get('tags') ) talk.save() # Saved, ... redirect back to account redirect_to = reverse('scipycon_edit_talk', kwargs={'scope': scope, 'id': talk.id}) return set_message_cookie(redirect_to, msg = u'Thanks, your talk has been submitted.') else: redirect_to = reverse('scipycon_submit_talk', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'Something is wrong here.') else: talk_form = TalkSubmitForm() register_form = RegisterForm() login_form = AuthenticationForm() return render_to_response(template_name, RequestContext(request, { 'params': {'scope': scope}, 'talk_form': talk_form, 'register_form' : register_form, 'message' : message, 'login_form' : login_form }))
def edit_registration(request, scope, id, template_name='registration/edit-registration.html'): """Allows users that submitted a registration to edit it. """ scope_entity = Event.objects.get(scope=scope) reg = Registration.objects.get(pk=int(id)) wifi = Wifi.objects.get(user=reg.registrant) # TODO: This is an ugly hack to add accommodation and payment forms # details at later stage for SciPy.in 2010. This must be removed for # SciPy.in 2011 acco, acco_created = Accommodation.objects.get_or_create( user=reg.registrant, scope=scope_entity) payment, payment_created = Payment.objects.get_or_create( user=reg.registrant, scope=scope_entity) if reg.registrant != request.user: redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) return set_message_cookie( redirect_to, msg = u'Redirected because the registration you selected' \ + ' is not your own.') if request.method == 'POST': registration_form = RegistrationEditForm(data=request.POST) wifi_form = WifiForm(data=request.POST) acco_form = AccommodationForm(data=request.POST) payment_form = PaymentForm(data=request.POST) if (registration_form.is_valid() and wifi_form.is_valid() and acco_form.is_valid() and payment_form.is_valid()): reg.organisation = registration_form.data.get('organisation') reg.occupation = registration_form.data.get('occupation') reg.city = registration_form.data.get('city') reg.phone_num = registration_form.data.get('phone_num') reg.postcode = registration_form.data.get('postcode') #reg.tshirt = registration_form.data.get('tshirt') reg.allow_contact = registration_form.data.get( 'allow_contact') and True or False reg.conference = registration_form.data.get( 'conference') and True or False reg.tutorial = registration_form.data.get( 'tutorial') and True or False reg.sprint = registration_form.data.get( 'sprint') and True or False reg.save() wifi = wifi_form.save(reg.registrant, reg.scope) acco = acco_form.save(reg.registrant, reg.scope) payment = payment_form.save(reg.registrant, reg.scope) # Saved.. redirect redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'Your changes have been saved.') else: registration_form = RegistrationEditForm(initial={ 'id' : id, 'organisation' : reg.organisation, 'occupation' : reg.occupation, 'city' : reg.city, 'phone_num': reg.phone_num, #'tshirt' : reg.tshirt, 'conference': reg.conference, 'tutorial': reg.tutorial, 'postcode' : reg.postcode, 'sprint' : reg.sprint, 'allow_contact' : reg.allow_contact, }) wifi_form = WifiForm(initial={ 'user': wifi.user, 'scope': wifi.scope, 'wifi': wifi.wifi, 'registration_id': wifi.registration_id }) acco_form = AccommodationForm(initial={ 'user': acco.user, 'scope': acco.scope, 'sex': acco.sex, 'accommodation_required': acco.accommodation_required, 'accommodation_on_1st': acco.accommodation_on_1st, 'accommodation_on_2nd': acco.accommodation_on_2nd, 'accommodation_on_3rd': acco.accommodation_on_3rd, 'accommodation_on_4th': acco.accommodation_on_4th, 'accommodation_on_5th': acco.accommodation_on_5th, 'accommodation_on_6th': acco.accommodation_on_6th, }) payment_form = PaymentForm(initial={ 'user': payment.user, 'scope': payment.scope, 'paid': payment.type or payment.details, 'type': payment.type, 'details': payment.details, }) return render_to_response( template_name, RequestContext(request, { 'params': {'scope': scope}, 'registration': {'id': id}, 'registration_form': registration_form, 'wifi_form': wifi_form, 'acco_form': acco_form, 'payment_form': payment_form}))
def manage_payments(request, scope, template_name='registration/manage_payments.html'): """View that gives a form to manage payments. """ if not request.user.is_superuser: redirect_to = reverse('scipycon_login', kwargs={'scope': scope}) return set_message_cookie( redirect_to, msg = u'You must be an admin on this website to ' 'access this page.') message = None scope_entity = Event.objects.get(scope=scope) if request.method == 'POST': post_data = request.POST list_user_ids = [] mail_subject = 'SciPy.in 2010: Confirmation of fee payment' mail_template = 'notifications/payment_confirmation2010.html' def parse_form(): """Helper function that gets the User ID from the form name """ confirmed_ids = [] acco_ids = [] date_ids = {} for name_string in post_data: id_str_list = name_string.split('_') if (len(id_str_list) == 3 and id_str_list[1] == 'id'): if id_str_list[0] == 'confirmed': confirmed_ids.append(int(id_str_list[2])) if id_str_list[0] == 'acco': acco_ids.append(int(id_str_list[2])) if id_str_list[0] == 'date': date_str = post_data.get(name_string, None) if date_str: date_ids[int(id_str_list[2])] = post_data.get( name_string, '') return confirmed_ids, acco_ids, date_ids confirmed_ids, acco_ids, date_ids = parse_form() confirmed_users = set(User.objects.filter(id__in=confirmed_ids)) acco_users = set(User.objects.filter(id__in=acco_ids)) # Users for whom both registration and accommodation is confirmed for user in confirmed_users & acco_users: payment, created = user.payment_set.get_or_create( user=user, scope=scope_entity) payment.confirmed = True payment.acco_confirmed = True payment.save() if not payment.confirmed_mail and not payment.acco_confirmed_mail: mail_message = loader.render_to_string( mail_template, dictionary={'name': user.get_full_name(), 'acco': True, 'reg': True}) user.email_user(mail_subject, mail_message, from_email='*****@*****.**') payment.confirmed_mail =True payment.acco_confirmed_mail = True payment.save() # Users for whom only registration is confirmed for user in confirmed_users - acco_users: payment, created = user.payment_set.get_or_create( user=user, scope=scope_entity) payment.confirmed = True payment.save() if not payment.confirmed_mail: mail_message = loader.render_to_string( mail_template, dictionary={'name': user.get_full_name(), 'reg': True}) user.email_user(mail_subject, mail_message, from_email='*****@*****.**') payment.confirmed_mail =True payment.save() # Users for whom only accommodation is confirmed for user in acco_users - confirmed_users: payment, created = user.payment_set.get_or_create( user=user, scope=scope_entity) payment.acco_confirmed = True payment.save() if not payment.acco_confirmed_mail: mail_message = loader.render_to_string( mail_template, dictionary={'name': user.get_full_name(), 'acco': True}) user.email_user(mail_subject, mail_message, from_email='*****@*****.**') payment.acco_confirmed_mail = True payment.save() # Users for whom fee payment date is updated for id in date_ids: user = User.objects.get(id=id) payment, created = user.payment_set.get_or_create( user=user, scope=scope_entity) time_format = "%m/%d/%Y" date = datetime.datetime.fromtimestamp(time.mktime( time.strptime(date_ids[id], time_format))) payment.date_confirmed = date payment.save() registrants = Registration.objects.all() return render_to_response(template_name, RequestContext(request, {'params': {'scope': scope}, 'registrants': registrants, }))
def regstats(request, scope, template_name='registration/regstats.html'): """View that gives the statistics of registrants. """ if not request.user.is_staff: redirect_to = reverse('scipycon_login', kwargs={'scope': scope}) return set_message_cookie( redirect_to, msg = u'You must be a staff on this website to ' 'access this page.') reg_q = Registration.objects.all() conf_num = reg_q.filter(conference=True).count() tut_num = reg_q.filter(tutorial=True).count() sprint_num = reg_q.filter(sprint=True).count() acco_q = Accommodation.objects.all() male = acco_q.filter(sex='Male').count() female = acco_q.filter(sex='Female').count() # Day 1 details day1 = acco_q.filter(accommodation_on_1st=True) acco_1 = { 'total': day1.count(), 'male': day1.filter(sex='Male').count(), 'female': day1.filter(sex='Female').count() } # Day 2 details day2 = acco_q.filter(accommodation_on_2nd=True) acco_2 = { 'total': day2.count(), 'male': day2.filter(sex='Male').count(), 'female': day2.filter(sex='Female').count() } # Day 3 details day3 = acco_q.filter(accommodation_on_3rd=True) acco_3 = { 'total': day3.count(), 'male': day3.filter(sex='Male').count(), 'female': day3.filter(sex='Female').count() } # Day 4 details day4 = acco_q.filter(accommodation_on_4th=True) acco_4 = { 'total': day4.count(), 'male': day4.filter(sex='Male').count(), 'female': day4.filter(sex='Female').count() } # Day 5 details day5 = acco_q.filter(accommodation_on_5th=True) acco_5 = { 'total': day5.count(), 'male': day5.filter(sex='Male').count(), 'female': day5.filter(sex='Female').count() } # Day 6 details day6 = acco_q.filter(accommodation_on_6th=True) acco_6 = { 'total': day6.count(), 'male': day6.filter(sex='Male').count(), 'female': day6.filter(sex='Female').count() } return render_to_response(template_name, RequestContext(request, {'params': {'scope': scope}, 'conf_num': conf_num, 'tut_num': tut_num, 'sprint_num': sprint_num, 'male': male, 'female':female, 'acco_days': [acco_1, acco_2, acco_3, acco_4, acco_5, acco_6], }))
def submit_registration(request, scope, template_name='registration/submit-registration.html'): """Allows user to edit registration """ user = request.user reg_count = Registration.objects.all().count() scope_entity = Event.objects.get(scope=scope) if user.is_authenticated(): try: profile = user.get_profile() except: profile, new = UserProfile.objects.get_or_create( user=user, scope=scope_entity) if new: profile.save() try: registration = Registration.objects.get(registrant=user) if registration: redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) return set_message_cookie( redirect_to, msg = u'You have already been registered.') except ObjectDoesNotExist: pass message = None if request.method == 'POST': registration_form = RegistrationSubmitForm(data=request.POST) registrant_form = RegistrantForm(data=request.POST) wifi_form = WifiForm(data=request.POST) acco_form = AccommodationForm(data=request.POST) payment_form = PaymentForm(data=request.POST) if request.POST.get('action', None) == 'login': login_form = AuthenticationForm(data=request.POST) if login_form.is_valid(): login(request, login_form.get_user()) redirect_to = reverse('scipycon_submit_registration', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'You have been logged in please continue' + \ 'with registration.') newuser = None passwd = None if not user.is_authenticated(): if registrant_form.is_valid(): newuser = scipycon_createregistrant( request, registrant_form.data, scope) # Log in user passwd = User.objects.make_random_password() newuser.set_password(passwd) newuser.save() user = authenticate(username=newuser.username, password=passwd) login(request, user) newuser = user else: newuser = user if (registration_form.is_valid() and newuser and wifi_form.is_valid() and acco_form.is_valid() and payment_form.is_valid()): allow_contact = registration_form.cleaned_data.get( 'allow_contact') and True or False conference = registration_form.cleaned_data.get( 'conference') and True or False tutorial = registration_form.cleaned_data.get('tutorial') and \ True or False sprint = registration_form.cleaned_data.get('sprint') and \ True or False registrant = User.objects.get(pk=newuser.id) reg = Registration( scope=scope_entity, registrant = registrant, organisation = registration_form.cleaned_data.get( 'organisation'), occupation = registration_form.cleaned_data.get('occupation'), city = registration_form.cleaned_data.get('city'), #tshirt = registration_form.data.get('tshirt'), postcode = registration_form.cleaned_data.get('postcode'), phone_num = registration_form.cleaned_data.get('phone_num'), allow_contact = allow_contact, conference = conference, tutorial = tutorial, sprint = sprint) reg.save() # get id and use as slug and invoice number id = reg.id slug = 'SCIPYIN2010%04d' % id reg.slug = slug reg.save() wifi = wifi_form.save(registrant, scope_entity) acco = acco_form.save(registrant, scope_entity) payment = payment_form.save(registrant, scope_entity) send_confirmation(registrant, scope_entity, password=passwd) redirect_to = reverse('scipycon_registrations', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'Thank you, your registration has been submitted '\ 'and an email has been sent with payment details.') else: registration_form = RegistrationSubmitForm() registrant_form = RegistrantForm() wifi_form = WifiForm() acco_form = AccommodationForm() payment_form = PaymentForm() login_form = AuthenticationForm() return render_to_response(template_name, RequestContext(request, { 'params': {'scope': scope}, 'registration_form': registration_form, 'registrant_form' : registrant_form, 'over_reg' : reg_count >= REG_TOTAL and True or False, 'acco_form': acco_form, 'payment_form': payment_form, 'wifi_form' : wifi_form, 'message' : message, 'login_form' : login_form }))
def submit(request, scope, id=None, template='proceedings/submit.html'): """View to submit the proceedings paper. """ user = request.user if user.is_authenticated(): try: profile = user.get_profile() except: profile, new = UserProfile.objects.get_or_create(user=user) if new: profile.save() message = None if request.method == 'POST': register_form = RegisterForm(data=request.POST) if request.POST.get('action', None) == 'login': login_form = AuthenticationForm(data=request.POST) if login_form.is_valid(): login(request, login_form.get_user()) redirect_to = reverse('scipycon_submit_proceedings', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg=u'You have been logged in.') if request.POST.get('action', None) == 'register': # add the new user if register_form.is_valid(): user = scipycon_createuser(request, register_form.data) proceedings_form = ProceedingsForm(data=request.POST, files=request.FILES) if proceedings_form.is_valid(): if user.is_authenticated(): # Data from reSt file is appended to the data in fields title, abstract, body, authors = handleUploadedFile( proceedings_form.cleaned_data, request.FILES.get('file')) paper = edit(id, title=title, abstract=abstract, body=body, authors=authors) if id else create( title=title, abstract=abstract, body=body, authors=authors) # Successfully saved. So get back to the edit page. redirect_to = reverse('scipycon_submit_proceedings', args=[paper.id], kwargs={'scope': scope}) return set_message_cookie( redirect_to, msg=u'Thanks, your paper has been submitted.') else: # This is impossible. Something was wrong so return back # to submit page redirect_to = reverse('scipycon_submit_proceedings', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg=u'Something is wrong here.') else: if id: # If id exists initialize the form with old values paper = Paper.objects.get(id=id) proceedings_form = ProceedingsForm( initial={ 'title': paper.title, 'abstract': paper.abstract, 'body': paper.body, 'authors': ', '.join( [author.username for author in paper.authors.all()]) }) else: # Otherwise create a new form proceedings_form = ProceedingsForm() register_form = RegisterForm() login_form = AuthenticationForm() context = RequestContext( request, { 'proceedings_form': proceedings_form, 'register_form': register_form, 'message': message, 'login_form': login_form }) context['id'] = id if id else None return render_to_response(template, context)
def submit_talk(request, scope, template_name='talk/submit-talk.html'): """Allows user to edit profile """ scope_entity = Event.objects.get(scope=scope) user = request.user if user.is_authenticated(): try: profile = user.get_profile() except: profile, new = UserProfile.objects.get_or_create( user=user, scope=scope_entity) if new: profile.save() message = None if request.method == 'POST': talk_form = TalkSubmitForm(data=request.POST) register_form = RegisterForm(data=request.POST, files=request.FILES) if request.POST.get('action', None) == 'login': login_form = AuthenticationForm(data=request.POST) if login_form.is_valid(): from django.contrib.auth import login login(request, login_form.get_user()) redirect_to = reverse('scipycon_submit_talk', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg=u'You have been logged in.') if request.POST.get('action', None) == 'register': # add the new user if register_form.is_valid(): user = scipycon_createuser(request, register_form.data) if talk_form.is_valid(): if user.is_authenticated(): title = talk_form.data.get('title') talk = Talk.objects.create( slug=slugify(title), scope=scope_entity, speaker=User.objects.get(pk=user.id), authors_bio=talk_form.data.get('authors_bio'), contact=talk_form.data.get('contact'), title=talk_form.data.get('title'), abstract=talk_form.data.get('abstract'), topic=talk_form.data.get('topic'), duration=talk_form.data.get('duration'), audience=talk_form.data.get('audience'), approved=False, # tags = talk_form.data.get('tags') ) talk.save() # Saved, ... redirect back to account redirect_to = reverse('scipycon_edit_talk', kwargs={ 'scope': scope, 'id': talk.id }) return set_message_cookie( redirect_to, msg=u'Thanks, your talk has been submitted.') else: redirect_to = reverse('scipycon_submit_talk', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg=u'Something is wrong here.') else: talk_form = TalkSubmitForm() register_form = RegisterForm() login_form = AuthenticationForm() return render_to_response( template_name, RequestContext( request, { 'params': { 'scope': scope }, 'talk_form': talk_form, 'register_form': register_form, 'message': message, 'login_form': login_form }))
def submit(request, scope, id=None, template='proceedings/submit.html'): """View to submit the proceedings paper. """ user = request.user if user.is_authenticated(): try: profile = user.get_profile() except: profile, new = UserProfile.objects.get_or_create(user=user) if new: profile.save() message = None if request.method == 'POST': register_form = RegisterForm(data=request.POST) if request.POST.get('action', None) == 'login': login_form = AuthenticationForm(data=request.POST) if login_form.is_valid(): login(request, login_form.get_user()) redirect_to = reverse('scipycon_submit_proceedings', kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u'You have been logged in.') if request.POST.get('action', None) == 'register': # add the new user if register_form.is_valid(): user = scipycon_createuser(request, register_form.data) proceedings_form = ProceedingsForm(data=request.POST, files=request.FILES) if proceedings_form.is_valid(): if user.is_authenticated(): # Data from reSt file is appended to the data in fields title, abstract, body, authors = handleUploadedFile( proceedings_form.cleaned_data, request.FILES.get('file')) paper = edit(id, title=title, abstract=abstract, body=body, authors=authors) if id else create(title=title, abstract=abstract, body=body, authors=authors) # Successfully saved. So get back to the edit page. redirect_to = reverse('scipycon_submit_proceedings', args=[paper.id], kwargs={'scope': scope}) return set_message_cookie( redirect_to, msg = u'Thanks, your paper has been submitted.') else: # This is impossible. Something was wrong so return back # to submit page redirect_to = reverse('scipycon_submit_proceedings', kwargs={'scope': scope}) return set_message_cookie( redirect_to, msg = u'Something is wrong here.') else: if id: # If id exists initialize the form with old values paper = Paper.objects.get(id=id) proceedings_form = ProceedingsForm( initial={'title': paper.title, 'abstract': paper.abstract, 'body': paper.body, 'authors': ', '.join([ author.username for author in paper.authors.all()]) }) else: # Otherwise create a new form proceedings_form = ProceedingsForm() register_form = RegisterForm() login_form = AuthenticationForm() context = RequestContext(request, { 'proceedings_form': proceedings_form, 'register_form' : register_form, 'message' : message, 'login_form' : login_form }) context['id'] = id if id else None return render_to_response(template, context)
def login(request, scope, template_name="user/login.html"): """Custom view to login or register/login a user. Integration of register and login form It uses Django's standard AuthenticationForm, though. """ user = request.user if user.is_authenticated(): redirect_to = reverse("scipycon_account", kwargs={'scope': scope}) return set_message_cookie(redirect_to, msg = u"Redirected to account from login form.") # Using Djangos default AuthenticationForm login_form = AuthenticationForm() register_form = RegisterForm() if request.POST.get("action") == "login": login_form = AuthenticationForm(data=request.POST) if login_form.is_valid(): redirect_to = request.POST.get("next") # Light security check -- make sure redirect_to isn't garbage. if not redirect_to or '//' in redirect_to or ' ' in redirect_to: redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) from django.contrib.auth import login login(request, login_form.get_user()) return set_message_cookie(redirect_to, msg = u"You have been logged in.") elif request.POST.get("action") == "register": register_form = RegisterForm(data=request.POST) if register_form.is_valid(): user = scipycon_createuser(request, register_form.data, scope) redirect_to = request.POST.get("next") if not redirect_to or '//' in redirect_to or ' ' in redirect_to: redirect_to = reverse('scipycon_account', kwargs={'scope': scope}) return set_message_cookie( redirect_to, msg = u"You have been registered and logged in.") # Get next_url next_url = request.REQUEST.get("next") if next_url is None: next_url = request.META.get("HTTP_REFERER") if next_url is None: next_url = reverse('scipycon_account', kwargs={'scope': scope}) # Get just the path of the url. # See django.contrib.auth.views.login for more next_url = urlparse(next_url) next_url = next_url[2] try: login_form_errors = login_form.errors["__all__"] except KeyError: login_form_errors = None return render_to_response(template_name, RequestContext(request, { 'params': {'scope': scope}, 'login_form' : login_form, 'login_form_errors' : login_form_errors, 'register_form' : register_form, 'next_url' : next_url, }))