Repository: Cloud-CV/EvalAI
Issue: Invite user to participant team api should be idempotent.
GitHub labels: enhancement

GSOC-2019

medium-difficulty

backend

`invite_participant_to_team` creates a Participant object. If this api is called N number of times for a single email id, N participant objects will be created.

Later on this api will involve sending a mail to the concerned participant, which implies that this can be called N number of times(email not received due to network failure or some error).

So ideal behaviour for this api should be create Participant object once and from next time return that created object. 

Django has a function called [get_or_create](https://docs.djangoproject.com/en/1.10/ref/models/querysets/#get-or-create) which can be used here. Hook will go in `save` of the concerned serialiser

@deshraj : what say?

Yeah, cool. Sounds like a plan. :+1: 

is this assigned ? @TroJan 

@gauthamzz : yeah you can work on it. thanks :)

1. Im facing some issue testing the module. im getting an error  ` "detail": "Authentication credentials were not provided." ` when using the http://127.0.0.1:8000/api/participants/participant_team 
2. the url should be changed to api/participants/ participant_team/(?P<participant_team_pk>[0-9]+)/invite/(?P<participant_pk>[0-9]+)$ [name='invite_participant_to_team'] as we need both team id and participant id .. am i correct ? 


@gauthamzz : Lets solve your issue step by step. Taking Step 1 into consideration

If you see the error message, it indicates that you are trying to access an api without providing authentication credentials. So lets us first see from where this message is coming from

If you see [here](https://github.com/Cloud-CV/EvalAI/blob/master/apps/participants/views.py#L92), then it says `TokenAuthentication` as `authentication_classes`. Error message is due this check.
Now comes how to get rid of this error message. You will need to hit this api with a valid user token. The token can be generated by you and used as a Header.

To generate token, first of you will need to find your user id(open Django admin and look into __Users__ table under __Authentication and Authorization__). Afterwards you can use this snippet to get the token

```
from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
u=User.objects.get(id=<your_user_id>)
token = Token.objects.create(user=u)
print token.key
```

You can run this snippet by firing up a django shell like

```
python manage.py shell --settings=settings.dev
```

Afterwards you use the following curl to check this api

```
curl -X POST -H "Authorization: Token <your_token_here>" "http://localhost:8000/api/participants/participant_team/1/invite"
```

Let me know once you are clear for your first point

Thanks I finished the first step. I was thinking since we need to take the email of the participant also.so after we create a participant object (for this we need to take participant pk from urls.py) .Am I going on the wrong track ?

@gauthamzz : great for the first part.
Let me explain why we dont need participant id. 

We are inviting user by email. If you see the serializer [code](https://github.com/Cloud-CV/EvalAI/blob/master/apps/participants/serializers.py#L46) we are creating participant object there only. The created participated object has its status as `Accepted`. Later on we plan to add verification here.

What you are trying to say will involve a major change in the existing flow where in we will create Participant object for each user and then for inviting anyone, this participant id will be used. but this is actually not acceptable because participant here signifies participation of a user against a challenge. It will be created only when the user is being invited or is creating a team.

so your change will be in the save function of serializer. i hope this helps.

Close this ? Thanks for the help @TroJan I got confused 

Closing #271