server/python/django2/renkanmanager/serializers.py
author durandn
Thu, 03 Mar 2016 17:09:27 +0100
changeset 593 2ff785d7026c
parent 592 66243fb5e3fc
child 594 b45eb8244cd9
permissions -rw-r--r--
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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
     1
import json
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
import uuid
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
     3
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
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
     6
from guardian.shortcuts import assign_perm
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
     7
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
     8
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
     9
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")
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
    13
    current_revision_id = serializers.ReadOnlyField(source="current_revision_guid")
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
    14
    workspace_id = serializers.CharField(source="workspace_guid", required=False)
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
    15
    source_revision_id = serializers.CharField(source="source_revision_guid", 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()
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
    17
    created_by = serializers.SlugRelatedField(source="creator", slug_field=settings.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
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    24
    
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
    # 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
    26
    def get_current_revision_last_updator(self, 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
    27
        current_revision = Revision.objects.get(revision_guid = renkan.current_revision_guid)
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
    28
        return getattr(current_revision.last_updated_by, 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
    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):
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    32
        current_revision = Revision.objects.get(revision_guid = renkan.current_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
    33
        return 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
    34
    
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')
592
66243fb5e3fc small corrections on serializers to sort out post_save signals
durandn
parents: 589
diff changeset
    40
        initial_revision = Revision.objects.create(
66243fb5e3fc small corrections on serializers to sort out post_save signals
durandn
parents: 589
diff changeset
    41
            title = validated_data.get('title', 'Untitled renkan'),
66243fb5e3fc small corrections on serializers to sort out post_save signals
durandn
parents: 589
diff changeset
    42
            creator = creator,
66243fb5e3fc small corrections on serializers to sort out post_save signals
durandn
parents: 589
diff changeset
    43
            last_updated_by = creator
66243fb5e3fc small corrections on serializers to sort out post_save signals
durandn
parents: 589
diff changeset
    44
        )
66243fb5e3fc small corrections on serializers to sort out post_save signals
durandn
parents: 589
diff changeset
    45
        renkan = Renkan.objects.create(
66243fb5e3fc small corrections on serializers to sort out post_save signals
durandn
parents: 589
diff changeset
    46
            current_revision_guid = initial_revision.revision_guid,
66243fb5e3fc small corrections on serializers to sort out post_save signals
durandn
parents: 589
diff changeset
    47
            workspace_guid = validated_data.get('workspace_guid', ''),
66243fb5e3fc small corrections on serializers to sort out post_save signals
durandn
parents: 589
diff changeset
    48
            source_revision_guid = validated_data.get('source_revision_guid', ''),
66243fb5e3fc small corrections on serializers to sort out post_save signals
durandn
parents: 589
diff changeset
    49
            creator = creator
66243fb5e3fc small corrections on serializers to sort out post_save signals
durandn
parents: 589
diff changeset
    50
        )
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
        initial_revision.parent_renkan_guid = renkan.renkan_guid
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    52
        initial_revision.content = validated_data.get('content', json.dumps(
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    53
            {
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    54
                "id": str(renkan.renkan_guid),
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    55
                "title": initial_revision.title,
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    56
                "description": "",
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    57
                "created": str(initial_revision.creation_date),
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    58
                "updated": str(initial_revision.modification_date),
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    59
                "edges": [],
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    60
                "nodes": [],
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    61
                "users": [],
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    62
                "space_id": str(renkan.workspace_guid),
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    63
                "views": []
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    64
            }
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    65
        ))
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
        initial_revision.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
    67
        renkan.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
    68
        assign_perm('view_renkan', creator, 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
    69
        assign_perm('change_renkan', creator, 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
    70
        assign_perm('delete_renkan', creator, 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
    71
        assign_perm('view_revision', creator, initial_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
    72
        assign_perm('delete_revision', creator, initial_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
    73
        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
    74
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    75
    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
    76
        """
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
            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
    78
        """
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    79
        updator = validated_data.get('updator')
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    80
        current_revision = Revision.objects.get(revision_guid=renkan.current_revision_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
    81
        if validated_data.get("create_new_revision", False):
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
            revision_to_update = Revision.objects.create()
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
            revision_to_update.parent_renkan_guid = renkan.renkan_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
    84
            revision_to_update.creator = 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
    85
            renkan.current_revision_guid = revision_to_update.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
    86
        else:
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    87
            revision_to_update = current_revision
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    88
        revision_to_update.title = validated_data.get('title', current_revision.title)
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
    89
        revision_to_update.content = validated_data.get('content', current_revision.content)
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
        revision_to_update.last_updated_by = 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
    91
        revision_to_update.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
    92
        if not updator.has_perm('view_revision', revision_to_update):
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    93
            assign_perm('view_revision', updator, revision_to_update)
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    94
            assign_perm('delete_revision', updator, revision_to_update)
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
        renkan.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
    96
        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
    97
    
589
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
    98
    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
    99
        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
   100
            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
   101
        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
   102
    
589
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
   103
    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
   104
        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
   105
            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
   106
        return value
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   107
    
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   108
    def validate_content(self, value):
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   109
        
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   110
        try:
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   111
            json.loads(value)
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   112
        except ValueError:
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   113
            raise serializers.ValidationError("Content format is not a JSON-serializable")
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   114
        loaded_json = json.loads(value)
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   115
            
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   116
        if (not "nodes" in loaded_json):
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   117
            raise serializers.ValidationError("Content requires a 'nodes' entry")
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   118
        if (not "edges" in loaded_json):
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   119
            raise serializers.ValidationError("Content requires a 'edges' entry")
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   120
        if (not "views" in loaded_json):
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   121
            raise serializers.ValidationError("Content requires a 'views' entry")
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   122
            
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   123
        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
   124
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   125
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
class RevisionSerializer(serializers.Serializer):
589
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
   127
    id = serializers.ReadOnlyField(source="revision_guid")
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
   128
    parent_renkan_id = serializers.ReadOnlyField(source="parent_renkan_guid")
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
   129
    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
   130
    title = serializers.ReadOnlyField()
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   131
    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
   132
    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
   133
    renkan_creation_date = serializers.SerializerMethodField("get_related_renkan_creation_date")
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
   134
    revision_created_by = serializers.SlugRelatedField(source="creator", slug_field=settings.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
   135
    revision_last_updated_by = serializers.StringRelatedField(source="last_updated_by")
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
    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
   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 get_related_workspace_guid(self, revision):
589
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
   139
        parent_renkan = Renkan.objects.get(renkan_guid = revision.parent_renkan_guid)
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
   140
        return parent_renkan.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
   141
    
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
   142
    def get_related_renkan_creator(self, revision):
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
   143
        parent_renkan = Renkan.objects.get(renkan_guid = revision.parent_renkan_guid)
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
        return getattr(parent_renkan.creator, 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
   145
    
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
   146
    def get_related_renkan_creation_date(self, revision):
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
   147
        parent_renkan = Renkan.objects.get(renkan_guid = revision.parent_renkan_guid)
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
   148
        return 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
   149
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   150
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
class WorkspaceSerializer(serializers.Serializer):
589
0ae11aa255a3 Moved python2/django into a django2 folder for consistency + clarified serializers fields name
durandn
parents: 588
diff changeset
   152
    id = serializers.ReadOnlyField(source="workspace_guid")
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
   153
    workspace_created_by = serializers.SlugRelatedField(source="creator", slug_field=settings.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
   154
    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
   155
    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
   156
    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
   157
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   158
    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
   159
        creator = validated_data.get('creator')
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
        workspace = Workspace.objects.create()
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   161
        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
   162
        workspace.creator = creator
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.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
   164
        assign_perm('view_workspace', creator, 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
   165
        assign_perm('change_workspace', creator, 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
   166
        assign_perm('delete_workspace', creator, 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
   167
        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
   168
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   169
    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
   170
        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
   171
        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
   172
        return workspace