def login_forwarding(request, log_out=False): """ login requested """ if not valid_token(request): return token_splash_page(request) extra = {} extra.update({'title' : _('Login')}) if "savedphrase" in extra: del extra["savedphrase"] if "just_logged_out" in extra: del extra["just_logged_out"] if "captchaerror" in extra: del extra["captchaerror"] if log_out: extra.update({'title' : _('You have been logged out.')}) extra.update({'just_logged_out' : True }) if request.method == "POST": # Split loginphrase in username and password. Also adds a flag to the # session so we can see which type of login was used. request.POST = loginphrase2username_password(request) if "captcha" in extra: del extra["captcha"] return django_login(request, extra_context=extra)
def delete_forwarding(request): """ account deletion requested """ if not valid_token(request): return token_splash_page(request) return delete(request)
def delete_confirm_forwarding(request): """ forwarding on delete confirm request """ if not valid_token(request): return token_splash_page(request) return delete_confirm(request)
def register(request): """Allows someone to register for the service""" if not valid_token(request): return token_splash_page(request) template_vars = {} template_path = 'registration/register.html' set_register_vars(request, template_vars) return render_to_response(template_path, template_vars, context_instance=RequestContext(request))
def faq(request): """show faq page""" if not valid_token(request): return token_splash_page(request) template_path = 'content/faq.html' template_vars = {} return render_to_response( template_path, template_vars, context_instance=RequestContext(request) )
def get_new_captcha(request): """get new captcha image""" if not valid_token(request): return token_splash_page(request) invalidate_captcha(request) form = CaptchaTestForm() new_form = "" for field in form: new_form += str(field) return HttpResponse (new_form)
def logout_forwarding(request): """ logout requested """ if not valid_token(request): return token_splash_page(request) log_out = request.user.is_authenticated() if request.user.is_authenticated(): auth_logout(request) return login_forwarding(request, log_out)
def membership(request): """show info about project membership""" if not valid_token(request): return token_splash_page(request) template_path = 'content/membership.html' template_vars = {} return render_to_response( template_path, template_vars, context_instance=RequestContext(request) )
def select_lang(request): """select language request""" if not valid_token(request): return token_splash_page(request) template_vars = {} template_vars['title'] = __('Select language') template_vars['previouspage'] = get_redirect_url(request) template_path = 'i18n/setlang.html' return render_to_response(template_path, template_vars, context_instance=RequestContext(request))
def no_authenticated_user(request): """ This page is shown if a user logs in but his Profile is not finished with being encrypted. """ if not valid_token(request): return token_splash_page(request) template_path = 'not_yet_encrypted.html' template_vars = {} return render_to_response(template_path, template_vars, context_instance=RequestContext(request))
def new_submit(request): """ A new user submits their registration here """ if not valid_token(request): return token_splash_page(request) if request.method == "POST": tpl_path, tpl_vars, _ = new_user(request, True) return render_to_response( tpl_path, tpl_vars, context_instance=RequestContext(request) ) else: return register(request)
def delete(request, password_error=False): """ account deletion """ if not valid_token(request): return token_splash_page(request) if request.method == "GET" or password_error: template_path = 'registration/delete.html' template_vars = {} template_vars['title'] = _('Delete Profile') template_vars['passworderror'] = password_error return render_to_response(template_path, template_vars, context_instance=RequestContext(request)) else: return delete_confirm(request)
def i18n_forwarding(request): """select display language""" if not valid_token(request): return token_splash_page(request) if request.user.is_authenticated(): user_profile = request.user.get_profile() restore_profile_from_session(request, user_profile) old_lang = user_profile.display_language lang_code = request.POST.get('language', None) if lang_code and check_for_language(lang_code): user_profile.display_language = lang_code if old_lang != user_profile.display_language: user_profile.notify_mailmanager ( user_profile.email,user_profile.email ) user_profile.save() request.session["display_language"] = user_profile.display_language return i18n.set_language(request)
def validate_captcha(request): """validate captcha""" if not valid_token(request): return token_splash_page(request) # extract captcha data from request test = deepcopy(request.POST) cid = request.GET.get("captchaid", "") ctext = request.GET.get("captchatext", "") test.__setitem__('captcha_0', cid) test.__setitem__('captcha_1', ctext) captcha_form = CaptchaTestForm (test) if not captcha_form.is_valid(): invalidate_captcha(request) return HttpResponse("fail") else: save_valid_captcha(request) return HttpResponse("win")
def home(request): """show home page""" if not valid_token(request): return token_splash_page(request) # copied from i18n - required to give users their language # after entering a token if request.method == 'POST': next_page = request.REQUEST.get('next', None) if not next_page: next_page = request.META.get('HTTP_REFERER', None) if not next_page: next_page = '/' response = http.HttpResponseRedirect(next_page) # give users the token which they have just entered as a cookie if not has_valid_token_cookie(request): response.set_cookie( "prj_token", value=request.POST.get('token', None), secure = settings.SESSION_COOKIE_SECURE, httponly = settings.SESSION_COOKIE_HTTPONLY) lang_code = request.POST.get('language', None) if lang_code and check_for_language(lang_code): if hasattr(request, 'session'): request.session['django_language'] = lang_code else: response.set_cookie( settings.LANGUAGE_COOKIE_NAME, value=lang_code, secure = settings.SESSION_COOKIE_SECURE, httponly = settings.SESSION_COOKIE_HTTPONLY) else: template_path = 'content/index.html' template_vars = {'title' : _('Welcome to STIGMERGY!'), 'quote' : True} response = render_to_response( template_path, template_vars, context_instance=RequestContext(request) ) return response
def delete_confirm(request): """ confirm a delete request """ if not valid_token(request): return token_splash_page(request) if request.POST.has_key('yes'): login_phrase = request.POST.get("password","") password = get_password_from_loginphrase(login_phrase) if request.user.check_password(password): template_path = 'registration/delete_confirm.html' template_vars = {} template_vars['title'] = _('Profile Deleted') user = request.user auth_logout(request) user.prj_delete() return render_to_response(template_path, template_vars, context_instance=RequestContext(request)) else: return delete(request, password_error=_("Wrong password.")) else: return redirect('/')
def user_forwarding(request, friend_id): """friend profile request""" if not valid_token(request): return token_splash_page(request) return user(request, friend_id)
def profile_forwarding(request): """Own profile pages""" if not valid_token(request): return token_splash_page(request) return profile(request)
def user(request, friend_id): """show friend profile""" if not valid_token(request): return token_splash_page(request) template_vars = {} user_ = request.user profile_ = user.get_profile() restore_profile_from_session(request, profile_) local_friends = profile_.friend_list(request.session, friend_type="local") remote_friends = profile_.friend_list(request.session, friend_type="remote") template_vars['user'] = user_ template_vars['profile'] = profile_ template_vars['localFriends'] = local_friends template_vars['remoteFriends'] = remote_friends template_vars['friend_id'] = friend_id found = False friend = None for frnd in local_friends: if frnd.profile.id == friend_id: template_vars['friend'] = frnd friend = frnd found = True friend_type = "local" break if not found: for i, frnd in enumerate(remote_friends): if frnd.profile.id == friend_id: template_vars['match_lang'] = profile.get_global_token(i) template_vars['friend'] = frnd friend = frnd found = True friend_type = "global" break if not found and friend_id == profile.id: class XFriend(object): """temporary friend class""" def __init__(self, prf): """init xfriend instance""" self.profile = prf me_as_friend = XFriend(profile_) template_vars['ownPage'] = True template_vars['friend'] = me_as_friend friend_type = choice(["global", "local"]) friend = me_as_friend found = True if found: template_vars['uniqueLanguages'] = friend.profile.getUniqueLanguages() template_vars['ownUniqueLanguages'] = profile_.getUniqueLanguages() template_path = 'user/user_found.html' template_vars['title'] = __('Friend') + ' ' + \ friend.profile.human_public_name() template_vars['use_maps_single'] = True template_vars['friendType'] = friend_type template_vars['cityDict'] = { 'latitude' : friend.profile.latitude, 'longitude' : friend.profile.longitude, } else: template_vars['title'] = \ __('Friend %(friend_id)s not found') % {'friend_id' : friend_id} template_path = 'user/user_not_found.html' return render_to_response( template_path, template_vars, context_instance=RequestContext(request) )
def drop_friend_forwarding(request): """drop request""" if not valid_token(request): return token_splash_page(request) return drop_friend(request)
def drop_friend(request): """drop contact""" if not valid_token(request): return token_splash_page(request)
def new_login(request): """ Careful - this function does not use django's login/logout functions intelligently. Bugs are likely to arise from here. """ if not valid_token(request): return token_splash_page(request) if match(settings.ID_REGEX, request.user.username): # the user already has the new passphrase. Don't let him change it. return HttpResponseRedirect(reverse('user.views.profileForwarding')) template_path = 'registration/new_login.html' template_vars = {} template_vars['title'] = _('IMPORTANT') # Restore the user's session. If he tries to visit any other page, # he will remain logged out. if "savedSession" in request.session: for key, value in request.session["savedSession"].items(): request.session[key] = value if SESSION_KEY in request.session: request.user = User.objects.get(id=request.session[SESSION_KEY]) del request.session["savedSession"] user = request.user # Don't let people visit this page without logging in. # Copied from django.contrib.auth.decorators.user_passes_test if not user.is_authenticated(): login_url = None path = request.build_absolute_uri() # If the login url is the same scheme and net location then just # use the path as the "next" url. login_scheme, login_netloc = urlparse.urlparse(login_url or settings.LOGIN_URL)[:2] current_scheme, current_netloc = urlparse.urlparse(path)[:2] if ((not login_scheme or login_scheme == current_scheme) and (not login_netloc or login_netloc == current_netloc)): path = request.get_full_path() return redirect_to_login(path, login_url, REDIRECT_FIELD_NAME) user_profile = user.get_profile() if request.method == "POST": loginphrase_entered = request.POST.get("loginphrase","").upper() loginphrase = request.session.get("temploginphrase","") if loginphrase_entered == loginphrase: # The user received the passphrase and has hopefully written it # down. Because now we are changing his credentials. user.username = user_profile.id password = get_password_from_loginphrase(loginphrase) encrypt_unencrypted_profile(user_profile, password) user.email = user_profile.get_anonymous_email() user.set_password(password) user.save() populate_session(request, password) return HttpResponseRedirect( reverse('user.views.profileForwarding') ) loginphrase = user_profile.generateNewLoginPhrase() template_vars['loginphrase'] = loginphrase request.session['temploginphrase'] = loginphrase # Delete the user's session: Stop the user from going to his profile pages # before he has entered the passphrase. saved_session = {} for key, value in request.session._session.items(): saved_session[key] = value auth_logout(request) request.session["savedSession"] = saved_session template_vars['savedSession'] = saved_session return render_to_response(template_path, template_vars, context_instance=RequestContext(request))
def edit(request, clicked): """edit profile""" if not valid_token(request): return token_splash_page(request) template_vars = {} if request.method == "POST": loginphrase = request.POST.get("password","") password = get_password_from_loginphrase(loginphrase) if request.user.check_password(password): if edit_captcha_ok(request): validation_errors = contactinfo_errors(request) or \ aboutme_errors(request) or \ location_errors(request) or \ language_errors(request) or \ email_errors(request) or None if validation_errors: validation_errors = ([contactinfo_errors(request)] + \ [aboutme_errors(request)] + \ [location_errors(request)] + \ [language_errors(request)] + \ [email_errors(request)]) for val_error in validation_errors: if val_error is not None: template_vars[val_error[-1]] = val_error[2] else: save_edited_info(request) # Get the newly encrypted data into the session again populate_session(request, password) return HttpResponseRedirect( reverse('user.views.profile_forwarding') ) else: template_vars["captchaerror"] = True template_vars["captcha"] = CaptchaTestForm(request.POST) else: template_vars["passworderror"] = __("Wrong passphrase.") else: if edit_needs_captcha(request): template_vars["captcha"] = CaptchaTestForm() template_vars['contact_info'] = request.POST.get("contact_info", False) template_vars['about_me'] = request.POST.get("about_me", False) template_vars['email'] = request.POST.get("email", False) template_vars['user'] = request.user profile_ = request.user.get_profile() restore_profile_from_session(request, profile_) template_vars['profile'] = profile_ for num, lang in enumerate(profile_.get_langs_spoken()): template_vars['lang_'+str(num)] = \ request.POST.get("lang_" + str(num), lang) template_vars['use_maps_single'] = True template_vars['cityDict'] = { 'latitude' : request.POST.get("latitude", profile_.latitude), 'longitude' : request.POST.get("longitude", profile_.longitude), } template_vars["clicked"] = clicked template_vars['title'] = __('Edit the information about you') template_path = "user/edit_profile.html" return render_to_response(template_path, template_vars, context_instance=RequestContext(request))
def profile(request): """show profile""" if not valid_token(request): return token_splash_page(request) user_ = request.user profile_ = user_.get_profile() restore_profile_from_session(request, profile_) # show the page in the user's language response = display_in_native_language(request, profile_) if response: return response template_path = 'user/profile.html' template_vars = {} template_vars['title'] = __('My Profile') if request.method == "POST": if settings.VERBOSE: print "POST on the profile. this is a Drop-attempt" password = request.POST.get("loginphrase", "") dropped_fid = request.POST.get('dropped_fid', None) reason = request.POST.get('reason', "") comment = request.POST.get('comment', "") reason_map = { "unresponsive":"nice", "differences":"middle", "rude":"bad", } if settings.VERBOSE: print "Trying to drop %s for reason %s with comment\n%s" \ % (dropped_fid, reason, comment) if user.check_password(password): if dropped_fid is not None and reason in reason_map: reason = reason_map[reason] dropped = profile_.defriend(dropped_fid, reason, comment) if dropped: populate_session(request, password) if dropped_fid in request.session: del request.session[dropped_fid] template_vars['droppedFriend'] = dropped_fid template_vars['message'] = \ __('Your contact has been dropped. ' 'We will find you another one soon.') if settings.VERBOSE: print "Drop successful." elif settings.VERBOSE: print "Drop failed: Wrong password" template_vars['user'] = user_ template_vars['profile'] = profile_ template_vars['localFriends'] = \ profile_.friend_list(request.session, friend_type="local") + \ request.session.get("display_local", []) template_vars['remoteFriends'] = \ profile_.friend_list(request.session, friend_type="remote") + \ request.session.get("display_remote", []) template_vars['ownUniqueLanguages'] = \ set(profile_.get_unique_languages()) template_vars['newFriends'] = request.session.get("new_friends", []) template_vars['use_maps_multiple'] = True if request.session.get("changes_saved", False): template_vars['success_message'] = __("Changes saved.") del request.session['changes_saved'] if settings.VERBOSE: print "============SESSION DUMP============" for key, value in request.session.items(): print key, smart_str(value) return render_to_response( template_path, template_vars, context_instance=RequestContext(request) )
def edit_forwarding(request, clicked): """edit profile request""" if not valid_token(request): return token_splash_page(request) return edit(request, clicked)