-
Notifications
You must be signed in to change notification settings - Fork 20
/
tag.py
157 lines (132 loc) · 4.67 KB
/
tag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
"""
tag
:copyright: (c) 2012-2014 by Openlabs Technologies & Consulting (P) Limited
:license: GPLv3, see LICENSE for more details.
"""
from nereid import (
request, login_required, url_for, redirect,
flash, jsonify, route
)
from trytond.model import ModelView, ModelSQL, fields
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
from trytond import backend
__all__ = ['Tag', 'TaskTags']
__metaclass__ = PoolMeta
class Tag(ModelSQL, ModelView):
"Tags"
__name__ = "project.work.tag"
name = fields.Char('Name', required=True)
color = fields.Char('Color Code', required=True)
project = fields.Many2One(
'project.work', 'Project', required=True,
domain=[('type', '=', 'project')], ondelete='CASCADE',
)
@classmethod
def __setup__(cls):
super(Tag, cls).__setup__()
cls._sql_constraints += [
('unique_name_project', 'UNIQUE(name, project)', 'Duplicate Tag')
]
@staticmethod
def default_color():
'''
Default for color
'''
return "#999"
def serialize(self, purpose=None):
'''
Serialize the tag and returns a dictionary.
'''
return {
'create_date': self.create_date.isoformat(),
"url": url_for(
'project.work.render_task_list', project_id=self.project.id,
state="opened", tag=self.id
),
"objectType": self.__name__,
"id": self.id,
"displayName": self.name,
}
@classmethod
@route('/project-<int:project_id>/tag/-new', methods=['GET', 'POST'])
@login_required
def create_tag(cls, project_id):
"""
Create a new tag for the specific project
:params project_id: Project id for which need to be created
"""
Project = Pool().get('project.work')
Activity = Pool().get('nereid.activity')
project = Project.get_project(project_id)
# Check if user is among the project admin members
if not request.nereid_user.is_admin_of_project(project):
flash(
"Sorry! You are not allowed to create new tags." +
" Contact your project admin for the same."
)
return redirect(request.referrer)
if request.method == 'POST':
tag, = cls.create([{
'name': request.form['name'],
'color': request.form['color'],
'project': project.id
}])
Activity.create([{
'actor': request.nereid_user.id,
'object_': 'project.work.tag, %d' % tag.id,
'verb': 'created_tag',
'target': 'project.work, %d' % project.id,
'project': project.id,
}])
flash("Successfully created tag")
return redirect(request.referrer)
flash("Could not create tag. Try Again")
return redirect(request.referrer)
@login_required
@route('/tag-<int:active_id>/-delete', methods=['GET', 'POST'])
def delete_tag(self):
"""
Delete the tag from project
"""
# Check if user is among the project admins
if request.method == 'POST' and not \
request.nereid_user.is_admin_of_project(self.project):
flash(
"Sorry! You are not allowed to delete tags." +
" Contact your project admin for the same."
)
return redirect(request.referrer)
if request.method == 'POST' and request.is_xhr:
self.delete([self])
return jsonify({
'success': True,
})
flash("Could not delete tag! Try again.")
return redirect(request.referrer)
class TaskTags(ModelSQL):
'Task Tags'
__name__ = 'project.work-project.work.tag'
task = fields.Many2One(
'project.work', 'Project',
ondelete='CASCADE', select=1, required=True,
domain=[('type', '=', 'task')]
)
tag = fields.Many2One(
'project.work.tag', 'Tag', select=1, required=True, ondelete='CASCADE',
)
@classmethod
def __register__(cls, module_name):
'''
Register class and update table name to new.
'''
cursor = Transaction().cursor
TableHandler = backend.get('TableHandler')
table = TableHandler(cursor, cls, module_name)
super(TaskTags, cls).__register__(module_name)
# Migration
if table.table_exist(cursor, 'project_work_tag_rel'):
table.table_rename(
cursor, 'project_work_tag_rel', 'project_work-project_work_tag'
)