def post(self, request, *args, **kwargs): form = self.form(request.POST) if form.is_valid(): user_exists = SessionManager.get_user_by_username_or_email(strip_tags(request.POST['email'])) if user_exists: messages.error(request, '{} already has an account.'.format(user_exists.email)) else: new_app_user = AppUser() new_app_user.save() participant = ExchangeParticipant.get_or_create( appuser=new_app_user, giftexchange=self.giftexchange, status='pending' ) UserToken.clean(appuser=new_app_user, token_type='invitation') token = UserToken(appuser=new_app_user, token_type='invitation') token._generate_token() token.save() mailer = GifteratorMailer() mailer.send_exchange_invitation_email( request.POST['email'], self.giftexchange, existing_user=False, registration_token=token ) messages.success(request, 'An invitation has been sent to {}!'.format(request.POST['email'])) if settings.PREVIEW_EMAILS_IN_APP: self.context.update({'show_email': mailer}) self.context.update({'form': self.form()}) return HttpResponse(self.template.render(self.context, request))
def post(self, request, *args, **kwargs): form = self.form(request.POST) if form.is_valid(): existing_user = SessionManager.get_user_by_username_or_email(request.POST['email_address']) if existing_user: messages.error(request, 'This user is already registered.') self.context.update({'form': form}) return HttpResponse(self.template.render(self.context, request)) else: new_appuser = AppUser(registration_source='invitation') new_appuser.save() mailer = SessionManagerEmailer() UserToken.clean(appuser=new_appuser, token_type='invitation') invitation_token = UserToken( appuser=new_appuser, token_type='invitation', ) invitation_token._generate_token() invitation_token.save() mailer.send_app_invitation_link( to_email=request.POST['email_address'], from_appuser=self.appuser, token=invitation_token ) messages.success( request, 'You sent an invitation to {}!'.format(request.POST['email_address']) ) if settings.PREVIEW_EMAILS_IN_APP: self.context.update({'show_email': mailer}) self.context.update({'form': form}) return HttpResponse(self.template.render(self.context, request))
def post(self, request, *args, **kwargs): form = PreRegisterUserForm(request.POST) if form.is_valid(): email_error = validate_email(request.POST['email'], 0) if email_error: error = '{}<p>Did you mean to <a href="{}">log in instead</a>?'.format( email_error, reverse('session_manager_login') ) messages.error(request, error) else: user = SessionManager.get_user_by_username_or_email(request.POST['email']) if not user: user, appuser = SessionManager.preregister_email(request.POST['email']) UserToken.clean(appuser=appuser, token_type='registration') token = UserToken(appuser=appuser, token_type='registration') token._generate_token() token.save() mailer = SessionManagerEmailer() mailer.send_app_registration_link(user, token) if settings.PREVIEW_EMAILS_IN_APP: self.context.update({'show_email': mailer}) messages.success(request, 'Thanks! To verify your email address, we have sent you a link to complete your registration.') return HttpResponse(self.template.render(self.context, request)) self.context.update({ 'form': form, }) return HttpResponse(self.template.render(self.context, request))
def post(self, request, *args, **kwargs): post_target = request.POST.get('js_target') self.data.update({'postTarget': post_target}) if post_target == 'email-search': search_results = SessionManager.search(strip_tags(request.POST['email'])) valid_results = [] for user in search_results.all(): if user not in self.active_participant_users: valid_results.append(user) self.context.update({ 'search_results': valid_results, 'search_term': request.POST['email'] }) result_html = loader.render_to_string( 'gifterator/admin_dashboard_includes/search_results.html', context=self.context, request=request ) self.data.update({ 'status': 'success', 'html': result_html }) elif post_target == 'add-user': user = SessionManager.get_user_by_username_or_email(strip_tags(request.POST['user-email'])) if user == self.user: self.participant.status = 'active' self.participant.save() else: # TODO get_or_create here? participant = ExchangeParticipant.get_or_create( appuser=user.appuser, status='invited', giftexchange=self.giftexchange ) email_view_url = '{}?participant_id={}'.format( reverse( 'gifterator_exchange_admin_sendmail', kwargs={ 'ex_uuid': self.giftexchange.uuid, 'mail_type': 'exchange-invitation' } ), participant.pk ) self.data.update(self._render_participant_list(request)) self.data.update({'postProcessUrl': email_view_url}) elif post_target == 'remove-user': user = SessionManager.get_user_by_username_or_email(request.POST['user-email']) participant = ExchangeParticipant.objects.get(appuser__user=user, giftexchange=self.giftexchange) if user == self.user: participant.status = 'inactive' participant.save() else: participant.delete() self.data.update(self._render_participant_list(request)) elif post_target == 'update-details': exchange_details_form = self.exchange_details_form( self.giftexchange, self.non_model_initial, request.POST, ) if exchange_details_form.is_valid(): post_data = {field: strip_tags(str(value)) for field, value in request.POST.items()} for field in GiftExchange.boolean_fields(): if post_data.get(field) == 'on': post_data[field] = True else: post_data[field] = False self.giftexchange.update(**post_data) self.data.update({ 'status': 'success', 'message': 'Updated gift exchange details' }) else: error_messages = [] for field, message_list in exchange_details_form.errors.items(): error_messages.append('<strong>{}</strong><br /> {}'.format( field, '<br />'.join(message_list) )) self.data.update({ 'status': 'error', 'message': '<br />'.join(error_messages) }) elif post_target == 'set-assignments': self.giftexchange.generate_assignemnts(override_lock=False) self.context.update({ 'assignment_list': self.giftexchange.exchangeassignment_set.all() }) result_html = loader.render_to_string( 'gifterator/admin_dashboard_includes/assignments_list.html', context=self.context, request=request, ) self.data.update({ 'status': 'success', 'html': result_html }) elif post_target == 'lock-assignments': self.giftexchange.locked = True self.giftexchange.save() self.data.update({ 'status': 'success', }) elif post_target == 'unset-assignments': self.giftexchange.locked = False self.assigment_list.all().delete() self.giftexchange.save() self.data.update(self._render_participant_list(request)) elif post_target == 'notify-participants': self.giftexchange.participants_notified = True self.giftexchange.save() for assignment in self.giftexchange.exchangeassignment_set.all(): mailer = GifteratorMailer() mailer.send_assignment_email(assignment) assignment.giver.email_last_sent = timezone.now() assignment.giver.save() result_html = loader.render_to_string( 'gifterator/admin_dashboard_includes/assignments_list.html', context=self.context, request=request, ) self.data.update({ 'status': 'success', 'html': result_html }) elif post_target == 'notify-single-participant': assignment = ExchangeAssignment.objects.get(pk=request.POST['assignment_id']) mailer = GifteratorMailer() mailer.send_assignment_email(assignment) assignment.giver.email_last_sent = timezone.now() assignment.giver.save() result_html = loader.render_to_string( 'gifterator/admin_dashboard_includes/assignments_list.html', context=self.context, request=request, ) self.data.update({ 'status': 'success', 'html': result_html }) elif post_target == 'send-bulk-message': message_target = request.POST['target'] message_body = strip_tags(request.POST['message']) target_users = self._get_participant_subset(message_target) mailer = GifteratorMailer() mailer.send_admin_message( giftexchange_name=self.giftexchange.title, message_body=message_body, from_user=self.participant, to_users=target_users ) self.data.update({ 'status': 'success', 'successMessage': 'Sent an email to {} users'.format(target_users.count()), 'html': None, }) self._refresh_data() return JsonResponse(self.data)
def post(self, request, *args, **kwargs): # we should only get here if they submitted the form instead of a token in the URL # standard Django form handling here if 'password' not in request.POST: self.login_stage = 2 self.context.update({'login_stage': self.login_stage}) form = LoginEmailForm(request.POST) if form.is_valid(): user = SessionManager.get_user_by_username_or_email(request.POST['email']) if not user: messages.error(request, 'Could not find account with that email address.') elif not user.password: messages.error( request, ( "It looks like you haven't completed your yet. " "If your registration link was lost or is expired, you can " "request a new one." ) ) self.template = loader.get_template('session_manager/default.html') self.context.update({ 'form': RegistrationLinkForm(initial={'email': user.email}), 'submit_text': 'Re-send Registration Link', 'form_action': reverse('session_manager_send_registration_link'), 'email': user.email, }) return HttpResponse(self.template.render(self.context, request)) else: self.context.update({ 'form': LoginPasswordForm(initial={'email': user.email}), 'email': user.email, }) return HttpResponse(self.template.render(self.context, request)) else: messages.error(request, 'Something went wrong. Please correct errors below.') self.context.update({ 'form': form, }) return HttpResponse(self.template.render(self.context, request)) else: self.login_stage = 3 self.context.update({'login_stage': self.login_stage}) form = LoginPasswordForm(request.POST) if form.is_valid(): user, error_reason = SessionManager.check_user_login( username_or_email=request.POST['email'], password=request.POST['password'] ) if not user: messages.error(request, error_reason) self.context.update({ 'form': form, }) return HttpResponse(self.template.render(self.context, request)) else: login(request, user) request.session['user_is_authenticated'] = True if settings.DISPLAY_AUTH_SUCCESS_MESSAGES: messages.success(request, 'Log in successful.') return redirect(reverse(settings.LOGIN_SUCCESS_REDIRECT)) return HttpResponse(self.template.render(self.context, request))