#225 Disable sending emails to Sender themselves
Merged 4 years ago by jflory7. Opened 4 years ago by alishapapun.
fedora-commops/ alishapapun/fedora-happiness-packets stop-email-to-youself  into  master

@@ -11,6 +11,7 @@ 

  from django.urls import reverse

  from django.db.models import Q

  from django.utils import timezone

+ from email_normalize import normalize

  

  from .models import Message, strip_email

  
@@ -61,14 +62,31 @@ 

              Submit('submit', 'Send some happiness', css_class='btn-lg centered'),

          )

  

+     def is_recipient_email_equals_sender_email(self):

+         recipient_email = self.cleaned_data.get('recipient_email')

+         sender_email = self.user.email

+         sender_username = self.user.username

+         normalized_sender_email = normalize(sender_email)

+         normalized_recipient_email = normalize(recipient_email)

+         #Fedora assigned email to the Sender

+         sender_fedora_email = sender_username + '@fedoraproject.org'

+         if normalized_recipient_email in (

+             sender_email, sender_fedora_email, normalized_sender_email):

+             return True

+         else:

+             return False

+             

      def clean(self):

          super(MessageSendForm, self).clean()

+         isREEqualsSE = self.is_recipient_email_equals_sender_email()

          # if self.cleaned_data.get('hp'):

          #     raise forms.ValidationError('')

          if self.cleaned_data.get('sender_approved_public_named') and not self.cleaned_data.get('sender_approved_public'):

              self.add_error('sender_approved_public_named', "If you want us to publish the message including your names, "

                                                             "you must also check 'I agree to publish this message and"

                                                             "display it publicly in the Happiness Archive'")

+         if isREEqualsSE:

+             raise forms.ValidationError("You cannot send a Fedora Happiness Packet to yourself!")

          validate_email(self.user.email)

  

  class MessageRecipientForm(forms.ModelForm):

@@ -227,7 +227,7 @@ 

      @staticmethod

      def fasidCheck(request):

          try:

-             fas = AccountSystem(username= settings.ADMIN_USERNAME, password= settings.ADMIN_PASSWORD)

+             fas = AccountSystem(username=settings.ADMIN_USERNAME, password=settings.ADMIN_PASSWORD)

              fasid = request.GET['fasid']

              is_server_error = 'False'

              type_of_error = ' No Error '
@@ -235,7 +235,7 @@ 

              u_name = 'No name'

              u_email = 'No email'

              if not person:

-                 logger.error("The FAS username doesnot exsist!")

+                 logger.error("The FAS username does not exist!")

                  account_exists = 'No'

              else:

                  account_exists = 'Yes'

file modified
+3
@@ -45,5 +45,8 @@ 

  #python-fedora for f-a-s API

  python-fedora==0.10.0

  

+ #Dependency for normalizing emails

+ email-normalize==0.2.1

+ 

  # Dependency for YAML file

  pyyaml==5.1

Metadata Update from @jflory7:
- Pull-request tagged with: improvement, needs testing, type - backend, type - summer coding
- Request assigned

4 years ago

Please change the function name to adhere to Python PEP-8 standards. Function names should be lowercase, with words separated by underscores as necessary to improve readability. More specifics explained on python.org.

Please change the function name to adhere to Python PEP-8 standards. Function names should be lowercase, with words separated by underscores as necessary to improve readability. More specifics explained on python.org.

Minor, minor typo: doesnot to does not

I think this error is not clear since a recipient can be anyone, not only the sender. I suggest this edit instead: You cannot send a Fedora Happiness Packet to yourself!

The Python PEP-8 standard suggests maximum line length of 79 characters (python.org). While minor, it does make PRs in the Pagure web UI easier to read / write since in-line comments extend off-screen. You can delete the commented line and shorten the following line in this way to adhere to PEP-8:

        if normalized_recipient_email in (
                sender_email, sender_fedora_email, normalized_sender_email):

Thanks @alishapapun, great work! :tada: I tested this locally and it worked as expected when I tried to send a packet to myself. I left a few comments with minor change requests. When you get a chance to get to this feedback, we can merge this. :smiley: Thanks for working on this.

Metadata Update from @jflory7:
- Pull-request untagged with: needs testing
- Pull-request tagged with: needs changes

4 years ago

rebased onto 6fe957f

4 years ago

Hey, @jflory7 I made the required changes and squashed the commits to 1. Do let me know if any further changes are required. Cheers :smile:

Metadata Update from @jflory7:
- Pull-request untagged with: needs changes

4 years ago

Thanks @alishapapun, the changes look good to me. :smile: Thanks for addressing the feedback. Merging! :clapper:

Pull-Request has been merged by jflory7

4 years ago