def get_by_email(cls, sender_email=None, receiver_email=None): q = Q() sender_email and q.add( Q(sender_email=HashHelper.md5_hash(sender_email)) | Q(sender_email=sender_email), q.AND) receiver_email and q.add( Q(receiver_email=HashHelper.md5_hash(receiver_email)) | Q(receiver_email=receiver_email), q.AND) return cls.objects.filter(q).distinct()
def confirm_sender(cls, sender_email, receiver_email): try: cls.__validate(sender_email=sender_email, receiver_email=receiver_email) except UserAlreadyInvited: pass except: raise existent_invitation = cls.objects.filter( sender_email=HashHelper.md5_hash(sender_email), receiver_email=HashHelper.md5_hash(receiver_email) ) if len(existent_invitation): existent_invitation[0].sender_verified = True existent_invitation[0].save() return existent_invitation[0] else: return False
def test8_invitation_debfuscation_first_name_only(self): # assert should deobfuscate first_name only self.reset_invitations() Invitation.deobfuscate_email(self.invitation['sender_email'], self.invitation['sender_first_name']) invitation = Invitation.get_by_email(sender_email=self.invitation['sender_email']) self.assertEqual( invitation[0].sender_first_name+invitation[0].sender_last_name, self.invitation['sender_first_name']+HashHelper.md5_hash(self.invitation['sender_last_name']), 'Should deobfuscate first_name and last_name' )
def create(cls, sender_email, sender_first_name, sender_last_name, receiver_email, receiver_first_name, receiver_last_name, sender_verified=True, user=None): # @TODO: remove when sanitize old user whit no profile from db try: profile = Profile.objects.get(user=user) except Profile.DoesNotExist: profile = None try: cls.__validate(sender_email=sender_email, receiver_email=receiver_email) except: raise # try: # Invitation.objects.get(receiver_email=HashHelper.md5_hash(receiver_email)) # raise UserAlreadyInvited # except Invitation.DoesNotExist: # pass invitation = cls( profile=profile, sender_email=HashHelper.md5_hash(sender_email) if not profile else sender_email, sender_first_name=HashHelper.md5_hash(sender_first_name) if not profile else sender_first_name, sender_last_name=HashHelper.md5_hash(sender_last_name) if not profile else sender_last_name, receiver_first_name=HashHelper.md5_hash(receiver_first_name), receiver_last_name=HashHelper.md5_hash(receiver_last_name), receiver_email=HashHelper.md5_hash(receiver_email), sender_verified=sender_verified ) invitation.save() return invitation
def deobfuscate_email(cls, email, first_name=None, last_name=None): hashed = HashHelper.md5_hash(email) sender_dict = { 'sender_email': email, 'sender_first_name': first_name, 'sender_last_name': last_name } receiver_dict = { 'receiver_email': email, 'receiver_first_name': first_name, 'receiver_last_name': last_name } cls.objects.filter(sender_email=hashed).update(**{k: v for k, v in sender_dict.items() if v is not None})
def create(cls, user, sender_email, sender_first_name, sender_last_name, receiver_email, receiver_first_name, receiver_last_name, sender_verified=True): try: Invitation.objects.get( receiver_email=HashHelper.md5_hash(receiver_email)) raise UserAlreadyInvited except Invitation.DoesNotExist: pass try: user = User.objects.get(email=receiver_email) raise EmailAlreadyUsed except User.DoesNotExist: pass try: profile = Profile.objects.get(user=user) except Profile.DoesNotExist: profile = None invitation = cls( profile=profile, sender_email=HashHelper.md5_hash(sender_email) if not profile else sender_email, sender_first_name=HashHelper.md5_hash(sender_first_name) if not profile else sender_first_name, sender_last_name=HashHelper.md5_hash(sender_last_name) if not profile else sender_last_name, receiver_first_name=HashHelper.md5_hash(receiver_first_name), receiver_last_name=HashHelper.md5_hash(receiver_last_name), receiver_email=HashHelper.md5_hash(receiver_email), sender_verified=sender_verified) invitation.save() return invitation
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 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', {})
def test0_invitation_search_by_email_not_obfuscated(self): # assert should find clear email providing clear email invitation = Invitation.objects.get(sender_email=HashHelper.md5_hash(self.invitation_2['sender_email'])) invitation.sender_email = self.invitation_2['sender_email'] invitation.save() self.assertGreater(len(Invitation.get_by_email(sender_email=self.invitation_2['sender_email'])), 0, 'Cannot find invitatiton by email')