def profile(request): logger = logging.getLogger("django") data = {} logger.error(request.method) if request.user.is_authenticated(): if request.method == 'POST': logger.error(request.FILES) profile_form = UserProfileForm(data=request.POST, files=request.FILES, instance=request.user.profile) logger.error(profile_form) if profile_form.is_valid(): logger.error(profile_form) profile_form.save() else: logger.error(profile_form.errors) data['error'] = "invalid input" data = {} user_profile = request.user.profile skillset = user_profile.skillset applications = Application.objects.filter(applicant=request.user) data['profile_form'] = UserProfileForm(instance=user_profile) data['skillset'] = skillset data['applications'] = applications print user_profile return render(request, 'core/profile.html', data) else: return index(request)
def edit_profile(request): context = RequestContext(request) error_msg = [] success = False if request.user.is_authenticated(): if request.method == 'POST': upf = UserProfileForm( data=request.POST, files=request.FILES, instance=request.user.userextension ) if upf.is_valid(): user = request.user user.first_name = request.POST['first_name'] user.last_name = request.POST['last_name'] user.save() #upf.birthdate = request.POST['birthdate'] upf.save() #userprofile = upf.save(commit=False) #userprofile.user = user #userprofile.save() success = True else: upf = UserProfileForm(instance=request.user) else: return redirect('/login/') return render_to_response('edit_profile.html', {'user': request.user, 'userprofileform': upf, 'success': success}, context_instance=context)
def register(request): registered = False if request.method == 'POST': user_form_new = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form_new.is_valid() and profile_form.is_valid(): user = user_form_new.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() context = { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered } template = loader.get_template('auth/register.html') return HttpResponse(template.render(context, request))
def register(request): # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'base/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def test_users_are_given_unique_usernames(self): # If joe-bloggs doesn't exist, user is given normal username form = UserProfileForm({ 'first_name': 'Joe', 'last_name': 'Bloggs', }) form.is_valid() self.assertEqual(form.cleaned_data['username'], 'joe-bloggs') user = User(username='******', first_name='Joe', last_name='Bloggs') user.set_password('password') user.save() # If joe-bloggs does exist, suffix is added form = UserProfileForm({ 'first_name': 'Joe', 'last_name': 'Bloggs', }) form.is_valid() self.assertEqual(form.cleaned_data['username'], 'joe-bloggs-2')
def add_profile(request): context = RequestContext(request) if request.method == "POST": profile_form = UserProfileForm(data=request.POST) if profile_form.is_valid(): user = profile_form.save() profile_form.save() else: print profile_form.errors else: profile_form = UserProfileForm() return render_to_response("/profile/add_profile.html", {'profile_form': profile_form}, context)
def view_settings(request): ctx = {} if request.method == 'POST': form = UserProfileForm( request.POST, request=request, instance=UserProfile.objects.get(user=request.user)) if form.is_valid(): request.user.email = form.cleaned_data['email'] request.user.save() form.save() if 'picture' in request.FILES: f = request.FILES.get("picture") m = sha1() m.update(request.user.username) hash = m.hexdigest() ext = f.name.split( "." )[1] # UserProfileForm.clean_picture() makes sure this is safe. filename = "userimg_%s.%s" % (hash, ext) path = settings.MEDIA_ROOT + "/" + filename #url = settings.MEDIA_URL + filename pic = open(path, 'wb+') for chunk in f.chunks(): pic.write(chunk) pic.close() p = request.user.userprofile p.picture.name = filename p.save() return HttpResponseRedirect("/accounts/profile/") else: print "FAIL!" ctx["form"] = form return render_to_response("settings.html", ctx, context_instance=RequestContext(request)) else: form = UserProfileForm( initial={'email': request.user.email}, instance=UserProfile.objects.get(user=request.user)) ctx["form"] = form return render_to_response("settings.html", ctx, context_instance=RequestContext(request))
def user_registration(request): template = 'core/user_registration.html' success_message = _( 'Registration completed. Check your phone for SMS confirmation code.') error_message = _('Error during registration. <br>Details: {}') if request.method == 'POST': user_form = CustomUserCreationForm(request.POST) profile_form = UserProfileForm(request.POST) if user_form.is_valid() and profile_form.is_valid(): try: with transaction.atomic(using='default'): user = user_form.save(commit=False) user.username = profile_form.cleaned_data['phone'] user.save() profile_form = UserProfileForm(request.POST, instance=user.profile) profile = profile_form.save(commit=False) profile.disabled = True profile.save() res = _send_sms(user) assert res if settings.DEBUG: print(res) messages.success(request, success_message) user = authenticate( username=user.username, password=user_form.cleaned_data['password1']) login(request, user) return redirect(reverse('core.user_profile')) except Exception as e: msg = error_message.format(e) messages.error(request, msg) else: user_form = CustomUserCreationForm() profile_form = UserProfileForm() return render(request, template, { 'user_form': user_form, 'profile_form': profile_form })
def user_edit(request, uuid): profile_object = get_object_or_404(UserProfile, uuid=uuid) user_object = profile_object.user if request.method == "POST": uform = UserForm(data=request.POST, instance=user_object) pform = UserProfileForm(data=request.POST, instance=profile_object) if uform.is_valid() and pform.is_valid(): user = uform.save() profile = pform.save(commit = False) profile.user = user profile.save() return HttpResponseRedirect(reverse('core.views.user', args=(uuid,))) else: uform = UserForm(instance=user_object) pform = UserProfileForm(instance=profile_object) return render_to_response('core/edit_user.html', {'profile_user': user_object, 'uform': uform, 'pform': pform}, context_instance=RequestContext(request))
def UserEdit(request, username): profile = UserProfile.objects.get(user__username=username) user = User.objects.get(username=username) if request.user.is_authenticated() and request.user.id == user.id: if request.method == "POST": profile_form = UserProfileForm(request.POST, request.FILES, instance=profile) if profile_form.is_valid(): updated_user = profile_form.save() client_msg = "Your profile has been updated." messages.add_message(request, messages.INFO, client_msg) return HttpResponseRedirect("/people/%s" % updated_user.username) else: print profile_form.errors else: profile_form = UserProfileForm(instance=profile) return render(request, 'user_edit.html', {'profile_form': profile_form}) return HttpResponseRedirect("/")
def UserEdit(request, username): profile = UserProfile.objects.get(user__username=username) user = User.objects.get(username=username) if request.user.is_authenticated() and request.user.id == user.id: if request.method == "POST": profile_form = UserProfileForm(request.POST, request.FILES, instance=profile) if profile_form.is_valid(): updated_user = profile_form.save() client_msg = "Your profile has been updated." messages.add_message(request, messages.INFO, client_msg) return HttpResponseRedirect("/people/%s" % updated_user.username) else: print profile_form.errors else: profile_form = UserProfileForm(instance=profile) return render(request, "user_edit.html", {"profile_form": profile_form}) return HttpResponseRedirect("/")
def UserEdit(request, username): profile = UserProfile.objects.get(user__username=username) user = User.objects.get(username=username) if request.user.is_authenticated() and request.user.id == user.id: if request.method == "POST": profile_form = UserProfileForm(request.POST, request.FILES, instance=profile) if profile_form.is_valid(): updated_user = profile_form.save() profile = updated_user.profile print "request data: image field" img_data = request.POST.get("image") if img_data: #print img_data img_data = base64.b64decode(img_data) filename = "%s.png" % uuid.uuid4() # XXX make the upload path a fixed setting in models, since it's # referenced in three places upload_path = "data/avatars/%s/" % user.username upload_abs_path = os.path.join(settings.MEDIA_ROOT, upload_path) if not os.path.exists(upload_abs_path): os.makedirs(upload_abs_path) full_file_name = os.path.join(upload_abs_path, filename) with open(full_file_name, 'wb') as f: f.write(img_data) f.close() profile.image = full_file_name profile.save() client_msg = "Your profile has been updated." messages.add_message(request, messages.INFO, client_msg) return HttpResponseRedirect("/people/%s" % updated_user.username) else: print profile_form.errors else: profile_form = UserProfileForm(instance=profile) if profile.image: has_image = True else: has_image = False #print 'profile image already?' #print has_image return render(request, 'registration/registration_form.html', {'form': profile_form, 'has_image': has_image, 'existing_user': True}) return HttpResponseRedirect("/")
def post(self,request): data = request.POST user_form = UserForm(data=data) user_profile_form = UserProfileForm(data=data) if user_form.is_valid() and user_profile_form.is_valid(): user_object = user_form.save(commit=False) user_object.set_password(user_object.password) user_object.save() user_prof_object = user_profile_form.save(commit=False) user_prof_object.user = user_object user_prof_object.save() return HttpResponseRedirect('/accounts/login/?message=Registration successfull, Please login with your credentials') context = { 'user_profile_form':user_profile_form, 'user_form': user_form } return render(request,'registration/registration.html',context)
def view_settings(request): ctx = {} if request.method == 'POST': form = UserProfileForm(request.POST, request=request, instance=UserProfile.objects.get(user=request.user)) if form.is_valid(): request.user.email = form.cleaned_data['email'] request.user.save() form.save() request.session[LANGUAGE_SESSION_KEY] = request.user.userprofile.language if 'picture' in request.FILES: f = request.FILES.get("picture") m = sha1() m.update(force_bytes(request.user.username)) hash = m.hexdigest() ext = f.name.split(".")[1] # UserProfileForm.clean_picture() makes sure this is safe. filename = "userimg_%s.%s" % (hash, ext) path = settings.MEDIA_ROOT + "/" + filename #url = settings.MEDIA_URL + filename pic = open(path, 'wb+') for chunk in f.chunks(): pic.write(chunk) pic.close() p = request.user.userprofile p.picture.name = filename p.save() return HttpResponseRedirect("/accounts/profile/") else: print "FAIL!" ctx["form"] = form return render_to_response("settings.html", ctx, context_instance=RequestContext(request)) else: form = UserProfileForm(initial={'email': request.user.email}, instance=UserProfile.objects.get(user=request.user)) ctx["form"] = form return render_to_response("settings.html", ctx, context_instance=RequestContext(request))
def post(self, request): data = request.POST user_form = UserForm(data=data) user_profile_form = UserProfileForm(data=data) if user_form.is_valid() and user_profile_form.is_valid(): user_object = user_form.save(commit=False) user_object.set_password(user_object.password) user_object.save() user_prof_object = user_profile_form.save(commit=False) user_prof_object.user = user_object user_prof_object.save() return HttpResponseRedirect( '/accounts/login/?message=Registration successfull, Please login with your credentials' ) context = { 'user_profile_form': user_profile_form, 'user_form': user_form } return render(request, 'registration/registration.html', context)
def user_edit(request, uuid): profile_object = get_object_or_404(UserProfile, uuid=uuid) user_object = profile_object.user if request.method == "POST": uform = UserForm(data=request.POST, instance=user_object) pform = UserProfileForm(data=request.POST, instance=profile_object) if uform.is_valid() and pform.is_valid(): user = uform.save() profile = pform.save(commit=False) profile.user = user profile.save() return HttpResponseRedirect( reverse('core.views.user', args=(uuid, ))) else: uform = UserForm(instance=user_object) pform = UserProfileForm(instance=profile_object) return render_to_response('core/edit_user.html', { 'profile_user': user_object, 'uform': uform, 'pform': pform }, context_instance=RequestContext(request))
def register(request): # Boolean saying whether registration is successful. False initially. registered = False if request.method == 'POST': # Take information from both forms. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): # Save user data to database and hash passwrod. user = user_form.save() user.set_password(user.password) user.save() # Save userprofil form data, delaying commiting until user foreign key is fileld. profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.profile_pic = request.FILES['picture'] profile.save() registered = True else: # Print problems to the terminal. print(user_form.errors, profile_form.errors) return HttpResponse( "The username or password you entered is wrong.") else: # Not form submitting, render blank forms isntead. user_form = UserForm() profile_form = UserProfileForm() return render( request, 'registration/registration_form.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def view_settings(request): ctx = {} if request.method == "POST": form = UserProfileForm(request.POST, instance=request.user.get_profile()) if form.is_valid(): request.user.email = form.cleaned_data["email"] request.user.save() form.save() if "picture" in request.FILES: f = request.FILES.get("picture") m = sha1() m.update(request.user.username) hash = m.hexdigest() ext = f.name.split(".")[1] filename = "userimg_%s.%s" % (hash, ext) path = settings.MEDIA_ROOT + "/" + filename # url = settings.MEDIA_URL + filename pic = open(path, "wb+") for chunk in f.chunks(): pic.write(chunk) pic.close() p = request.user.get_profile() p.picture.name = filename p.save() return HttpResponseRedirect("/accounts/profile/") else: print "FAIL!" ctx["form"] = form return render_to_response("settings.html", ctx, context_instance=RequestContext(request)) else: form = UserProfileForm(initial={"email": request.user.email}, instance=request.user.get_profile()) ctx["form"] = form return render_to_response("settings.html", ctx, context_instance=RequestContext(request))
def view_settings(request): # Short-hands. profile = request.user.userprofile if request.method == 'POST': form = UserProfileForm(request.POST, request=request, instance=profile) if form.is_valid(): # FIXME/TODO: When a user changes email addresses, there is # currently no functionality to verify the new email address. # Therefore, the email field is disabled in UserProfileForm until # that functionality has been implemented. #request.user.email = form.cleaned_data['email'] #request.user.save() form.save() set_language(request, form.cleaned_data['language']) if 'picture' in request.FILES: in_file = request.FILES.get('picture') # UserProfileForm makes sure that this works. extension = in_file.name.split('.')[-1] # A function for generating a new filename relative to upload # directory, returning both that and a full path as well. def new_full_name(extension): # Filename relative to the uploads directory. filename = os.path.join( UserProfile.picture.field.upload_to, 'userimg_%s.%s' % (random_word(12), extension)) # Full path of the image on disk. fullpath = os.path.join(settings.MEDIA_ROOT, filename) return filename, fullpath # We'll generate a random filename until we find one that # isn't already in use. (Almost certainly, the first attempt # will do just fine.) filename, path = new_full_name(extension) while os.path.isfile(path): filename, path = new_full_name(extension) # Write the picture to disk. pic = open(path, 'wb+') for chunk in in_file.chunks(): pic.write(chunk) pic.close() # Save the picture's name in the profile. profile.picture.name = filename profile.save() # Cleanup time! # First, find picture files used by any profile. db_pictures = [ up['picture'] for up in UserProfile.objects.all().values( 'picture').distinct() ] # Paths of profile pictures are denoted relative to the # settings.MEDIA_ROOT directory. The "upload_to" parameter # provided in the "picture" ImageField in the UserProfile # model tells us where exactly, inside settings.MEDIA_ROOT, # the profile pictures can be found. For example, when the # "upload_to" field is "profiles" (as should be the case # here), the filenames denoted in the user profiles should be # something like "profiles/userimg_something.png". This path # is relative to the settings.MEDIA_ROOT directory. upload_to = UserProfile.picture.field.upload_to # List the files that are actually in the profile picture # directory and delete them if they are no longer in use. items = os.listdir(os.path.join(settings.MEDIA_ROOT, upload_to)) for item in items: # Let's not delete the default image. That would be silly. if item == 'default.jpg': continue # We'll use full disk paths for file operations. item_fullpath = os.path.join(settings.MEDIA_ROOT, upload_to, item) if os.path.isdir(item_fullpath): # If this is a directory, we are slightly more shy of # deleting the whole thing, so we'll explicitly check # if it's a thumbnail directory (ending with # "-thumbnail"). If it's some random directory of # unknown origin, we'll leave it alone. if item[-10:] == '-thumbnail' and os.path.join( upload_to, item[:-10]) not in db_pictures: shutil.rmtree(item_fullpath) elif os.path.isfile(item_fullpath): # If this is a file, and it's not being used in a user # profile, we'll delete it. if os.path.join(upload_to, item) not in db_pictures: os.unlink(item_fullpath) if hasattr(settings, 'ICEPIRATE'): # The request.user object doesn't yet reflect recently made # changes, so we need to ask the database explicitly. update_member(User.objects.get(id=request.user.id)) return redirect(reverse('profile')) else: form = UserProfileForm(initial={'email': request.user.email}, instance=profile) ctx = { 'form': form, } return render(request, 'accounts/settings.html', ctx)
def profile(request): link_req_cookie = 'link_req_sent-{}'.format(request.user.id) context = {} kwargs = dict(user=request.user) member = Member.objects.safe_get(**kwargs) context['member'] = member # We store the fact that the user has requested to be linked to a Member in a cookie # This way we don't show the link request again (on the same browser) once they've clicked on it try: context['link_req_sent'] = int(request.COOKIES.get( link_req_cookie, 0)) except ValueError: context['link_req_sent'] = 0 if request.method == 'POST': if request.POST.get('request_link') == '1': # This is a request to link the authenticated user to a member. context['form'] = UserProfileForm(instance=request.user) try: success = send_link_req(request) if success: context['link_req_sent'] = 1 except CshcUser.DoesNotExist: pass elif request.POST.get('no_member') == '1': # This is a User with no member associated. So we should use the UserProfileForm. form = UserProfileForm(request.POST, instance=request.user) if form.is_valid(): updated_user = form.save() messages.success( request, "Your profile has been updated successfully") context['form'] = UserProfileForm(instance=updated_user) else: messages.error( request, "Profile could not be updated. See individual fields for details.") context['form'] = form else: # This is a User with an associated Member. Use the MemberProfileForm. form = MemberProfileForm( request.POST, request.FILES, instance=member) if form.is_valid(): updated_member = form.save() messages.success( request, "Your profile has been updated successfully") context['form'] = MemberProfileForm(instance=updated_member) context['form'].fields['dob'].initial = updated_member.dob else: messages.error( request, "Profile could not be updated. See individual fields for details.") context['form'] = form else: # if req_link is supplied in the url params, trigger the player link request now req_link_id = request.GET.get('req_link_id') if not member and not context['link_req_sent'] and req_link_id is not None: try: success = send_link_req(request) if success: context['link_req_sent'] = 1 except CshcUser.DoesNotExist: pass # Create the appropriate form, populated with the model data context['form'] = MemberProfileForm( instance=member) if member is not None else UserProfileForm(instance=request.user) response = render(request, 'account/profile.html', context) response.set_cookie(link_req_cookie, context.get('link_req_sent', 0)) return response
def register(request): # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'base/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }, context)
def settings(request): """ :param request: :return: Initialize core for first time logged in user. """ context_dict = {} try: user = User.objects.get(username=request.user.username) except User.DoesNotExist: return redirect('core:index') userprofile = UserProfile.objects.get_or_create(user=user)[0] context_dict['userprofile'] = userprofile if userprofile.is_previously_logged: try: levels = Level.objects.order_by("created") for level in levels: new_levelpublish = LevelPublish.objects.get_or_create( userprofile=userprofile, level=level) if new_levelpublish[1]: new_levelpublish[0].publish = False new_levelpublish[0].save() except: print("Levels not added") return redirect('core:index') else: try: levels = Level.objects.order_by("created") for level in levels: levelpublish = LevelPublish.objects.get_or_create( userprofile=userprofile, level=level)[0] levelpublish.publish = False levelpublish.save() userprofile.clear = 0 userprofile.save() first_level = levels.first() first_level.publish = True first_level.save() all_levelpublish = LevelPublish.objects.filter( userprofile=userprofile) first_levelpublish = all_levelpublish[0] first_levelpublish.publish = True first_levelpublish.save() except: print("Levels not added") userprofile.is_previously_logged = True # userprofile.save() form = UserProfileForm() if request.method == 'POST': form = UserProfileForm(request.POST, request.FILES, instance=userprofile) if form.is_valid(): form.save() print(request.POST.get('firstname')) user.first_name = request.POST.get('firstname') user.last_name = request.POST.get('lastname') user.save() print(user.first_name) return redirect('core:index') else: print(form.errors) return render(request, 'core/settings.html', context=context_dict)
def rsvp_new_user(request, event_id, event_slug, location_slug=None): location = get_object_or_404(Location, slug=location_slug) if not request.method == 'POST': return HttpResponseRedirect('/404') print 'in rsvp_new_user' print request.POST # get email signup info and remove from form, since we tacked this field on # but it's not part of the user model. weekly_updates = request.POST.get('weekly-email-notifications') notify_new = request.POST.get('new-event-notifications') if weekly_updates == 'on': weekly_updates = True else: weekly_updates = False print 'weekly updates?' print weekly_updates if notify_new == 'on': notify_new = True else: notify_new = False # Create new user but simplify the process form = UserProfileForm(request.POST) form.fields['city'].required = False form.fields['referral'].required = False form.fields['image'].required = False form.fields['cropped_image_data'].required = False form.fields['discussion'].required = False form.fields['sharing'].required = False form.fields['projects'].required = False print form if form.is_valid(): new_user = form.save() new_user.save() notifications = new_user.event_notifications if weekly_updates: # since the signup was related to a specific location we assume # they wanted weekly emails about the same location notifications.location_weekly.add(location) if notify_new: # since the signup was related to a specific location we assume # they wanted weekly emails about the same location notifications.location_publish.add(location) notifications.save() password = request.POST.get('password1') new_user = authenticate(username=new_user.username, password=password) login(request, new_user) # RSVP new user to the event event = Event.objects.get(id=event_id) event.attendees.add(new_user) print(event.attendees.all()) event.save() messages.add_message( request, messages.INFO, 'Thanks! Your account has been created. Check your email for login info and how to update your preferences.' ) return HttpResponse(status=200) else: errors = json.dumps({"errors": form.errors}) return HttpResponse(json.dumps(errors)) return HttpResponse(status=500)
def rsvp_new_user(request, event_id, event_slug, location_slug=None): location = get_object_or_404(Location, slug=location_slug) if not request.method == 'POST': return HttpResponseRedirect('/404') print 'in rsvp_new_user' print request.POST # get email signup info and remove from form, since we tacked this field on # but it's not part of the user model. weekly_updates = request.POST.get('weekly-email-notifications') notify_new = request.POST.get('new-event-notifications') if weekly_updates == 'on': weekly_updates = True else: weekly_updates = False print 'weekly updates?' print weekly_updates if notify_new == 'on': notify_new = True else: notify_new = False # Create new user but simplify the process form = UserProfileForm(request.POST) form.fields['city'].required = False form.fields['referral'].required = False form.fields['image'].required = False form.fields['cropped_image_data'].required = False form.fields['discussion'].required = False form.fields['sharing'].required = False form.fields['projects'].required = False print form if form.is_valid(): new_user = form.save() new_user.save() notifications = new_user.event_notifications if weekly_updates: # since the signup was related to a specific location we assume # they wanted weekly emails about the same location notifications.location_weekly.add(location) if notify_new: # since the signup was related to a specific location we assume # they wanted weekly emails about the same location notifications.location_publish.add(location) notifications.save() password = request.POST.get('password1') new_user = authenticate(username=new_user.username, password=password) login(request, new_user) # RSVP new user to the event event = Event.objects.get(id=event_id) event.attendees.add(new_user) print (event.attendees.all()) event.save() messages.add_message(request, messages.INFO, 'Thanks! Your account has been created. Check your email for login info and how to update your preferences.') return HttpResponse(status=200) else: errors = json.dumps({"errors": form.errors}) return HttpResponse(json.dumps(errors)) return HttpResponse(status=500);