def settings(request): success = False if request.method == 'POST': form = UserSettingsForm(request.POST, request.FILES, instance=request.user) old_pw_hash = request.user.password if form.is_valid(): u = form.save(commit=False) if u.password: u.password = hash_password(u.password) else: u.password = old_pw_hash if form.cleaned_data['profile_pic']: u.upload_profile_pic(form.cleaned_data['profile_pic']) if form.cleaned_data['location_data']: u.location = Location.get_or_create(form.cleaned_data['location_data']) u.save() success = True else: if request.user.location: location_input = str(request.user.location) location_data = request.user.location.to_json() else: location_input = location_data = '' form = UserSettingsForm(instance=request.user, initial={'location_input':location_input, 'location_data':location_data,}) return render(request, 'user/settings.html', { 'success': success, #to drop a little notice like "CONGRATS #WINNING" 'entity':request.user, 'form': form, })
def setup(request): sans_facebook = True if request.GET.has_key( 'sans_facebook') and request.GET['sans_facebook'] else False redirect_to = request.GET.get('redirect_to', "/") form = CreateAccountForm(initial={ 'redirect_to': redirect_to, }) if request.POST: sans_facebook = True if request.POST.has_key( 'sans_facebook') and request.POST['sans_facebook'] else False form = CreateAccountForm(request.POST) if form.is_valid(): u = User() u.bio = form.cleaned_data['bio'] u.birth_year = form.cleaned_data['birth_year'] u.email = u.long_email = form.cleaned_data['email'] u.fb_access_token = form.cleaned_data['fb_access_token'] u.gender = form.cleaned_data['gender'] u.first_name = form.cleaned_data['first_name'] u.last_name = form.cleaned_data['last_name'] u.facebook_id = form.cleaned_data['fbid'] u.bio = u.bio.encode('utf-8') if u.bio else "" u.first_name = u.first_name.encode('utf-8') if u.first_name else "" u.last_name = u.last_name.encode('utf-8') if u.last_name else "" if form.cleaned_data['location_data']: u.location = Location.get_or_create( form.cleaned_data['location_data']) u.next_email_time = datetime.datetime.now() + timedelta(days=1) u.username = create_handle('%s%s' % (u.first_name, u.last_name)) u.password = hash_password(form.cleaned_data['password']) u.save() Subscription.get_or_create(user=u, pub_id=NOTIFICATIONS_PUB) #Post to Facebook if form.cleaned_data['post_to_facebook']: fb_helpers.post_joined_to_wall(u) cache.put_on_handle(u, u.username) redirect_to = form.cleaned_data['redirect_to'] or '/' #perform for all that login magic that happens under the covers attempt_login(request, u.username, form.cleaned_data["password"]) return set_auth_cookies(HttpResponseRedirect(redirect_to), u) return render( request, 'user/setup.html', { 'title': 'Setup your account', 'create_form': form, 'sans_facebook': sans_facebook, })
def setup(request): sans_facebook = True if request.GET.has_key('sans_facebook') and request.GET['sans_facebook'] else False redirect_to = request.GET.get('redirect_to', "/") form = CreateAccountForm(initial={'redirect_to': redirect_to,}) if request.POST: sans_facebook = True if request.POST.has_key('sans_facebook') and request.POST['sans_facebook'] else False form = CreateAccountForm(request.POST) if form.is_valid(): u = User() u.bio = form.cleaned_data['bio'] u.birth_year = form.cleaned_data['birth_year'] u.email = u.long_email = form.cleaned_data['email'] u.fb_access_token = form.cleaned_data['fb_access_token'] u.gender = form.cleaned_data['gender'] u.first_name = form.cleaned_data['first_name'] u.last_name = form.cleaned_data['last_name'] u.facebook_id = form.cleaned_data['fbid'] u.bio = u.bio.encode('utf-8') if u.bio else "" u.first_name = u.first_name.encode('utf-8') if u.first_name else "" u.last_name = u.last_name.encode('utf-8') if u.last_name else "" if form.cleaned_data['location_data']: u.location = Location.get_or_create(form.cleaned_data['location_data']) u.next_email_time = datetime.datetime.now() + timedelta(days = 1) u.username = create_handle('%s%s' % (u.first_name, u.last_name)) u.password = hash_password(form.cleaned_data['password']) u.save() Subscription.get_or_create(user = u, pub_id = NOTIFICATIONS_PUB) #Post to Facebook if form.cleaned_data['post_to_facebook']: fb_helpers.post_joined_to_wall(u) cache.put_on_handle(u, u.username) redirect_to = form.cleaned_data['redirect_to'] or '/' #perform for all that login magic that happens under the covers attempt_login(request, u.username, form.cleaned_data["password"]) return set_auth_cookies(HttpResponseRedirect(redirect_to), u) return render(request, 'user/setup.html', { 'title' : 'Setup your account', 'create_form' : form, 'sans_facebook': sans_facebook, })
def settings(request): success = False if request.method == 'POST': form = UserSettingsForm(request.POST, request.FILES, instance=request.user) old_pw_hash = request.user.password if form.is_valid(): u = form.save(commit=False) if u.password: u.password = hash_password(u.password) else: u.password = old_pw_hash if form.cleaned_data['profile_pic']: u.upload_profile_pic(form.cleaned_data['profile_pic']) if form.cleaned_data['location_data']: u.location = Location.get_or_create( form.cleaned_data['location_data']) u.save() success = True else: if request.user.location: location_input = str(request.user.location) location_data = request.user.location.to_json() else: location_input = location_data = '' form = UserSettingsForm(instance=request.user, initial={ 'location_input': location_input, 'location_data': location_data, }) return render( request, 'user/settings.html', { 'success': success, #to drop a little notice like "CONGRATS #WINNING" 'entity': request.user, 'form': form, })
def reset_password(request, reset_id): error = None if request.method == 'POST' and 'password' in request.POST and request.POST['password']: try: p = PasswordResetRequest.objects.get(uid = reset_id) u = p.user u.password = hash_password(request.POST['password'].strip()) u.save() #perform for all that login magic that happens under the covers user = attempt_login(request, u.username, request.POST['password'].strip()) p.delete() return set_auth_cookies(HttpResponseRedirect('/'), user) except Exception: logging.exception("Error In Reset Password") error = 'There was an error resetting your password.' return render(request, 'user/reset_password.html', { 'error' : error, 'reset_token' : reset_id, })
def reset_password(request, reset_id): error = None if request.method == 'POST' and 'password' in request.POST and request.POST[ 'password']: try: p = PasswordResetRequest.objects.get(uid=reset_id) u = p.user u.password = hash_password(request.POST['password'].strip()) u.save() #perform for all that login magic that happens under the covers user = attempt_login(request, u.username, request.POST['password'].strip()) p.delete() return set_auth_cookies(HttpResponseRedirect('/'), user) except Exception: logging.exception("Error In Reset Password") error = 'There was an error resetting your password.' return render(request, 'user/reset_password.html', { 'error': error, 'reset_token': reset_id, })
def update_user(request): if 'user' not in request.POST: return HttpResponseBadRequest() user = json.loads(request.POST['user']) if 'location' in user and user['location']: loc = user['location'] raw_geodata = json.dumps(loc["raw_geodata"]) if isinstance( loc.get("raw_geodata"), dict) else loc.get("raw_geodata") #Until we fix duplicate locations we have to do the following...lame. _locs = Location.objects.filter( raw_geodata=raw_geodata, longitude=loc.get('longitude', None), latitude=loc.get('latitude', None), address=loc.get('address', ' '), region=loc.get('region', ' '), locality=loc.get('locality', ' '), postal_code=loc.get('postal_code', ' '), country_name=loc.get('country_name', ' ')) if len(_locs) > 0: _loc = _locs[0] else: _loc = Location( raw_geodata=raw_geodata, longitude=loc.get('longitude', None), latitude=loc.get('latitude', None), address=loc.get('address', ' '), region=loc.get('region', ' '), locality=loc.get('locality', ' '), postal_code=loc.get('postal_code', ' '), country_name=loc.get('country_name', ' '), ) _loc.save() request.user.location = _loc else: request.user.location = None str_fields = [ 'first_name', 'last_name', 'email', 'gender', 'bio', 'url', 'twitter_id', 'flickr_id', 'youtube_id', 'vimeo_id', 'blog_url', ] settings_fields = [ 'enable_jumo_updates', 'email_stream_frequency', 'post_to_fb', ] int_fields = [ 'birth_year', ] if 'enable_followed_notification' in user: try: sub = request.user.subscriptions.get(id=NOTIFICATIONS_PUB) except Subscription.DoesNotExist: sub = Subscription.get_or_create(user=request.user, pub_id=NOTIFICATIONS_PUB) if sub.subscribed <> user['enable_follow_notification']: sub.subscribed = user['enable_follow_notification'] sub.save() for f in str_fields: if f in user and user[f] != getattr(request.user, f): setattr(request.user, f, user[f]) for f in settings_fields: settings = user['settings'] if f in settings: setattr(request.user, f, settings[f]) for f in int_fields: if f in user and user[f] != getattr(request.user, f): if user[f] == '': user[f] = None setattr(request.user, f, user[f]) if 'password' in user and user['password'] != '': request.user.password = hash_password(user['password']) if 'username' in user and user['username'] != request.user.username: _username = request.user.username request.user.username = create_handle(user['username']) cache.bust_on_handle(request.user, _username, False) request.user.save() cache.bust_on_handle(request.user, request.user.username) return json_response({'result': request.user.username})
def update_user(request): if 'user' not in request.POST: return HttpResponseBadRequest() user = json.loads(request.POST['user']) if 'location' in user and user['location']: loc = user['location'] raw_geodata = json.dumps(loc["raw_geodata"]) if isinstance(loc.get("raw_geodata"), dict) else loc.get("raw_geodata") #Until we fix duplicate locations we have to do the following...lame. _locs = Location.objects.filter(raw_geodata = raw_geodata, longitude = loc.get('longitude', None), latitude = loc.get('latitude', None), address = loc.get('address', ' '), region = loc.get('region', ' '), locality = loc.get('locality', ' '), postal_code = loc.get('postal_code', ' '), country_name = loc.get('country_name', ' ')) if len(_locs) > 0: _loc = _locs[0] else: _loc = Location(raw_geodata = raw_geodata, longitude = loc.get('longitude', None), latitude = loc.get('latitude', None), address = loc.get('address', ' '), region = loc.get('region', ' '), locality = loc.get('locality', ' '), postal_code = loc.get('postal_code', ' '), country_name = loc.get('country_name', ' '),) _loc.save() request.user.location = _loc else: request.user.location = None str_fields = [ 'first_name', 'last_name', 'email', 'gender', 'bio', 'url', 'twitter_id', 'flickr_id', 'youtube_id', 'vimeo_id', 'blog_url', ] settings_fields = [ 'enable_jumo_updates', 'email_stream_frequency', 'post_to_fb', ] int_fields = [ 'birth_year', ] if 'enable_followed_notification' in user: try: sub = request.user.subscriptions.get(id=NOTIFICATIONS_PUB) except Subscription.DoesNotExist: sub = Subscription.get_or_create(user=request.user, pub_id=NOTIFICATIONS_PUB) if sub.subscribed <> user['enable_follow_notification']: sub.subscribed = user['enable_follow_notification'] sub.save() for f in str_fields: if f in user and user[f] != getattr(request.user, f): setattr(request.user, f, user[f]) for f in settings_fields: settings = user['settings'] if f in settings: setattr(request.user, f, settings[f]) for f in int_fields: if f in user and user[f] != getattr(request.user, f): if user[f] == '': user[f] = None setattr(request.user, f, user[f]) if 'password' in user and user['password'] != '': request.user.password = hash_password(user['password']) if 'username' in user and user['username'] != request.user.username: _username = request.user.username request.user.username = create_handle(user['username']) cache.bust_on_handle(request.user, _username, False) request.user.save() cache.bust_on_handle(request.user, request.user.username) return json_response({'result' : request.user.username})