server/python/django2/renkanmanager/models.py
author durandn
Wed, 27 Apr 2016 16:36:30 +0200
changeset 609 854a027c80ff
parent 589 0ae11aa255a3
child 610 b9edc1c1538a
permissions -rw-r--r--
models refactoring to use ForeignKey fields + associated migrations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
     1
'''
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
Created on Jul 17, 2014
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
Reworked in December, 2015
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
     4
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
     5
@author: tc, nd
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
'''
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
     7
import uuid, logging, json, datetime
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 django.conf import settings
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
     9
from django.db import models, transaction
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    10
from django.core.exceptions import ValidationError
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    11
from django.utils import timezone, dateparse
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
    12
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    13
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    14
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    15
logger = logging.getLogger(__name__)
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
auth_user_model = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    17
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
class Workspace(models.Model):
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
    
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
    workspace_guid = models.CharField(max_length=1024, default=uuid.uuid4, unique=True, blank=False, null=False) # typically 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
    21
    title = models.CharField(max_length=1024, null=True)
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
    creator = models.ForeignKey(auth_user_model, blank=True, null=True, related_name="workspace_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
    23
    creation_date = models.DateTimeField(auto_now_add=True)
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
    @property
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 renkan_count(self):
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    27
        return Renkan.objects.filter(workspace__workspace_guid=self.workspace_guid).count()
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
    28
    
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
    class Meta:
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
        app_label = 'renkanmanager'
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
        permissions = (
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
            ('view_workspace', 'Can view 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
    33
        )
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    34
        
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    35
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    36
class RenkanManager(models.Manager):
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    37
    
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    38
    @transaction.atomic
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    39
    def create_renkan(self, creator, title='', content='', source_revision=None, workspace = None):
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    40
        new_renkan = Renkan()
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    41
        new_renkan.creator = creator
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    42
        new_renkan_workspace_guid = ""
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    43
        new_renkan_title = title
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    44
        new_renkan_content = content
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    45
        if workspace is not None:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    46
            new_renkan.workspace = workspace
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    47
            new_renkan_workspace_guid = workspace.workspace_guid
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    48
        if source_revision is not None:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    49
            new_renkan.source_revision = source_revision
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    50
            if not title:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    51
                new_renkan_title = source_revision.title
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    52
            new_renkan_content = source_revision.content
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    53
        new_renkan.save()
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    54
        initial_revision = Revision(parent_renkan=new_renkan)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    55
        initial_revision.modification_date = timezone.now()
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    56
        initial_revision.creator = creator
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    57
        initial_revision.last_updated_by = creator
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    58
        initial_revision.save() # saving once to set the creation date as we need it to fill the json content
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    59
        if initial_revision.modification_date != initial_revision.creation_date:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    60
            initial_revision.modification_date = initial_revision.creation_date
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    61
        initial_revision.title = new_renkan_title if new_renkan_title else "Untitled Renkan"
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    62
        if new_renkan_content:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    63
            try:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    64
                new_renkan_content_dict = json.loads(new_renkan_content)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    65
            except ValueError:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    66
                raise ValidationError("Provided content to create Renkan is not a JSON-serializable")
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    67
            new_renkan_content_dict["created"] = str(initial_revision.creation_date)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    68
            new_renkan_content_dict["updated"] = str(initial_revision.modification_date)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    69
        else: 
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    70
            new_renkan_content_dict = {
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    71
                "id": str(new_renkan.renkan_guid),
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    72
                "title": initial_revision.title,
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    73
                "description": "",
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    74
                "created": str(initial_revision.creation_date),
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    75
                "updated": str(initial_revision.modification_date),
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    76
                "edges": [],
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    77
                "nodes": [],
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    78
                "users": [],
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    79
                "space_id": new_renkan_workspace_guid,
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    80
                "views": []
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    81
            }
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    82
        initial_revision.content = json.dumps(new_renkan_content_dict)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    83
        initial_revision.save()
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    84
        return new_renkan
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    85
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
    86
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
class Renkan(models.Model):
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    88
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
    89
    renkan_guid = models.CharField(max_length=256, default=uuid.uuid4, unique=True, blank=False, null=False) # typically UUID
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    90
    workspace = models.ForeignKey('Workspace', null=True, blank=True, to_field='workspace_guid')
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    91
    source_revision = models.ForeignKey('Revision', null=True, blank=True, related_name="renkan_source_revision", to_field='revision_guid', on_delete=models.SET_NULL)
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
    92
    creator = models.ForeignKey(auth_user_model, blank=True, null=True, related_name="renkan_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
    93
    creation_date = models.DateTimeField(auto_now_add=True)
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
    state = models.IntegerField(default=1)
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
    
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    96
    objects = RenkanManager()
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
    97
    
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
    98
    @property
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
    def revision_count(self):
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   100
        return Revision.objects.filter(parent_renkan__renkan_guid=self.renkan_guid).count()
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
   101
    
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
    @property
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   103
    def is_copy(self):
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   104
        return bool(self.source_revision)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   105
    
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   106
    # Current revision object or None if there is none
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   107
    @property
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   108
    def current_revision(self):
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   109
        return Revision.objects.filter(parent_renkan__renkan_guid=self.renkan_guid).order_by('-creation_date').first()
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
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   111
    # Current revision 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
   112
    @property
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   113
    def title(self):
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   114
        if self.current_revision:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   115
            return self.current_revision.title
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   116
        else:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   117
            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
   118
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   119
    # Current revision content
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   120
    @property
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   121
    def content(self):
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   122
        if self.current_revision:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   123
            return self.current_revision.content
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   124
        else:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   125
            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
   126
    
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   127
    @transaction.atomic
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   128
    def save_renkan(self, updator, timestamp="", title="", content="", create_new_revision=False):
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   129
        """
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   130
            Saves over current revision or saves a new revision entirely. 
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   131
            Timestamp must be the current revision modification_date.
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   132
        """
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   133
        if (not timestamp) or ((self.current_revision is not None) and dateparse.parse_datetime(timestamp) < self.current_revision.modification_date):
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   134
            logger.error("SAVING RENKAN: provided timestamp is %r, which isn't current revision modification_date %r", timestamp, self.current_revision.modification_date)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   135
            raise ValidationError(message="Error saving Renkan: provided timestamp isn't current revision modification_date")
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   136
        else:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   137
            dt_timestamp = dateparse.parse_datetime(timestamp)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   138
        self.save()
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   139
        if create_new_revision:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   140
            revision_to_update = Revision(parent_renkan=self)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   141
            revision_to_update.creator = updator
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   142
        else:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   143
            revision_to_update = Revision.objects.select_for_update().get(revision_guid=self.current_revision.revision_guid)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   144
        
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   145
        updated_content = content if content else current_revision.content
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   146
        try: 
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   147
            updated_content_dict = json.loads(updated_content)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   148
        except ValueError:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   149
            raise ValidationError(message="Provided content for Renkan is not a JSON-serializable")
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   150
        
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   151
        # If title is passed as arg to the method, update the title in the json
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   152
        if title:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   153
            updated_title = title
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   154
            updated_content_dict["title"] = title
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   155
        # If it is not, we use the one in the json instead
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   156
        else:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   157
            updated_title = updated_content_dict["title"] 
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   158
        
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   159
        revision_to_update.modification_date = timezone.now()
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   160
        updated_content_dict["updated"] = str(revision_to_update.modification_date)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   161
        updated_content = json.dumps(updated_content_dict)  
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   162
        revision_to_update.title = updated_title
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   163
        revision_to_update.content = updated_content
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   164
        if dt_timestamp == revision_to_update.modification_date:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   165
            revision_to_update.modification_date += datetime.resolution
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   166
        revision_to_update.last_updated_by = updator
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   167
        revision_to_update.save()
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   168
    
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   169
    @transaction.atomic
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   170
    def delete(self):
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   171
        """
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   172
            Deleting a renkan also deletes every related revision
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   173
        """
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   174
        renkan_revisions = Revision.objects.filter(parent_renkan__renkan_guid = self.renkan_guid)
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   175
        for child_revision in renkan_revisions:
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   176
            child_revision.delete()
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   177
        super(Renkan, self).delete()
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   178
        
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
   179
    class Meta:
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   180
        app_label = 'renkanmanager'
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   181
        permissions = (
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   182
            ('view_renkan', 'Can view 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
   183
        )
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   184
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   185
        
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
   186
class Revision(models.Model):
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   187
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   188
    revision_guid = models.CharField(max_length=256, default=uuid.uuid4, unique=True) # typically UUID
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   189
    parent_renkan = models.ForeignKey('Renkan', null=False, blank=False, to_field='renkan_guid')
588
95536fa18d0d Minor adjustements to properly interact with Renkan client
durandn
parents: 587
diff changeset
   190
    title = models.CharField(max_length=1024, null=True, blank=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
   191
    content = models.TextField(blank=True, null=True)
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   192
    creator = models.ForeignKey(auth_user_model, blank=True, null=True, related_name="revision_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
   193
    last_updated_by = models.ForeignKey(auth_user_model, blank=True, null=True, related_name="revision_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
   194
    creation_date = models.DateTimeField(auto_now_add=True)
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   195
    modification_date = models.DateTimeField()
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
   196
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   197
    @property
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   198
    def is_current_revision(self):
609
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   199
        # No need to check if parent_renkan.current_revision is not None, as it won't be if we're calling from a revision
854a027c80ff models refactoring to use ForeignKey fields + associated migrations
durandn
parents: 589
diff changeset
   200
        return self.parent_renkan.current_revision.revision_guid == self.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
   201
    
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   202
    class Meta:
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   203
        app_label = 'renkanmanager'
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   204
        permissions = (
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   205
            ('view_revision', 'Can view 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
   206
        )       
fb0041aa74d3 transfer work on renkanmanager into a renkan/server/python2 folder: reworked models, implemented simple API basic permissions, wrote tests
durandn
parents:
diff changeset
   207