Skip to content

Commit

Permalink
Add barebones token data retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
madprime committed Aug 23, 2017
1 parent 96d365c commit 01d0710
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 8 deletions.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
release: python manage.py migrate
web: gunicorn oh_proj_management.wsgi --log-file -
6 changes: 3 additions & 3 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "Django Sample",
"description": "A barebones Django app created with startproject",
"name": "Open Humans Project Management",
"description": "A web app to view & work with your Open Humans project",
"repository": "https://github.com/OpenHumans/oh-proj-management",
"keywords": ["django"],
"env": {
"SECRET_KEY": {
"description": "A secret key for verifying the integrity of signed cookies.",
"generator": "secret"
},
"HEROKU_DEPLOY": {
"ON_HEROKU": {
"description": "Indicate if the deployment is occurring on Heroku.",
"value": "true"
}
Expand Down
9 changes: 8 additions & 1 deletion oh_proj_management/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import os

import dj_database_url


def to_bool(string, default):
"""Convert os environment variable to boolean."""
Expand All @@ -36,7 +38,7 @@ def to_bool(string, default):
DEBUG = to_bool(os.getenv('DEBUG'), default=False)

# Enable any host for Heroku deployments.
if to_bool(os.getenv('HEROKU_DEPLOY'), False):
if to_bool(os.getenv('ON_HEROKU'), False):
ALLOWED_HOSTS = ['*']
else:
ALLOWED_HOSTS = []
Expand Down Expand Up @@ -100,6 +102,11 @@ def to_bool(string, default):
}
}

# Set up Heroku database.
if to_bool(os.getenv('ON_HEROKU'), False):
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
Expand Down
21 changes: 20 additions & 1 deletion oh_proj_management/templates/oh_proj_management/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,24 @@

<h1>Hello world.</h1>

<b>Master access token:</b> {{ master_access_token }}
<b>Member data:</b>
<table class="table">
<tr>
<th>Proj member ID</th>
<th>Files</th>
</tr>
{% for member in member_data.results %}
<tr>
<td>{{ member.project_member_id }}</td>
<td>
{% for item in member.data %}
<p>{{ item.source }}: {{ item.basename }}</p>
{% endfor %}
</td>
</tr>
{% endfor %}
</table>
{% for item in member_data.results %}
<p>{{ item }}</p>
{% endfor %}
{% endblock main_container %}
2 changes: 1 addition & 1 deletion oh_proj_management/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^/?$', HomeView.as_view(), name='home'),
url(r'^$', HomeView.as_view(), name='home'),
url(r'^login/?$', LoginView.as_view(), name='login'),
]
25 changes: 23 additions & 2 deletions oh_proj_management/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import requests

from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.views.generic import FormView, TemplateView
Expand All @@ -9,17 +12,35 @@ class HomeView(TemplateView):
template_name = "oh_proj_management/home.html"

def get(self, request, *args, **kwargs):
token = None
self.member_data = None

if 'master_access_token' in request.session:
token = request.session['master_access_token']
self.member_data = self.token_for_memberlist(token)
if not self.member_data:
del request.session['master_access_token']

if self.member_data:
return super().get(request, *args, **kwargs)
else:
return redirect('login')

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['master_access_token'] = self.request.session[
'master_access_token']
context['member_data'] = self.member_data
return context

def token_for_memberlist(self, token):
req_url = ('https://www.openhumans.org/api/direct-sharing/project/'
'members/?access_token={}'.format(token))
req = requests.get(req_url)
if req.status_code == 200:
return req.json()
else:
messages.error('Token not valid. Maybe a fresh one is needed?')
return None


class LoginView(FormView):
template_name = 'oh_proj_management/login.html'
Expand Down
2 changes: 2 additions & 0 deletions oh_proj_management/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import os

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "oh_proj_management.settings")

application = get_wsgi_application()
application = DjangoWhiteNoise(application)
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
certifi==2017.7.27.1
chardet==3.0.4
dj-database-url==0.4.2
Django==1.11.4
gunicorn==19.7.1
idna==2.6
psycopg2==2.7.3
pytz==2017.2
requests==2.18.4
urllib3==1.22
whitenoise==3.3.0

0 comments on commit 01d0710

Please sign in to comment.