Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First attempt at add/get user

  • Loading branch information...
commit f0b4ad49249fa903416588041974223b5857a049 1 parent 17b2512
Buzzi authored
Showing with 55 additions and 3 deletions.
  1. +9 −0 api/forms.py
  2. +3 −1 api/urls.py
  3. +28 −2 api/views.py
  4. +15 −0 utils/decorators.py
View
9 api/forms.py
@@ -1,12 +1,21 @@
from django import forms
+from django.auth.models import User
from app import models
+
+class UserForm(forms.ModelForm):
+ class Meta:
+ model = User
+ exclude = ("is_superuser","is_staff","last_login","groups","user_permissions","password","date_joined","is_active",)
+
+
class CommentForm(forms.ModelForm):
class Meta:
model = models.Comment
exclude = ('created_at',)
+
class CommentRatingForm(forms.ModelForm):
class Meta:
model = models.Rating
View
4 api/urls.py
@@ -6,7 +6,9 @@
url(r'^comments/?$', Comments.as_view(), name="comments"),
url(r'^comments/(?P<comment_id>\d+)/?$', Comment.as_view(), name="comment"),
url(r'^comments/(?P<comment_id>\d+)/rate/?$', login_required(CommentRating.as_view()), name="comment-ratings"),
-
+
url(r'^organizations/?$', Organizations.as_view(), name="organizations"),
url(r'^organizations/(?P<organization_id>\d+)/?$', Organization.as_view(), name="organization"),
+
+ url(r'^user/?$', User.as_view(), name="user"),
)
View
30 api/views.py
@@ -1,9 +1,10 @@
+from django.forms.models import model_to_dict
from django.shortcuts import get_object_or_404
from django.views.generic import View
from django.utils import simplejson
-from utils.decorators import as_json, authorized_user
-from utils.http import HttpResponseBadRequest, HttpResponseNoContent
+from utils.decorators import as_json, authorized_user, unauthorized_user
+from utils.http import HttpResponseBadRequest, HttpResponseNoContent, HttpResponseUnauthorized
from app import models
from api import forms
@@ -108,3 +109,28 @@ def get(self, request, organization_id):
organization = get_object_or_404(models.Organization, id=organization_id)
return organization.to_dict()
+
+
+class User(View):
+ @authorized_user
+ @as_json
+ def get(self, request):
+ return model_to_dict(request.user, exclude=(
+ "is_superuser","is_staff","last_login","groups","user_permissions","password","date_joined","is_active",
+ ))
+
+ @unauthorized_user
+ @as_json
+ def post(self, request):
+ """
+ Adds a new user
+ """
+ data = simplejson.loads(request.read())
+
+ form = forms.UserForm(data)
+
+ if form.is_valid():
+ form.save()
+ return {"id": form.instance.id}
+
+ return form.errors.copy(), HttpResponseBadRequest
View
15 utils/decorators.py
@@ -62,4 +62,19 @@ def _wrapped_view(clss, request, *args, **kwargs):
return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view)
+ return decorator(view)
+
+def unauthorized_user(view):
+ """
+ Ensure that the user is NOT authenticated (ONLY USE INSIDE CLASS VIEW)
+ """
+ def decorator(view_func):
+ def _wrapped_view(clss, request, *args, **kwargs):
+ if not request.user.is_authenticated():
+ return view_func(clss, request, *args, **kwargs)
+
+ return HttpResponseUnauthorized()
+
+ return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view)
+
return decorator(view)
Please sign in to comment.
Something went wrong with that request. Please try again.