From 26bce8af7f8851304b19d7fe07958aae521fa5ce Mon Sep 17 00:00:00 2001 From: Nikolai R Kristiansen Date: Wed, 7 Jan 2015 03:11:35 +0100 Subject: [PATCH] Passwords, TODO++ --- README.md | 6 ++++ dpdapi/migrations/0002_auto_20150107_0309.py | 30 ++++++++++++++++++++ dpdapi/models.py | 4 +-- dpdapi/serializers.py | 17 +++++++++-- dpdapisite/urls.py | 2 +- requirements.txt | 2 +- 6 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 dpdapi/migrations/0002_auto_20150107_0309.py diff --git a/README.md b/README.md index e69de29..5aabb42 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,6 @@ +# TODO +- Fabfile with deploy task +- Alias should only allow unique (source,destination) tuples + +# TODO later +- Create domain dir for dovecot? \ No newline at end of file diff --git a/dpdapi/migrations/0002_auto_20150107_0309.py b/dpdapi/migrations/0002_auto_20150107_0309.py new file mode 100644 index 0000000..c4d3ce4 --- /dev/null +++ b/dpdapi/migrations/0002_auto_20150107_0309.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('dpdapi', '0001_initial'), + ] + + operations = [ + migrations.AlterModelOptions( + name='alias', + options={'verbose_name_plural': 'aliases'}, + ), + migrations.AlterField( + model_name='domain', + name='name', + field=models.CharField(unique=True, max_length=1000), + preserve_default=True, + ), + migrations.AlterField( + model_name='user', + name='email', + field=models.EmailField(unique=True, max_length=75), + preserve_default=True, + ), + ] diff --git a/dpdapi/models.py b/dpdapi/models.py index a53b521..81e9287 100644 --- a/dpdapi/models.py +++ b/dpdapi/models.py @@ -24,14 +24,14 @@ class Alias(BaseModel): class Domain(BaseModel): - name = models.CharField(max_length=1000) + name = models.CharField(max_length=1000, unique=True) def __unicode__(self): return self.name class User(BaseModel): - email = models.EmailField() + email = models.EmailField(unique=True) password = models.CharField(max_length=109) domain = models.ForeignKey('dpdapi.Domain') diff --git a/dpdapi/serializers.py b/dpdapi/serializers.py index f42d661..9539041 100644 --- a/dpdapi/serializers.py +++ b/dpdapi/serializers.py @@ -1,5 +1,5 @@ from rest_framework import serializers - +from passlib.hash import sha512_crypt from .models import Alias, Domain, User @@ -21,4 +21,17 @@ class DomainSerializer(serializers.ModelSerializer): class UserSerializer(serializers.ModelSerializer): class Meta: - model = User \ No newline at end of file + model = User + extra_kwargs = {'password': {'write_only': True}} + + def _hash_password(self, raw_password): + return sha512_crypt.encrypt(raw_password) + + def create(self, validated_data): + user = User( + email=validated_data['email'], + domain=validated_data['domain'], + password=self._hash_password(validated_data['password']) + ) + user.save() + return user diff --git a/dpdapisite/urls.py b/dpdapisite/urls.py index 386e07f..f4165e8 100644 --- a/dpdapisite/urls.py +++ b/dpdapisite/urls.py @@ -4,8 +4,8 @@ from django.views.generic import RedirectView urlpatterns = patterns( '', - url(r'^api/', include('dpdapi.urls')), url(r'^$', RedirectView.as_view(pattern_name='api-root')), + url(r'^api/', include('dpdapi.urls')), url(r'^admin/', include(admin.site.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) ) diff --git a/requirements.txt b/requirements.txt index 105d518..aad785b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ Django>=1.7,<1.8 django-extensions==1.4.9 django-filter==0.9.1 djangorestframework>=3.0,<3.1 - +passlib==1.6.2