server/python/django2/renkanmanager/serializers.py
author durandn
Tue, 03 May 2016 14:32:28 +0200
changeset 610 b9edc1c1538a
parent 608 8fd40139827c
child 611 f0f07e2b841f
permissions -rw-r--r--
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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
     1
import json, 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
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
     6
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
     7
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
     8
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
     9
RENKAN_USER_DISPLAY_FIELD = getattr(settings, "RENKAN_USER_DISPLAY_FIELD", get_user_model().USERNAME_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
    10
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
class RenkanSerializer(serializers.Serializer):
589
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
    12
    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
    13
    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
    14
    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
    15
    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
    16
    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
    17
    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
    18
    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
    19
    title = serializers.CharField(required=False)
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    20
    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
    21
    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
    22
    modification_date = serializers.SerializerMethodField("get_current_revision_modification_date")
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
    create_new_revision = serializers.BooleanField(write_only=True, required=False) # only used for updating
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
    24
    validation_timestamp = serializers.CharField(write_only=True, required=False) # only used for updating is a content json is not provided
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
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    26
    # ADD ERROR HANDLING
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    27
    def get_current_revision_last_updator(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
    28
        return getattr(renkan.current_revision.last_updated_by, 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
    29
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    30
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    31
    def get_current_revision_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
    32
        return renkan.current_revision.modification_date
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
    33
    
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
    34
    @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
    35
    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
    36
        """
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    37
            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
    38
        """
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    39
        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
    40
        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
    41
        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
    42
        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
    43
            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
    44
                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
    45
                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
    46
                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
    47
                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
    48
                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
    49
            )
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
    50
        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
    51
            raise serializers.ValidationError(ve.args[0])
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
    52
    
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
    53
    @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
    54
    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
    55
        """
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    56
            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
    57
        """
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    58
        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
    59
        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
    60
        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
    61
        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
    62
        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
    63
            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
    64
            if not 'validation_timestamp' in validated_data:
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
                raise serializers.ValidationError("No validation timestamp and no content json to extract it from")
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
    66
            validation_timestamp = validated_data.get('validation_timestamp')
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
    67
        else:
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
    68
            validation_timestamp = json.loads(content).get("updated", "")
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
    69
        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
    70
            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
    71
            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
    72
            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
    73
        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
    74
            renkan.save_renkan(updator=updator, timestamp=validation_timestamp, title=title, content=content, create_new_revision=create_new_revision)
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
        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
    76
            raise serializers.ValidationError(ve.args[0])
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
    77
        return renkan
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    78
    
589
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
    79
    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
    80
        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
    81
            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
    82
        return value
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    83
    
589
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
    84
    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
    85
        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
    86
            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
    87
        return value
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    88
    
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
    89
    def validate_content(self, value):  
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    90
        try:
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    91
            json.loads(value)
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    92
        except ValueError:
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    93
            raise serializers.ValidationError("Content format is not a JSON-serializable")
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    94
        loaded_json = json.loads(value)
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    95
            
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    96
        if (not "nodes" in loaded_json):
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    97
            raise serializers.ValidationError("Content requires a 'nodes' entry")
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    98
        if (not "edges" in loaded_json):
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    99
            raise serializers.ValidationError("Content requires a 'edges' entry")
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   100
        if (not "views" in loaded_json):
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   101
            raise serializers.ValidationError("Content requires a 'views' entry")
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   102
            
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   103
        return 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
   104
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   105
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   106
class RevisionSerializer(serializers.Serializer):
589
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
   107
    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
   108
    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
   109
    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
   110
    title = serializers.ReadOnlyField()
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   111
    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
   112
    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
   113
    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
   114
    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
   115
    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
   116
    revision_modification_date = serializers.ReadOnlyField(source="modification_date")
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   117
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   118
    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
   119
        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
   120
            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
   121
        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
   122
            return ''
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
   123
    
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
   124
    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
   125
        return getattr(revision.parent_renkan.creator, 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
   126
    
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
   127
    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
   128
        return revision.parent_renkan.creation_date
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
   129
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   130
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   131
class WorkspaceSerializer(serializers.Serializer):
589
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
   132
    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
   133
    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
   134
    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
   135
    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
   136
    title = serializers.CharField()
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   137
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   138
    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
   139
        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
   140
        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
   141
        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
   142
        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
   143
        return workspace
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   144
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   145
    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
   146
        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
   147
        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
   148
        return workspace