Skip to content
This repository has been archived by the owner on Feb 21, 2020. It is now read-only.

Commit

Permalink
Allowing anonimous API calls for login/register
Browse files Browse the repository at this point in the history
  • Loading branch information
danigm committed Sep 2, 2014
1 parent 6dd9aa4 commit 6303c52
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions agora_site/agora_core/resources/user.py
Expand Up @@ -175,11 +175,11 @@ def prepend_urls(self):
url(r"^(?P<resource_name>%s)/register%s$" \
% (self._meta.resource_name, trailing_slash()),
self.wrap_form(form_class=APISignupForm,
method="POST"), name="api_user_register"),
method="POST", anon=True), name="api_user_register"),

url(r"^(?P<resource_name>%s)/login%s$" % (self._meta.resource_name,
trailing_slash()), self.wrap_form(
form_class=LoginForm, method="POST"),
form_class=LoginForm, method="POST", anon=True),
name="api_username_login"),

url(r"^(?P<resource_name>%s)/logout%s$" % (self._meta.resource_name,
Expand Down
11 changes: 7 additions & 4 deletions agora_site/misc/generic_resource.py
Expand Up @@ -51,7 +51,7 @@ def raise_error(self, request, http_method, data):
return http_method(serialized,
content_type=build_content_type(desired_format))

def wrap_form(self, form_class, method="POST", raw=False):
def wrap_form(self, form_class, method="POST", raw=False, anon=False):
"""
Creates a view for a given form class, which calls to is_valid()
and save() when needed. You can get the form args reimplementing
Expand All @@ -60,7 +60,8 @@ def wrap_form(self, form_class, method="POST", raw=False):
"""
@csrf_exempt
def wrapper(request, *args, **kwargs):
self.is_authenticated(request)
if not anon:
self.is_authenticated(request)
try:
desired_format = self.determine_format(request)
if method == "POST" or method== "PUT":
Expand Down Expand Up @@ -193,7 +194,7 @@ def api_field_from_django_field(cls, f, default=fields.CharField):
# we can make sure its GenericResourceMixin.api_field_from_django_field is
# used
class GenericResource(GenericResourceMixin, ModelResource):
def wrap_view(self, view):
def wrap_view(self, view, anon=False):
"""
Adds the authentication call to every view
"""
Expand All @@ -204,7 +205,9 @@ def wrap(request, *args, **kwargs):
return wrap

wrapper = super(GenericResource, self).wrap_view(view)
wrapper = authenticated(wrapper)

if not anon:
wrapper = authenticated(wrapper)

return wrapper

Expand Down

0 comments on commit 6303c52

Please sign in to comment.