Skip to content

Re-send validation email, refactor to APIViews#840

Merged
GergiH merged 17 commits intodevelopfrom
feature/resend-verif-email
Aug 13, 2020
Merged

Re-send validation email, refactor to APIViews#840
GergiH merged 17 commits intodevelopfrom
feature/resend-verif-email

Conversation

@GergiH
Copy link
Copy Markdown
Collaborator

@GergiH GergiH commented Aug 4, 2020

Ref.: #737

Changes

  • Refactored all PublicJsonPostViews and PublicJsonRequestViews to be APIViews instead
  • Added ResendValidation View which requires a username and resends the registration validation email to the user (if it's within a day of the registration, the same as it is handled by the main registration process)

Frontend PR: IFRCGo/go-frontend#1490

Comment thread api/views.py Outdated
Comment thread api/views.py Outdated
Comment on lines +439 to +441
class ResendValidation(PublicJsonPostView):
def handle_post(self, request, *args, **kwargs):
body = json.loads(request.body.decode('utf-8'))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid using PublicJsonPostView and instead try to use DRF for any API views.

Something like this

go-api/api/views.py

Lines 51 to 62 in c1e11c4

class UpdateSubscriptionPreferences(APIView):
authentication_classes = (authentication.TokenAuthentication,)
permissions_classes = (permissions.IsAuthenticated,)
def post(self, request):
errors, created = Subscription.sync_user_subscriptions(self.request.user, request.data, True) # deletePrevious
if len(errors):
return Response({
'status': 400,
'data': 'Could not create one or more subscription(s), aborting'
})
return Response({'data': 'Success'})

Copy link
Copy Markdown
Collaborator Author

@GergiH GergiH Aug 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other similar views use the same pattern, like ShowUsername or RecoverPassword. Should I also change those to use APIView?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need to refactor so that all API views are using DRF views instead of the custom PublicJsonPostView.
It will be cool to follow this for any new addition to the go-api.
We planned to do this right? @batpad @GregoryHorvath

Copy link
Copy Markdown
Collaborator Author

@GergiH GergiH Aug 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thenav56 Since these are public Views we don't need these for the PublicJsonPostView->APIViews, right?

go-api/api/views.py

Lines 52 to 53 in c1e11c4

authentication_classes = (authentication.TokenAuthentication,)
permissions_classes = (permissions.IsAuthenticated,)

The part here is what confusing me, though it doesn't seem that we use this anywhere atm:

go-api/api/views.py

Lines 283 to 286 in c1e11c4

def get_authenticated_user(self, request):
auth_header = request.META.get('HTTP_AUTHORIZATION')
if not auth_header:
return None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GregoryHorvath Yeah we don't need them. The best way will be to just define empty permissions_classses

permissions_classes = []

Basically we should use request.user directly instead of get_authenticated_user, and for any custom logic, we should create a custom authentication class and add it to DRF DEFAULT_AUTHENTICATION_CLASSES. But yeah it isn't used anywhere so we don't need to worry about it.

@GergiH GergiH changed the title Re-send validation email Re-send validation email, refactor to APIViews Aug 11, 2020
Comment thread per/views.py
)
from rest_framework.authtoken.models import Token

def create_draft(raw):
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@batpad @thenav56 Removed most of these unnecessarily abstracted functions. Most of these can be found now inside the Views, ex. this one:
b8b8e9e#diff-8e4a20b18006b7a31a84250b0828251eR182

Comment thread per/views.py
from api.views import bad_request


def get_client_ip(request):
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@batpad @thenav56 No clue if there's a nicer way to achieve this so I just made this a function from Zoltán's code. I don't even know why are we saving IPs here...

Comment thread per/views.py Outdated
except Exception:
return bad_request('Could not change PER form record.')

if hasattr(form,'status_code') and form.status_code == 400:
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@batpad @thenav56 This felt redundant to me since we're already throwing a bad_request above if it fails. Is there any scenario where we might want to keep this in?

Comment thread per/views.py
# Update Form Data of the Form
if data:
try:
with transaction.atomic(): # all or nothing
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@batpad @thenav56 I hope I used with transaction.atomic() right. Also I guess we don't to save half-good Form Data so if one would fail, all should?

Comment thread per/views.py

class DraftSent(PublicJsonPostView):
def handle_post(self, request, *args, **kwargs):
u = None
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@batpad @thenav56 I guessed this part (repeating in most Views below) is just a custom way for doing:

authentication_classes = (authentication.TokenAuthentication,)
permissions_classes = (permissions.IsAuthenticated,)

So I just switched them out.

@GergiH GergiH requested a review from thenav56 August 11, 2020 11:14
Comment thread per/views.py
return form_data

class FormSent(PublicJsonPostView):
def handle_post(self, request, *args, **kwargs):
Copy link
Copy Markdown
Collaborator Author

@GergiH GergiH Aug 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@batpad @thenav56 This didn't need authentication before but I added it now: https://app.circleci.com/pipelines/github/IFRCGo/go-api/613/workflows/4c8458c8-22d6-491e-81e5-7f1e37800d85/jobs/2529

Should I remove the auth from this? It was weird to me why we allowed insertion without auth...

@GergiH
Copy link
Copy Markdown
Collaborator Author

GergiH commented Aug 12, 2020

@batpad @thenav56 Also removed PublicJsonRequestView totally, tested the affected endpoints, all good on my side.

@GergiH GergiH merged commit 0005a6f into develop Aug 13, 2020
@GergiH GergiH deleted the feature/resend-verif-email branch August 13, 2020 14:03
@GergiH GergiH mentioned this pull request Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants