Subversion Repositories basico

Rev

Rev 251 | Rev 264 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
195 t00m 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
251 t00m 3
# File: srv_cols.py
195 t00m 4
# Author: Tomás Vírseda
5
# License: GPL v3
229 t00m 6
# Description: Collections service
195 t00m 7
 
8
import os
197 t00m 9
import json
195 t00m 10
import uuid
11
 
243 t00m 12
from basico.core.mod_env import FILE
13
from basico.core.mod_srv import Service
195 t00m 14
 
197 t00m 15
HEADER = ['id', 'title']
229 t00m 16
COLLECTIONS_FILE = FILE['COLLECTIONS']
195 t00m 17
 
197 t00m 18
 
229 t00m 19
class Collections(Service):
195 t00m 20
    def initialize(self):
21
        '''
229 t00m 22
        Setup Collections Service
195 t00m 23
        '''
24
        self.get_services()
229 t00m 25
        self.clts = {}
197 t00m 26
        self.load()
195 t00m 27
 
28
 
29
    def get_services(self):
247 t00m 30
        self.srvgui = self.app.get_service("GUI")
31
        self.srvuif = self.app.get_service("UIF")
32
        self.srvclb = self.app.get_service('Callbacks')
33
        self.srvicm = self.app.get_service('IM')
34
        self.srvdtb = self.app.get_service('DB')
195 t00m 35
 
36
 
197 t00m 37
    def load(self):
38
        try:
229 t00m 39
            with open(COLLECTIONS_FILE, 'r') as ft:
40
                self.clts = json.load(ft)
41
                self.debug ("Loaded %d collections" % len(self.clts))
197 t00m 42
        except Exception as error:
229 t00m 43
            self.debug("Collections file not found. Creating a new one.")
197 t00m 44
            self.save()
45
 
46
 
229 t00m 47
    def save(self, collections={}):
48
        if len(collections) == 0:
49
            collections = self.clts
50
        with open(COLLECTIONS_FILE, 'w') as ft:
51
            json.dump(collections, ft)
52
            self.debug ("Saved %d collections" % (len(collections)))
197 t00m 53
 
54
 
195 t00m 55
    def get_all(self):
229 t00m 56
        return self.clts
195 t00m 57
 
255 t00m 58
    def get_collections_by_row_title(self, row_title):
59
        cids = []
60
        for cid in self.clts:
61
            col_name = self.get_name_by_cid(cid)
62
            if row_title in col_name:
63
                cids.append(cid)
64
        return cids
195 t00m 65
 
229 t00m 66
    def get_collections_name(self):
197 t00m 67
        names = []
229 t00m 68
        for tid in self.clts:
69
            names.append(self.clts[tid])
197 t00m 70
        return names
71
 
72
 
229 t00m 73
    def get_collections_id(self):
195 t00m 74
        tids = []
229 t00m 75
        for tid in self.clts:
197 t00m 76
            tids.append(tid)
195 t00m 77
        return tids
78
 
79
 
239 t00m 80
    def create(self, name):
197 t00m 81
        tid = str(uuid.uuid4())
195 t00m 82
 
83
        if len(name) == 0:
84
            return
85
 
229 t00m 86
        tid_exists = tid in self.get_collections_id()
87
        name_exists = name in self.get_collections_name()
197 t00m 88
 
89
        if tid_exists:
195 t00m 90
            self.debug('Collision? :)')
91
            self.create(entry)
197 t00m 92
        else:
93
            if name_exists:
94
                return
95
            else:
229 t00m 96
                self.clts[tid] = name
239 t00m 97
                self.debug("Created collection: '%s' with cid: '%s'" % (name, tid))
197 t00m 98
                self.save()
195 t00m 99
 
239 t00m 100
        return tid
196 t00mlabs 101
 
239 t00m 102
 
197 t00m 103
    def delete(self, tid):
104
        try:
239 t00m 105
            name = self.get_name_by_cid(tid)
247 t00m 106
            sapnotes = self.srvdtb.get_notes_by_node('collection', tid)
241 t00m 107
            if len(sapnotes) > 0:
108
                self.info("Collection '%s' can't be deleted" % name)
109
                return False
110
            else:
111
                del(self.clts[tid])
112
                self.info("Collection '%s' deleted" % name)
113
                self.save()
114
                return True
197 t00m 115
        except KeyError:
229 t00m 116
            self.debug("You can't delete a non existent collection...")
197 t00m 117
        except Exception as error:
229 t00m 118
            self.debug("Error deleting collection: %s" % error)
119
            self.debug(self.clts)
197 t00m 120
            raise
196 t00mlabs 121
 
122
 
240 t00m 123
    def get_name_by_cid(self, cid):
124
        return self.clts[cid]
196 t00mlabs 125
 
239 t00m 126
 
127
    def get_cid_by_name(self, name):
128
        database = self.get_all()
129
        for col in database:
130
            if database[col] == name:
131
                self.debug("Collection '%s' corresponds with CID: %s" % (name, col))
132
                return col
240 t00m 133
 
239 t00m 134
        return None
240 t00m 135
 
136
 
137
    def rename(self, cid, target):
138
        # check if collections name are the same
139
        source = self.get_name_by_cid(cid)
140
        if source == target:
141
            self.debug("Rename not possible: same name.")
142
            return
143
 
144
        # check if the new name already exists in database
145
        acid = self.get_cid_by_name(target)
146
        if acid is not None:
147
            self.debug("Rename not possible: there is already another collection with this name.")
148
            return
149
 
150
        # Finally, rename the collection and save it.
151
        self.clts[cid] = target
152
        self.debug("Collection[%s] renamed from '%s' to '%s'" % (cid, source, target))
153
        self.save()