Browse Source

simplify api

pull/342/head
Guillaume Vincent 8 years ago
parent
commit
ca80f8348e
4 changed files with 17 additions and 20 deletions
  1. +4
    -3
      api/migrations/0001_initial.py
  2. +3
    -2
      api/models.py
  3. +6
    -4
      api/serializers.py
  4. +4
    -11
      api/tests/tests_entries.py

+ 4
- 3
api/migrations/0001_initial.py View File

@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2016-02-04 21:50
# Generated by Django 1.9.5 on 2016-04-06 10:11
from __future__ import unicode_literals

from django.conf import settings
@@ -27,7 +27,7 @@ class Migration(migrations.Migration):
('is_admin', models.BooleanField(default=False)),
],
options={
'abstract': False,
'verbose_name_plural': 'Users',
},
),
migrations.CreateModel(
@@ -36,7 +36,8 @@ class Migration(migrations.Migration):
('created', models.DateTimeField(auto_now_add=True, verbose_name='created')),
('modified', models.DateTimeField(auto_now=True, verbose_name='modified')),
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('site', models.CharField(max_length=255)),
('login', models.CharField(default='', max_length=255)),
('site', models.CharField(default='', max_length=255)),
('title', models.CharField(blank=True, max_length=255, null=True)),
('username', models.CharField(blank=True, max_length=255, null=True)),
('email', models.EmailField(blank=True, max_length=254, null=True)),


+ 3
- 2
api/models.py View File

@@ -81,7 +81,8 @@ class PasswordInfo(models.Model):
class Entry(DateMixin):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(LessPassUser, on_delete=models.CASCADE, related_name='entries')
site = models.CharField(max_length=255)
login = models.CharField(max_length=255, default='')
site = models.CharField(max_length=255, default='')
password = models.ForeignKey(PasswordInfo)

title = models.CharField(max_length=255, null=True, blank=True)
@@ -94,4 +95,4 @@ class Entry(DateMixin):
verbose_name_plural = "Entries"

def __str__(self):
return self.title
return str(self.id)

+ 6
- 4
api/serializers.py View File

@@ -30,11 +30,13 @@ class PasswordInfoSerializer(serializers.ModelSerializer):


class EntrySerializer(serializers.ModelSerializer):
site = serializers.CharField(allow_blank=True)
login = serializers.CharField(allow_blank=True)
password = PasswordInfoSerializer()

class Meta:
model = models.Entry
fields = ('id', 'site', 'password', 'title', 'username', 'email', 'description', 'url', 'created', 'modified')
fields = ('id', 'site', 'login', 'password', 'created', 'modified')
read_only_fields = ('created', 'modified')

def create(self, validated_data):
@@ -45,10 +47,10 @@ class EntrySerializer(serializers.ModelSerializer):

def update(self, instance, validated_data):
password_data = validated_data.pop('password')
passwordInfo = instance.password
password_info = instance.password
for attr, value in password_data.items():
setattr(passwordInfo, attr, value)
passwordInfo.save()
setattr(password_info, attr, value)
password_info.save()

for attr, value in validated_data.items():
setattr(instance, attr, value)


+ 4
- 11
api/tests/tests_entries.py View File

@@ -50,6 +50,7 @@ class LoginApiTestCase(APITestCase):
def test_create_entry(self):
entry = {
"site": "twitter",
"login": "guillaume@oslab.fr",
"password": {
"counter": 1,
"settings": [
@@ -60,11 +61,6 @@ class LoginApiTestCase(APITestCase):
],
"length": 12
},
"title": "twitter",
"username": "guillaume20100",
"email": "guillaume@oslab.fr",
"description": "",
"url": "https://twitter.com/"
}
self.assertEqual(0, models.Entry.objects.count())
self.assertEqual(0, models.PasswordInfo.objects.count())
@@ -77,6 +73,7 @@ class LoginApiTestCase(APITestCase):
self.assertNotEqual('facebook', entry.site)
new_entry = {
"site": "facebook",
"login": "",
"password": {
"counter": 1,
"settings": [
@@ -86,13 +83,9 @@ class LoginApiTestCase(APITestCase):
],
"length": 12
},
"title": "facebook",
"username": "",
"email": "",
"description": "",
"url": "https://facebook.com/"
}
self.client.put('/api/entries/%s/' % entry.id, new_entry)
request = self.client.put('/api/entries/%s/' % entry.id, new_entry)
self.assertEqual(200, request.status_code, request.content.decode('utf-8'))
entry_updated = models.Entry.objects.get(id=entry.id)
self.assertEqual('facebook', entry_updated.site)
self.assertEqual(3, len(json.loads(entry_updated.password.settings)))


Loading…
Cancel
Save