Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contact Us: Modifies api to fetch name and email for logged in users. #922

Merged
merged 10 commits into from
Jun 3, 2017

Conversation

RishabhJain2018
Copy link
Member

fixes #921 . @deshraj Please review the PR.

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

elif request.method == 'GET':
user = User.objects.get(username=request.user)
Copy link
Member

Choose a reason for hiding this comment

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

Whenever using get operation, please make sure that it is properly catched. Same goes for post method.

Copy link
Member Author

Choose a reason for hiding this comment

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

@trojan Thank you for guiding. But I think in this case we don't need to catch it separately as it is already being cached in the try and except case.
The function works as follows:

  1. If the user is logged in:
    a) On get request the username and email is being fetched from the database.
    b) On post request the name, email and message is posted to the database.

  2. If the user is not logged in then name, email and message is posted to the database.

Please review and tell me if I am missing somewhere ?

Copy link
Member

Choose a reason for hiding this comment

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

We would probably remove the try statement from line 41 and make it more specific to the particular statement. also, if try-except block is required for some other piece of code, then you should add a separate try-catch for that statement.

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

elif request.method == 'GET':
user = User.objects.get(username=request.user)
Copy link
Member

Choose a reason for hiding this comment

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

We would probably remove the try statement from line 41 and make it more specific to the particular statement. also, if try-except block is required for some other piece of code, then you should add a separate try-catch for that statement.

@RishabhJain2018
Copy link
Member Author

@deshraj @trojan Please review the PR.

if request.method == 'POST':
request_data = {"name": name, "email": email}
request_data['message'] = request.data['message']
serializer = ContactSerializer(data=request_data)
if serializer.is_valid():
serializer.save()
response_data = {'message': 'Your message has been successfully recorded. We will contact you shortly.'}
Copy link
Member

Choose a reason for hiding this comment

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

Can we make it "We have received your request and will contact you shortly" ?

@@ -35,29 +35,35 @@ def internal_server_error(request):


@throttle_classes([AnonRateThrottle, ])
@api_view(['POST', ])
@api_view(['POST', 'GET'])
Copy link
Member

Choose a reason for hiding this comment

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

Can we make it alphabetically ? GET, POST


if request.method == 'POST':
request_data = {"name": name, "email": email}
request_data['message'] = request.data['message']
Copy link
Member

Choose a reason for hiding this comment

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

Is message mandatory on frontend. If not, then I will prefer to use .get here.

Copy link
Member Author

Choose a reason for hiding this comment

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

@trojan I think we should keep a check on front-end only as it will prevent unnecessary api calls.
What are your views about it?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we should keep a check on the frontend for this. Also, we should throttle this endpoint to 10 requests/minute. Does that sound reasonable to you guys? @trojan @RishabhJain2018 ?

Copy link
Member

Choose a reason for hiding this comment

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

On front end, there should be a check that message is mandatory, because if a user is trying to contact then he or she should definitely leave a message.

Also as far as throttling is concerned, I am not sure of what exactly should be the limit.

Anyways it will be nice if you use .get here.

Copy link
Member Author

Choose a reason for hiding this comment

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

@deshraj Can you please tell why we should change the limit from 100 requests/minute to 10 requests/minute ?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry I missed this part. IMO: a user won't submit the contact form 100 times in a minute. No one will have that many problems.

Copy link
Member

Choose a reason for hiding this comment

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

Anyways, this way we can restrict the users from abusing the system.

Copy link
Member Author

Choose a reason for hiding this comment

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

@deshraj I agree that we can restrict the user from abusing the system.

request_data['message'] = request.data['message']
serializer = ContactSerializer(data=request_data)
except:
serializer = ContactSerializer(data=request.data)
Copy link
Member

Choose a reason for hiding this comment

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

This is redundant. Same thing is happening in POST check one just that data is different. I think it can be easily handled with a simple if condition. @RishabhJain2018

Copy link
Member Author

Choose a reason for hiding this comment

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

@trojan Please explain how can we remove the redundancy ?

Copy link
Member

Choose a reason for hiding this comment

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

I think something like this can. Let me know your concerns if any

user_does_not_exist = False
try:
    user = User.objects.get(username=request.user)
    name = user.username
    email = user.email
    request_data = {'name': name, 'email': email}
except:
    request_data = request.data
    user_does_not_exist = True

if request.method == 'POST' or user_does_not_exist:
    if request.POST.get('message'):
        request_data['message'] = request.POST.get('message')
    serializer = ContactSerializer(data=request_data)
    if serializer.is_valid():
        serializer.save()
        response_data = {'message': 'We have received your request and will contact you shortly.'}
        return Response(response_data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

elif request.method == 'GET':
    response_data = {"name": name, "email": email}
    return Response(response_data, status=status.HTTP_200_OK)

@deshraj
Copy link
Member

deshraj commented May 26, 2017

@RishabhJain2018 any update on this?

@RishabhJain2018
Copy link
Member Author

@deshraj @trojan Please review the PR.

@taranjeet
Copy link
Member

LGTM. Let @deshraj review it once.

@RishabhJain2018
Copy link
Member Author

@deshraj @trojan Please review the PR.

@deshraj
Copy link
Member

deshraj commented Jun 3, 2017

Looks good. Merging this. :)

Good job Rishabh.

@deshraj deshraj merged commit 01581d8 into Cloud-CV:master Jun 3, 2017
@RishabhJain2018 RishabhJain2018 deleted the api branch July 13, 2017 19:34
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.

Modify contact_us api to fetch name and email, if the user is logged in.
3 participants