| author | ymh <ymh.work@gmail.com> |
| Tue, 21 Jun 2016 17:53:06 +0200 | |
| changeset 618 | 3051b847c124 |
| parent 615 | f3875fbe206a |
| child 619 | d427a6db1902 |
| permissions | -rw-r--r-- |
|
611
f0f07e2b841f
serverside: translation + timestamp handling + logging
durandn
parents:
610
diff
changeset
|
1 |
import json, logging, uuid, datetime, time |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
2 |
|
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
3 |
from django.db import transaction |
|
593
2ff785d7026c
corrected tests and serializer so they can support custom user model, added a readme (to be expanded) with configuration information for the field renkan api will use to render user information
durandn
parents:
592
diff
changeset
|
4 |
from django.contrib.auth import get_user_model |
|
2ff785d7026c
corrected tests and serializer so they can support custom user model, added a readme (to be expanded) with configuration information for the field renkan api will use to render user information
durandn
parents:
592
diff
changeset
|
5 |
from django.conf import settings |
|
618
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
6 |
from django.utils import six |
|
611
f0f07e2b841f
serverside: translation + timestamp handling + logging
durandn
parents:
610
diff
changeset
|
7 |
from django.core.exceptions import ValidationError |
|
615
f3875fbe206a
redo foreign key for renkanmanager, optimize property access, correct unit tests
ymh <ymh.work@gmail.com>
parents:
613
diff
changeset
|
8 |
from renkanmanager import settings as renkan_settings |
|
588
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
9 |
from renkanmanager.models import Renkan, Workspace, Revision |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
10 |
from rest_framework import serializers |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
11 |
|
|
611
f0f07e2b841f
serverside: translation + timestamp handling + logging
durandn
parents:
610
diff
changeset
|
12 |
logger = logging.getLogger(__name__) |
|
615
f3875fbe206a
redo foreign key for renkanmanager, optimize property access, correct unit tests
ymh <ymh.work@gmail.com>
parents:
613
diff
changeset
|
13 |
RENKAN_USER_DISPLAY_FIELD = renkan_settings.RENKAN_USER_DISPLAY_FIELD |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
14 |
|
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
15 |
class RenkanSerializer(serializers.Serializer): |
|
589
0ae11aa255a3
Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents:
588
diff
changeset
|
16 |
id = serializers.ReadOnlyField(source="renkan_guid") |
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
17 |
current_revision_id = serializers.SlugRelatedField(source="current_revision", slug_field="revision_guid", read_only=True) |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
18 |
workspace_id = serializers.SlugRelatedField(source="workspace", slug_field="workspace_guid", queryset=Workspace.objects, required=False) |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
19 |
source_revision_id = serializers.SlugRelatedField(source="source_revision", slug_field="revision_guid", queryset=Revision.objects, required=False) |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
20 |
revision_count = serializers.ReadOnlyField() |
|
594
b45eb8244cd9
Added default to the RENKAN_USER_DISPLAY_FIELD setting. Will default to the USERFIELD_NAME for the user model
durandn
parents:
593
diff
changeset
|
21 |
created_by = serializers.SlugRelatedField(source="creator", slug_field=RENKAN_USER_DISPLAY_FIELD, read_only=True) |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
22 |
last_updated_by = serializers.SerializerMethodField("get_current_revision_last_updator") |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
23 |
title = serializers.CharField(required=False) |
|
588
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
24 |
content = serializers.JSONField(required=False) |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
25 |
creation_date = serializers.ReadOnlyField() |
|
618
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
26 |
modification_date = serializers.SerializerMethodField() |
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
27 |
|
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
28 |
def __init__(self, instance=None, data=serializers.empty, **kwargs): |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
29 |
|
|
618
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
30 |
self.validation_timestamp = kwargs.pop('validation_timestamp', None) |
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
31 |
super(RenkanSerializer, self).__init__(instance, data, **kwargs) |
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
32 |
|
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
33 |
|
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
34 |
def get_current_revision_last_updator(self, renkan): |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
35 |
last_updator = renkan.current_revision.last_updated_by |
|
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
36 |
if last_updator: |
|
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
37 |
return getattr(last_updator, RENKAN_USER_DISPLAY_FIELD) |
|
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
38 |
else: |
|
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
39 |
return '' |
|
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
40 |
|
|
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
41 |
|
|
618
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
42 |
def get_modification_date(self, renkan): |
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
43 |
return renkan.current_revision.modification_date |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
44 |
|
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
45 |
@transaction.atomic |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
46 |
def create(self, validated_data): |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
47 |
""" |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
48 |
Method to create a new Renkan (and its first revision) |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
49 |
""" |
|
615
f3875fbe206a
redo foreign key for renkanmanager, optimize property access, correct unit tests
ymh <ymh.work@gmail.com>
parents:
613
diff
changeset
|
50 |
logger.debug("RENKANSERIALIZER %r", validated_data) |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
51 |
creator = validated_data.get('creator') |
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
52 |
workspace_obj = validated_data.get('workspace', None) |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
53 |
source_revision_obj = validated_data.get('source_revision', None) |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
54 |
try: |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
55 |
return Renkan.objects.create_renkan( |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
56 |
creator=creator, |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
57 |
title=validated_data.get('title', ''), |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
58 |
content=validated_data.get('content', ''), |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
59 |
workspace=workspace_obj, |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
60 |
source_revision=source_revision_obj |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
61 |
) |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
62 |
except ValidationError as ve: |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
63 |
raise serializers.ValidationError(ve.args[0]) |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
64 |
|
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
65 |
@transaction.atomic |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
66 |
def update(self, renkan, validated_data): |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
67 |
""" |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
68 |
Method to update a Renkan object. Creates a new revision if needed |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
69 |
""" |
|
615
f3875fbe206a
redo foreign key for renkanmanager, optimize property access, correct unit tests
ymh <ymh.work@gmail.com>
parents:
613
diff
changeset
|
70 |
logger.debug('RENKAN SERIALIZER UPDATE %r', validated_data) |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
71 |
updator = validated_data.get('updator') |
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
72 |
create_new_revision = validated_data.get("create_new_revision", False) |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
73 |
title = validated_data.get('title', renkan.current_revision.title) |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
74 |
content = validated_data.get('content', '') |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
75 |
if not content: |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
76 |
content = renkan.current_revision.content |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
77 |
if title != json.loads(content).get("title", ""): |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
78 |
content_dict = json.loads(content) |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
79 |
content_dict["title"] = title |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
80 |
content = json.dumps(content_dict) |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
81 |
try: |
|
618
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
82 |
renkan.save_renkan(updator=updator, timestamp=self.validation_timestamp, title=title, content=content, create_new_revision=create_new_revision) |
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
83 |
except ValidationError as ve: |
|
611
f0f07e2b841f
serverside: translation + timestamp handling + logging
durandn
parents:
610
diff
changeset
|
84 |
raise serializers.ValidationError(str(ve.args[0])) |
|
615
f3875fbe206a
redo foreign key for renkanmanager, optimize property access, correct unit tests
ymh <ymh.work@gmail.com>
parents:
613
diff
changeset
|
85 |
# FORCE Renkan reload. |
|
f3875fbe206a
redo foreign key for renkanmanager, optimize property access, correct unit tests
ymh <ymh.work@gmail.com>
parents:
613
diff
changeset
|
86 |
# TODO: How to avoid the reload ??? |
|
f3875fbe206a
redo foreign key for renkanmanager, optimize property access, correct unit tests
ymh <ymh.work@gmail.com>
parents:
613
diff
changeset
|
87 |
return Renkan.objects.get(id=renkan.id) |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
88 |
|
|
589
0ae11aa255a3
Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents:
588
diff
changeset
|
89 |
def validate_workspace_id(self, value): |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
90 |
if self.instance is not None: |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
91 |
raise serializers.ValidationError("You cannot update workspace_guid") |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
92 |
return value |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
93 |
|
|
589
0ae11aa255a3
Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents:
588
diff
changeset
|
94 |
def validate_source_revision_id(self, value): |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
95 |
if self.instance is not None: |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
96 |
raise serializers.ValidationError("You cannot update source_revision_guid") |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
97 |
return value |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
98 |
|
|
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
99 |
def validate_content(self, value): |
|
588
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
100 |
try: |
|
611
f0f07e2b841f
serverside: translation + timestamp handling + logging
durandn
parents:
610
diff
changeset
|
101 |
loaded_json = json.loads(value) |
|
588
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
102 |
except ValueError: |
|
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
103 |
raise serializers.ValidationError("Content format is not a JSON-serializable") |
|
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
104 |
if (not "nodes" in loaded_json): |
|
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
105 |
raise serializers.ValidationError("Content requires a 'nodes' entry") |
|
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
106 |
if (not "edges" in loaded_json): |
|
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
107 |
raise serializers.ValidationError("Content requires a 'edges' entry") |
|
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
108 |
if (not "views" in loaded_json): |
|
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
109 |
raise serializers.ValidationError("Content requires a 'views' entry") |
|
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
110 |
return value |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
111 |
|
|
618
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
112 |
def validate(self, data): |
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
113 |
if self.instance: |
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
114 |
validation_timestamp = self.validation_timestamp or data.get('content', {}).get('updated') |
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
115 |
if not validation_timestamp: |
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
116 |
raise serializers.ValidationError("No validation timestamp and no content json to extract it from") |
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
117 |
validation_timestamp = dateparse.parse_datetime(validation_timestamp) if isinstance(validation_timestamp, six.string_types) else validation_timestamp |
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
118 |
if validation_timestamp != self.instance.current_revision.modification_date: |
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
119 |
raise serializers.ValidationError("Invalid timestamp was provided") |
|
3051b847c124
validation_timestamp and create_new_revision are not part of the data. move them to query parameters
ymh <ymh.work@gmail.com>
parents:
615
diff
changeset
|
120 |
return data |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
121 |
|
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
122 |
|
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
123 |
class RevisionSerializer(serializers.Serializer): |
|
589
0ae11aa255a3
Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents:
588
diff
changeset
|
124 |
id = serializers.ReadOnlyField(source="revision_guid") |
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
125 |
parent_renkan_id = serializers.SlugRelatedField(source="parent_renkan", slug_field='renkan_guid', read_only=True) |
|
589
0ae11aa255a3
Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents:
588
diff
changeset
|
126 |
workspace_id = serializers.SerializerMethodField("get_related_workspace_guid") |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
127 |
title = serializers.ReadOnlyField() |
|
588
95536fa18d0d
Minor adjustements to properly interact with Renkan client
durandn
parents:
587
diff
changeset
|
128 |
content = serializers.JSONField(read_only=True) |
|
593
2ff785d7026c
corrected tests and serializer so they can support custom user model, added a readme (to be expanded) with configuration information for the field renkan api will use to render user information
durandn
parents:
592
diff
changeset
|
129 |
renkan_created_by = serializers.SerializerMethodField("get_related_renkan_creator") |
|
2ff785d7026c
corrected tests and serializer so they can support custom user model, added a readme (to be expanded) with configuration information for the field renkan api will use to render user information
durandn
parents:
592
diff
changeset
|
130 |
renkan_creation_date = serializers.SerializerMethodField("get_related_renkan_creation_date") |
|
594
b45eb8244cd9
Added default to the RENKAN_USER_DISPLAY_FIELD setting. Will default to the USERFIELD_NAME for the user model
durandn
parents:
593
diff
changeset
|
131 |
revision_created_by = serializers.SlugRelatedField(source="creator", slug_field=RENKAN_USER_DISPLAY_FIELD, read_only=True) |
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
132 |
revision_last_updated_by = serializers.SlugRelatedField(source="last_updated_by", slug_field=RENKAN_USER_DISPLAY_FIELD, read_only=True) |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
133 |
revision_modification_date = serializers.ReadOnlyField(source="modification_date") |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
134 |
|
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
135 |
def get_related_workspace_guid(self, revision): |
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
136 |
if revision.parent_renkan.workspace: |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
137 |
return revision.parent_renkan.workspace.workspace_guid |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
138 |
else: |
|
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
139 |
return '' |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
140 |
|
|
593
2ff785d7026c
corrected tests and serializer so they can support custom user model, added a readme (to be expanded) with configuration information for the field renkan api will use to render user information
durandn
parents:
592
diff
changeset
|
141 |
def get_related_renkan_creator(self, revision): |
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
142 |
return getattr(revision.parent_renkan.creator, RENKAN_USER_DISPLAY_FIELD) |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
143 |
|
|
593
2ff785d7026c
corrected tests and serializer so they can support custom user model, added a readme (to be expanded) with configuration information for the field renkan api will use to render user information
durandn
parents:
592
diff
changeset
|
144 |
def get_related_renkan_creation_date(self, revision): |
|
610
b9edc1c1538a
reworked views and serializers to pass and take into account the "validation timestamp" to the model + refactored models to have a content validation method + adapted tests
durandn
parents:
608
diff
changeset
|
145 |
return revision.parent_renkan.creation_date |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
146 |
|
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
147 |
|
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
148 |
class WorkspaceSerializer(serializers.Serializer): |
|
589
0ae11aa255a3
Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents:
588
diff
changeset
|
149 |
id = serializers.ReadOnlyField(source="workspace_guid") |
|
594
b45eb8244cd9
Added default to the RENKAN_USER_DISPLAY_FIELD setting. Will default to the USERFIELD_NAME for the user model
durandn
parents:
593
diff
changeset
|
150 |
workspace_created_by = serializers.SlugRelatedField(source="creator", slug_field=RENKAN_USER_DISPLAY_FIELD, read_only=True) |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
151 |
creation_date = serializers.ReadOnlyField() |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
152 |
renkan_count = serializers.ReadOnlyField() |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
153 |
title = serializers.CharField() |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
154 |
|
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
155 |
def create(self, validated_data): |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
156 |
creator = validated_data.get('creator') |
|
608
8fd40139827c
permission implementation has to be client-side and not in the generic api package + deleted duplicate migration
durandn
parents:
594
diff
changeset
|
157 |
title = validated_data.get('title', '') |
|
8fd40139827c
permission implementation has to be client-side and not in the generic api package + deleted duplicate migration
durandn
parents:
594
diff
changeset
|
158 |
workspace = Workspace.objects.create(creator=creator, title=title) |
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
159 |
workspace.save() |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
160 |
return workspace |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
161 |
|
|
587
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
162 |
def update(self, workspace, validated_data): |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
163 |
workspace.title = validated_data.get('title', '') |
|
fb0041aa74d3
transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff
changeset
|
164 |
workspace.save() |
|
613
e00a24b711a0
make renkanmanager test run locally
ymh <ymh.work@gmail.com>
parents:
611
diff
changeset
|
165 |
return workspace |