-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #794 from tonioo/change_password_api_523
First draft of a REST API.
- Loading branch information
Showing
25 changed files
with
356 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('admin', '0002_migrate_from_modoboa_admin'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='aliasrecipient', | ||
name='address', | ||
field=models.EmailField(max_length=254), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""Modoboa core viewsets.""" | ||
|
||
from django.http import Http404 | ||
|
||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from . import models | ||
from . import serializers | ||
|
||
|
||
class UserPasswordChangeAPIView(APIView): | ||
|
||
"""A view to update password for user instances.""" | ||
|
||
def put(self, request, pk, format=None): | ||
"""PUT method hander.""" | ||
try: | ||
user = models.User.objects.get(username=pk) | ||
except models.User.DoesNotExist: | ||
raise Http404 | ||
serializer = serializers.UserPasswordSerializer( | ||
user, data=request.data) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response() | ||
return Response( | ||
serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Modoboa core serializers.""" | ||
|
||
from django.core.exceptions import ValidationError | ||
|
||
from rest_framework import serializers | ||
from passwords import validators | ||
|
||
from . import models | ||
|
||
|
||
class UserPasswordSerializer(serializers.ModelSerializer): | ||
|
||
"""A serializer used to change a user password.""" | ||
|
||
new_password = serializers.CharField() | ||
|
||
class Meta: | ||
model = models.User | ||
fields = ( | ||
"password", "new_password", ) | ||
|
||
def validate_password(self, value): | ||
"""Check password.""" | ||
if not self.instance.check_password(value): | ||
raise serializers.ValidationError("Password not correct") | ||
return value | ||
|
||
def validate_new_password(self, value): | ||
"""Check new password.""" | ||
try: | ||
validators.validate_length(value) | ||
validators.complexity(value) | ||
except ValidationError as exc: | ||
raise serializers.ValidationError(exc.messages[0]) | ||
return value | ||
|
||
def update(self, instance, validated_data): | ||
"""Set new password.""" | ||
instance.set_password(validated_data["new_password"]) | ||
instance.save() | ||
return instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{% load i18n form_tags %} | ||
|
||
<h2>{% trans "API access" %} <small>{% trans "Control your access to Modoboa API" %}</small></h2> | ||
<hr> | ||
{% if user.auth_token %} | ||
<div class="panel panel-default"> | ||
<div class="panel-heading"> | ||
<h3 class="panel-title">{% trans "API access token" %}</h3> | ||
</div> | ||
<div class="panel-body"> | ||
{{ user.auth_token }} | ||
</div> | ||
</div> | ||
<br> | ||
{% endif %} | ||
<form class="form-inline" method="POST" action="{% url 'core:user_api_access' %}" | ||
id="api_form"> | ||
{% csrf_token %} | ||
<div class="row"> | ||
<div class="col-sm-2"> | ||
<div class="checkbox"> | ||
<label> | ||
{{ form.enable_api_access }} | ||
{{ form.enable_api_access.label }} | ||
</label> | ||
</div> | ||
</div> | ||
<div class="col-sm-3"> | ||
<button type="submit" class="btn btn-primary" id="update" name="update">{% trans 'Update' %}</button> | ||
</div> | ||
</div> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
{% load i18n form_tags %}{% load url from future %} | ||
{% load i18n form_tags %} | ||
{% load url from future %} | ||
|
||
<h2>{% trans "Profile" %} <small>{% trans "Update your personal information" %}</small></h2> | ||
<hr> | ||
<form class="form-horizontal" method="POST" action="{% url 'core:user_profile' %}" | ||
id="uprefs_form">{% csrf_token %} | ||
id="uprefs_form"> | ||
{% csrf_token %} | ||
{% render_form form %} | ||
<div class="form-actions"> | ||
<button type="submit" class="btn btn-primary col-lg-offset-8 col-sm-offset-7 col-lg-1 col-sm-2" id="update" name="update">{% trans 'Update' %}</button> | ||
<button type="submit" class="btn btn-primary btn-lg col-sm-offset-4 col-sm-4" id="update" name="update">{% trans 'Update' %}</button> | ||
</div> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""Core API urls.""" | ||
|
||
from django.conf.urls import patterns, url | ||
|
||
from . import api | ||
|
||
|
||
urlpatterns = patterns( | ||
"", | ||
url("^users/(?P<pk>.+)/password/$", | ||
api.UserPasswordChangeAPIView.as_view(), | ||
name="user_password_change"), | ||
|
||
) |
Oops, something went wrong.