Repository: Cloud-CV/EvalAI
Issue: Use Decorator to check permission in Disable Challenge API
Root cause: Architectural Constraint

Currently for the disable challenge API, there is an explicit condition added to check if the user is creator of the challenge or not (https://github.com/Cloud-CV/EvalAI/blob/master/apps/challenges/views.py#L160). This has to be removed and the `isChallengeCreator` decorator has to be added to this view. 

The decorator can be found here https://github.com/Cloud-CV/EvalAI/blob/master/apps/challenges/permissions.py#L6

I'll work on this.

Yeah, please go ahead. I cannot assign you right now since you are not a contributor yet. I will add you once you have made small contributions. :) 

Please let me know if you need some help. 

Whoops. Seems like someone forgot to commit the 0004 migration file in the `jobs` app. I am getting a dependency error when running the Django migration.

@varunagrawal can you please share the screenshot of the error? 

Sure thing. Here you go: 
![evalai_migration](https://cloud.githubusercontent.com/assets/975964/24340295/054d2798-127f-11e7-9901-42e7dc52fe09.png)

I see the problem. If you look at `challenges/migrations/0019_added_leaderboard_data_table.py`, it references a migration `('jobs', '0004_submission_output'),` as a dependency which does not exist in the repo.
@deshraj your commit from 2 months ago created 0019 migration. I'd look at that part again. :blush:

Also, quick question: You've referenced the "decorator" in the issue as [this](https://github.com/Cloud-CV/EvalAI/blob/master/apps/challenges/permissions.py#L6), so I am assuming the idea here is to add an actual decorator function that behaves in the same way as the `has_permission` method?

>Also, quick question: You've referenced the "decorator" in the issue as this, so I am assuming the idea here is to add an actual decorator function that behaves in the same way as the has_permission method?

Yeah, the idea is same as you mentioned. But, you might want to test this on your local system after making the changes. :) 

> I see the problem. If you look at challenges/migrations/0019_added_leaderboard_data_table.py, it references a migration ('jobs', '0004_submission_output'), as a dependency which does not exist in the repo.

I tried to reproduce the error on my local machine by cloning the repository again but I didn't get the error. Can you please share the OS name and share the screenshot of the full error log? 


> I tried to reproduce the error on my local machine by cloning the repository again but I didn't get the error. Can you please share the OS and share the screenshot of the full error log?

That's super strange. I don't see the migration file `0004_submission_output.py` in the `jobs` directory at all. Check out the screenshot below:

![evalai_jobs](https://cloud.githubusercontent.com/assets/975964/24340578/5c04a640-1281-11e7-940c-efd37ab301ef.png)

I'm pasting the error message below. I'm on Ubuntu 16.04.

```
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/core/management/base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 83, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/db/migrations/loader.py", line 52, in __init__
    self.build_graph()
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/db/migrations/loader.py", line 268, in build_graph
    raise exc
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/db/migrations/loader.py", line 238, in build_graph
    self.graph.validate_consistency()
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/db/migrations/graph.py", line 261, in validate_consistency
    [n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)]
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/db/migrations/graph.py", line 261, in <listcomp>
    [n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)]
  File "/home/varun/projects/CloudCV/evalai/venv/lib/python3.6/site-packages/django/db/migrations/graph.py", line 104, in raise_error
    raise NodeNotFoundError(self.error_message, self.key, origin=self.origin)
django.db.migrations.exceptions.NodeNotFoundError: Migration challenges.0019_added_leaderboard_data_table dependencies reference nonexistent parent node ('jobs', '0004_submission_output')
```

@varunagrawal I think this is most probably due to different version of Postgres. We have merged the migrations a while back and hence the dependency on `0004_submission_output.py` should not appear again. 

I am using `psql (PostgreSQL) 9.6.1` and it is working fine on my machine. 

I'm on 9.6.2. I don't think a revision version change should have this much of an effect.

Also, https://github.com/Cloud-CV/EvalAI/blob/master/apps/challenges/migrations/0019_added_leaderboard_data_table.py#L13 still points to the missing migration file. I am not sure I understand what you mean by merged the migrations.

I get that the migration is dependent on a previous migration that is now merged into a single migration. For reference, see this https://github.com/Cloud-CV/EvalAI/commit/bd17b39936990513fc758b5bf40e9063a06ae2ba. 

To solve this problem on your machine, you might want to delete all the migrations of the `challenges` app and then it should work fine. 

Let me know if that doesn't work. 

Ahh I see. The fix is super simple then. I can simply replace the `0004_submission_output` migration dependency with `0001_squashed_0005_upload_unique_random_filename`.

And indeed it works! I'll add it to my PR.

Question related to the task at hand:
In my decorator, I need to get the ID of the challenge creator which requires querying the DB for the challenge. Then again in the wrapped function there is the same DB query, and thus repetition of code. Is that fine or would you prefer something more elegant?

Alright. I seem to be done, but another stumbling block in terms of testing. When I try to host a new challenge, I get  a message saying that the feature does not exist. Should I just go ahead and create a challenge from the django console?
Sorry if this is a noob question.

Great. :) 

For creating a challenge, you can create a challenge from Django admin, That would be much easier than using the django console. Else if you are interested in using the code, then feel free to look at the existing test setup for creating a challenge at this link https://github.com/Cloud-CV/EvalAI/blob/master/tests/unit/challenges/test_views.py#L24. 

I am working on the challenge creation feature as of now and it should be ready soon. :)