def _send_email(subject, message, receiver_name, receiver_email): """ Send Email method :param subject: Subject of the email :param message: Email Content :param receiver_name: Name of the receiver :param receiver_email: Email of the receiver :return: Nothing """ from utils.mailer import EmailHelper EmailHelper.send_email(message=message, subject=subject, receiver_name=receiver_name, receiver_email=receiver_email)
def recover_pwd(request): """ Method used to ask for a reset password :param request: :return: """ if request.user.is_authenticated: return HttpResponseRedirect(reverse('dashboard:dashboard')) if request.POST: username = request.POST['email'] try: profile = Profile.get_by_email(username) # Check if user is active if not profile.user.is_active: messages.error( request, 'Your user is not yet active, ' 'please complete the activation process before requesting a new password' ) return HttpResponseRedirect(reverse('dashboard:login')) profile.reset_token = Profile.get_new_reset_token() profile.ask_reset_at = dt.datetime.now() profile.save() # send e-mail email_body = authentication_reset_password.format( FIRST_NAME=profile.user.first_name.encode('utf-8'), LAST_NAME=profile.user.last_name.encode('utf-8'), BASE_URL=get_current_site(request), TOKEN=profile.reset_token) email_content = "{0}{1}{2}".format( invitation_base_template_header, email_body, invitation_base_template_footer) EmailHelper.send_email(message=email_content, subject='DSPExplorer - Reset Password', receiver_email=profile.user.email) messages.success( request, 'You will receive an email with a link to reset your password!' ) return HttpResponseRedirect(reverse('dashboard:login')) except Profile.DoesNotExist: messages.error(request, 'User not Found.') return HttpResponseRedirect(reverse('dashboard:login')) return render(request, 'dashboard/recover_pwd.html', {})
def post_om_invitation(request): if request.method != 'POST': return not_authorized() try: sender_first_name = request.POST['sender_first_name'].title() sender_last_name = request.POST['sender_last_name'].title() sender_email = request.POST['sender_email'].lower() receiver_first_name = request.POST['receiver_first_name'].title() receiver_last_name = request.POST['receiver_last_name'].title() receiver_email = request.POST['receiver_email'].lower() if sender_first_name == '' or sender_last_name == '' or sender_email == '' or receiver_first_name == '' \ or receiver_last_name == '' or receiver_email == '': return bad_request("Please fill al the fields") if sender_email == receiver_email: return bad_request("Sender and receiver must be different") except KeyError: return bad_request("Please fill al the fields") # sender already a DSP user? try: User.objects.get(email=sender_email) return HttpResponseRedirect('http://openmaker.eu/error_sender/') except User.DoesNotExist: pass # receiver already a DSP user? try: User.objects.get(email=receiver_email) return HttpResponseRedirect('http://openmaker.eu/error_receiver/') except User.DoesNotExist: pass # receiver already invited? try: Invitation.objects.get( receiver_email=HashHelper.md5_hash(receiver_email)) return HttpResponseRedirect('http://openmaker.eu/error_invitation/') except Invitation.DoesNotExist: pass Invitation.create(user=None, sender_email=sender_email, sender_first_name=sender_first_name, sender_last_name=sender_last_name, receiver_first_name=receiver_first_name, receiver_last_name=receiver_last_name, receiver_email=receiver_email, sender_verified=False) activation_link = 'http://{}/om_confirmation/{}/{}/{}/{}/{}/{}'.format( get_current_site(request), sender_first_name.encode('utf-8').encode('base64'), sender_last_name.encode('utf-8').encode('base64'), sender_email.encode('base64'), receiver_first_name.encode('utf-8').encode('base64'), receiver_last_name.encode('utf-8').encode('base64'), receiver_email.encode('base64')) subject = 'OpenMaker Nomination.. almost done!' content = "{}{}{}".format( invitation_base_template_header, invitation_email_confirm.format(SENDER_NAME=sender_first_name, CONFIRMATION_LINK=activation_link), invitation_base_template_footer) EmailHelper.send_email(message=content, subject=subject, receiver_email=sender_email, receiver_name='') return HttpResponseRedirect('http://openmaker.eu/pending_invitation/')
def om_confirmation(request, sender_first_name, sender_last_name, sender_email, receiver_first_name, receiver_last_name, receiver_email): # sender sender_first_name = sender_first_name.decode('base64') sender_last_name = sender_last_name.decode('base64') sender_email = sender_email.decode('base64') # receiver receiver_first_name = receiver_first_name.decode('base64') receiver_last_name = receiver_last_name.decode('base64') receiver_email = receiver_email.decode('base64') try: User.objects.get(email=receiver_email) messages.error(request, 'User is already a DSP member!') return HttpResponseRedirect(reverse('dashboard:dashboard')) except User.DoesNotExist: pass try: invitation = Invitation.objects.get( sender_email=HashHelper.md5_hash(sender_email), receiver_email=HashHelper.md5_hash(receiver_email)) if invitation.sender_verified: messages.error(request, 'Invitation already sent!') else: # invitation flow start invitation.sender_verified = True invitation.save() # sending invitation mail subject = 'OpenMaker Nomination done!' content = "{0}{1}{2}".format( invitation_base_template_header, invitation_email_confirmed.format( ONBOARDING_LINK=request.build_absolute_uri( '/onboarding/')), invitation_base_template_footer) EmailHelper.send_email(message=content, subject=subject, receiver_email=sender_email, receiver_name='') subject = 'You are invited to join the OpenMaker community!' content = "{0}{1}{2}".format( invitation_base_template_header, invitation_email_receiver.format( RECEIVER_FIRST_NAME=receiver_first_name.encode('utf-8'), RECEIVER_LAST_NAME=receiver_last_name.encode('utf-8'), SENDER_FIRST_NAME=sender_first_name.encode('utf-8'), SENDER_LAST_NAME=sender_last_name.encode('utf-8'), ONBOARDING_LINK=request.build_absolute_uri( '/onboarding/')), invitation_base_template_footer) EmailHelper.send_email(message=content, subject=subject, receiver_email=receiver_email, receiver_name='') messages.success(request, 'Invitation complete!') except Invitation.DoesNotExist: messages.error(request, 'Invitation does not exist') return HttpResponseRedirect('http://openmaker.eu/confirmed/')
def onboarding(request): if request.user.is_authenticated: return HttpResponseRedirect(reverse('dashboard:dashboard')) if request.method == 'POST': try: email = request.POST['email'].lower() pasw = request.POST['password'] pasw_confirm = request.POST['password_confirm'] first_name = request.POST['first_name'].title() last_name = request.POST['last_name'].title() gender = request.POST['gender'] birthdate_dt = datetime.strptime(request.POST['birthdate'], '%Y/%m/%d') birthdate_dt = pytz.utc.localize(birthdate_dt) city = request.POST['city'] occupation = request.POST['occupation'] tags = request.POST['tags'] place = request.POST.get( 'place', None) if request.POST.get('place', None) != '{}' else None if tags == '' or tags == None or tags == 'undefined': raise KeyError twitter_username = request.POST.get('twitter', '') except ValueError: messages.error( request, 'Incorrect birthdate format: it must be YYYY/MM/DD') return HttpResponseRedirect(reverse('dashboard:onboarding')) except KeyError: messages.error(request, 'Please fill the required fields!') return HttpResponseRedirect(reverse('dashboard:onboarding')) # check password if pasw != pasw_confirm: messages.error(request, 'Password and confirm password must be the same') return HttpResponseRedirect(reverse('dashboard:onboarding')) # check birthdate if birthdate_dt > pytz.utc.localize( datetime(dt.datetime.now().year - 13, *birthdate_dt.timetuple()[1:-2])): messages.error(request, 'You must be older than thirteen') return HttpResponseRedirect(reverse('dashboard:onboarding')) # Check image and get url imagefile = 'images/profile/default_user_icon.png' # Check if user exist try: User.objects.get(email=email) messages.error(request, 'User is already a DSP member!') return HttpResponseRedirect(reverse('dashboard:onboarding')) except User.DoesNotExist: pass # profile create try: profile = Profile.create(email, first_name, last_name, imagefile, pasw, gender, birthdate_dt, city, occupation, twitter_username, place) except Exception as exc: logging.error( '[PROFILE_CREATION_ERROR] Error during local profile creation for user email: {USER} , EXCEPTION {EXC}' .format(USER=email, EXC=exc)) messages.error(request, 'Error creating user') return HttpResponseRedirect(reverse('dashboard:onboarding')) # Add tags to profile # @TODO : handle tag Creation exception for tag in map( lambda x: re.sub( r'\W', '', x.lower().capitalize(), flags=re.UNICODE), tags.split(",")): tagInstance = Tag.objects.filter(name=tag).first() or Tag.create( name=tag) profile.tags.add(tagInstance) profile.save() # Add twitter username to social links social_links = json.loads(profile.socialLinks) social_links[0]['link'] = twitter_username profile.socialLinks = json.dumps(social_links) profile.save() # send e-mail confirmation_link = request.build_absolute_uri( '/onboarding/confirmation/{TOKEN}'.format( TOKEN=profile.reset_token)) subject = 'Onboarding... almost done!' content = "{0}{1}{2}".format( invitation_base_template_header, onboarding_email_template.format( FIRST_NAME=first_name.encode('utf-8'), LAST_NAME=last_name.encode('utf-8'), CONFIRMATION_LINK=confirmation_link, ), invitation_base_template_footer) EmailHelper.send_email(message=content, subject=subject, receiver_email=email) messages.success(request, 'Confirmation mail sent!') return HttpResponseRedirect(reverse('dashboard:dashboard')) return render( request, 'dashboard/onboarding.html', {'tags': json.dumps(map(lambda x: x.name, Tag.objects.all()))})
def invite(request): if request.method == 'POST': try: address = request.POST['email'].lower() first_name = request.POST['first_name'].title() last_name = request.POST['last_name'].title() except KeyError: messages.error(request, 'Please all the fields are required!') return HttpResponseRedirect(reverse('dashboard:invite')) try: User.objects.get(email=address) messages.error(request, 'User is already a DSP member!') return HttpResponseRedirect(reverse('dashboard:invite')) except User.DoesNotExist: pass try: Invitation.objects.get(receiver_email=HashHelper.md5_hash(address)) messages.error(request, 'User is been already invited!') return HttpResponseRedirect(reverse('dashboard:invite')) except Invitation.DoesNotExist: pass # email not present, filling invitation model try: Invitation.create( user=request.user, sender_email=request.user.email, sender_first_name=request.user.first_name, sender_last_name=request.user.last_name, receiver_first_name=first_name, receiver_last_name=last_name, receiver_email=address, ) subject = 'You are invited to join the OpenMaker community!' content = "{0}{1}{2}".format( invitation_base_template_header, invitation_email_receiver.format( RECEIVER_FIRST_NAME=first_name.encode('utf-8'), RECEIVER_LAST_NAME=last_name.encode('utf-8'), SENDER_FIRST_NAME=request.user.first_name.encode('utf-8'), SENDER_LAST_NAME=request.user.last_name.encode('utf-8'), ONBOARDING_LINK=request.build_absolute_uri( '/onboarding/')), invitation_base_template_footer) EmailHelper.send_email(message=content, subject=subject, receiver_email=address, receiver_name='') messages.success(request, 'Invitation sent!') except EmailAlreadyUsed: messages.error(request, 'User is already a member!') except UserAlreadyInvited: messages.error(request, 'User has already received an invitation!') except Exception as e: print e.message messages.error(request, 'Please try again!') return render(request, 'dashboard/invite.html', {})