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

Upgrade Django and Python #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[source]]
url = "https://pypi.org/simple"
name = "pypi"
verify_ssl = true

[dev-packages]

[requires]
python_version = "3.6"

[packages]
django = "*"
django-heroku = "*"
celery = "*"
arrow = "*"
requests = "*"
gunicorn = "*"
193 changes: 193 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 36 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,63 @@ https://dataxfer-ns-oh.herokuapp.com/

### Local Setup and Development

To develop this tool, you can run this app locally by following the instructions below or by using the Heroku-CLI.

#### 1. Creating a project on Open Humans
#### Creating a project on Open Humans

First you will need an Open Humans project to test and develop on. You can set up a project here: https://www.openhumans.org/direct-sharing/projects/oauth2/create/

The `Enrollment URL` will be a link to your final app page (http://yourapp.herokuapp.com) once development is complete.
The `Enrollment URL` would be a link to a final app page (http://yourapp.herokuapp.com) once development is complete. You can pick or use a dummy name in the meantime.

Set the `Redirect URL` to exactly: http://127.0.0.1:5000/complete/
Set the `Redirect URL` to exactly: `http://127.0.0.1:5000/complete`

Once created, you can go to https://www.openhumans.org/direct-sharing/projects/manage/ and click on the project name. You will find your `client_id` and`client_secret` here.

For more information on OAuth2 setup, go to: https://www.openhumans.org/direct-sharing/oauth2-setup/

#### 2. Install Dependencies

A number of software depenencies are required to develop locally with foreman. You can install them using the following commands:

```sudo apt-get install rabbitmq-server
sudo apt-get install python-dev
sudo apt-get install libpq-dev
sudo gem install foreman
sudo apt-get install virtualenv
```

#### 3. Configure Local Environment Variables

Download the repository and then copy the enviornment variable file using `cp env.example .env`

Open the `.env` file and replace `CLIENT_ID` and `CLIENT_SECRET` with your project's matching ID and SECRET.

#### 4. Setup Local Virtual Environment

Use `virtualenv venv` to create a new virtual environment file.
#### Installation and configuration

Enter the virtual environment with `source venv/bin/activate`.
These instructions expect you to be using the following. Make sure it's set
up for your system first!

Install the requirements using `pip install -r requirements.txt`.
* `pipenv` for local Python development (`brew install pipenv` on Mac)
* the Heroku CLI: https://devcenter.heroku.com/articles/heroku-cli (`brew install heroku/brew/heroku` on Mac)
* Make sure to create a Heroku account if you don't have one, and log in.

Finally run `python manage.py migrate`.

Local setup:

#### 5. Start Local Server
1. Download the git repository, e.g. using `git clone`.
2. Navigate to be inside the repository base directory. Check also to make sure you're on the correct branch, in case you're using a dev branch instead of master.
3. Start a local pipenv shell with `pipenv shell`
4. Run `pipenv install` to install any needed dependencies automatically.
5. Copy the environment variable file using `cp env.example .env`
6. Open the `.env` file (`vi .env`) and edit to replace `CLIENT_ID` and `CLIENT_SECRET` with your project's matching ID and SECRET (see above)
7. Initialize the database locally with `heroku local:run python manage.py migrate`

Startup your local server in the virtual environment by typing `foreman start`.
#### Running the app locally

Your local site can be loaded by opening a web browser and visiting http://127.0.0.1:5000/
Inside the repository base, run: `heroku local`.

`Ctrl-C` to quit foreman and `deactivate` to exit the virtual environment.
The app should be available in a local web browser at: http://127.0.0.1:5000 (if you have amqp; if not, you'll get a 500 Server error. Move on to Heroku deployment below. )

### Deployment to Heroku

Create a new app in Heroku, and link it to your own repository or use the Heroku-CLI to upload files to the heroku server.

Once you have your heroku app setup, go to https://www.openhumans.org/direct-sharing/projects/manage/ and click on the `edit` button next to your project name. Set the `Redirect URL` to exactly: https://your-app-name.herokuapp.com/complete/

Next you will need to add the following local enviornment variables in the Config Vars section of your heroku app:

`APP_BASE_URL`: Set this to exactly https://your-app-name.herokuapp.com

`OH_ACTIVITY_PAGE`: https://www.openhumans.org/activity/your-project-name

`OH_CLIENT_ID`: Your Client ID

`OH_CLIENT_SECRET`: Your Client Secret

`HEROKU_APP`: true
Create a new app in Heroku, and link it to your own repository (connect to Github first inside Heroku) or use the Heroku-CLI to upload files to the heroku server.

`SECRET_KEY` : your_secret_key_here (see http://www.howtogeek.com/howto/30184/10-ways-to-generate-a-random-password-from-the-command-line/)
#### Config in Open Humans

`PYTHONUNBUFFERED` : true
Once you have your heroku app created, go to https://www.openhumans.org/direct-sharing/projects/manage/ and click on the `edit` button next to your project name. Set the `Redirect URL` to exactly: `https://your-app-name.herokuapp.com/complete`

On the Resources tab for your app, edit the Celery Worker to be active. After this, add a CloudAMQP add-on and use the "Little Lemur" version.
#### Config in Heroku

1. You should add the following local environment variables in the Config Vars section of your heroku app:
* `APP_BASE_URL`: Set this to exactly https://your-app-name.herokuapp.com
* `OH_ACTIVITY_PAGE` (optional): https://www.openhumans.org/activity/your-project-name
* `OH_CLIENT_ID`: Your Client ID
* `OH_CLIENT_SECRET`: Your Client Secret
* `SECRET_KEY` : Something long and random
* `PYTHONUNBUFFERED` : true
2. Deploy your git repository to git (e.g. `git push heroku master`. Note you may need to set the remote, and do a `git pull`, before you can push.)
3. On the Resources tab for your app, edit the Celery Worker to be active. (This may take some time to appear.)
4. On the same Resources tab, add a CloudAMQP add-on and use the "Little Lemur" version.
5. Initialize your database with `heroku run 'python manage.py migrate'`

Setup is now complete.
Setup is now complete and the Heroku app should hopefully work!
9 changes: 6 additions & 3 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
# When this webapp is run with foreman, variables defined in `.env` are
# loaded as environment variables.

# Optional, but you probably want this for logging and debugging.
PYTHONUNBUFFERED='true'

# DEBUG is off unless this is set to true.
DEBUG='True'

# Django secret. Long and random, e.g.:
# http://www.howtogeek.com/howto/30184/10-ways-to-generate-a-random-password-from-the-command-line/
SECRET_KEY='secret_key_here'

# DEBUG is True by default. Set to 'False' to turn it off (e.g. in production).
# DEBUG='False'

# Open Humans OAuth2 project settings.
OH_ACTIVITY_PAGE='https://www.openhumans.org/activity/your-project-name-should-be-here/'
OH_CLIENT_ID='client_id_here'
Expand Down
2 changes: 1 addition & 1 deletion oh_data_source/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class OpenHumansMember(models.Model):

A User account is created for this Open Humans member.
"""
user = models.OneToOneField(User)
user = models.OneToOneField(User, on_delete=models.CASCADE)
oh_id = models.CharField(max_length=16, primary_key=True, unique=True)
access_token = models.CharField(max_length=256)
refresh_token = models.CharField(max_length=256)
Expand Down
Loading