1
0
Fork 0

Add REST Token app

merge-requests/78/head
Nikos Roussos 2014-12-03 18:22:59 -08:00
parent 191c21eedc
commit 215b26ae80
5 changed files with 50 additions and 12 deletions

View File

@ -8,4 +8,4 @@ class StationForm(forms.ModelForm):
class Meta:
model = Station
fields = ['name', 'image', 'alt',
'lat', 'lng', 'antenna', 'online']
'lat', 'lng', 'antenna', 'online']

View File

@ -39,13 +39,15 @@ class Common(Configuration):
THIRD_PARTY_APPS = (
'crispy_forms', # Form layouts
'avatar', # for user avatars
'rest_framework'
'rest_framework',
'rest_framework.authtoken'
)
# Apps specific for this project go here.
LOCAL_APPS = (
'users', # custom users app
'users',
'base',
'api',
'django_extensions',
)
@ -183,7 +185,7 @@ class Common(Configuration):
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'
# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = (
join(BASE_DIR, 'static'),
)
@ -266,9 +268,10 @@ class Common(Configuration):
# END LOGGING CONFIGURATION
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
]
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
)
}

View File

@ -15,7 +15,8 @@
<div class="pull-right edit-profile-buttons">
<a class="btn btn-primary" href="{% url 'users:update' %}">My Info</a>
<a class="btn btn-primary" href="{% url 'account_email' %}">E-Mail</a>
<a class="btn btn-info" href="{% url 'avatar_change' %}">Avatar</a>
<a class="btn btn-primary" href="{% url 'avatar_change' %}">Avatar</a>
<a class="btn btn-info" data-toggle="modal" data-target="#APIModal" href="#">API Key</a>
</div>
{% endif %}
</h2>
@ -133,9 +134,29 @@
</div>
<!-- API Modal -->
<div class="modal fade" id="APIModal" tabindex="-1" role="dialog" aria-labelledby="APIModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="APIModalLabel">API Key</h4>
</div>
<div class="modal-body">
<div>You can use this token to interact with the API.</div>
<div>
<code>{{ token }}</code>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Station Modal -->
<div class="modal fade" id="StationModal" tabindex="-1" role="dialog" aria-labelledby="StationModal" aria-hidden="true">
<div class="modal fade" id="StationModal" tabindex="-1" role="dialog" aria-labelledby="StationModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">

View File

@ -1,7 +1,15 @@
# -*- coding: utf-8 -*-
from rest_framework.authtoken.models import Token
from django.contrib.auth.models import AbstractUser
from django.core.validators import MaxLengthValidator
from django.db import models
from django.db.models.signals import post_save
def gen_token(sender, instance, created, **kwargs):
token = Token.objects.get(user=instance)
if not token:
Token.objects.crete(user=instance)
class User(AbstractUser):
@ -11,3 +19,5 @@ class User(AbstractUser):
def __unicode__(self):
return self.username
post_save.connect(gen_token, sender=User)

View File

@ -8,6 +8,8 @@ from django.views.generic import ListView
from braces.views import LoginRequiredMixin
from rest_framework.authtoken.models import Token
from .forms import UserForm
from .models import User
from base.forms import StationForm
@ -54,6 +56,7 @@ def view_user(request, username):
user = User.objects.get(username=username)
observations = Observation.objects.filter(author=user)[0:10]
stations = Station.objects.filter(owner=user)
token = Token.objects.get(user=user)
form = StationForm()
if request.method == 'POST':
form = StationForm(request.POST, request.FILES)
@ -69,4 +72,5 @@ def view_user(request, username):
{'user': user,
'observations': observations,
'stations': stations,
'token': token,
'form': form})