forked from openlabs/nereid-project
/
project.py
1037 lines (847 loc) · 31.9 KB
/
project.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
project
Extend the project to allow users
:copyright: (c) 2012 by Openlabs Technologies & Consulting (P) Limited
:license: GPLv3, see LICENSE for more details.
"""
import tempfile
from datetime import datetime
from itertools import groupby, chain
from mimetypes import guess_type
from nereid import (request, abort, render_template, login_required, url_for,
redirect, flash, jsonify)
from flask import send_file
from nereid.ctx import has_request_context
from nereid.contrib.pagination import Pagination
from trytond.model import ModelView, ModelSQL, fields
from trytond.pool import Pool
from trytond.pyson import And, Not, Or, Bool, Equal, Eval
class WebSite(ModelSQL, ModelView):
"""
Website
"""
_name = "nereid.website"
@login_required
def home(self):
"""
Put recent projects into the home
"""
user_obj = Pool().get('nereid.user')
project_obj = Pool().get('project.work')
# TODO: Limit to the last 5 projects
if user_obj.is_project_admin(request.nereid_user):
project_ids = project_obj.search([
('type', '=', 'project'),
('parent', '=', False),
])
else:
project_ids = project_obj.search([
('participants', '=', request.nereid_user.id),
('type', '=', 'project'),
('parent', '=', False),
])
projects = project_obj.browse(project_ids)
return render_template('home.jinja', projects=projects)
WebSite()
class IrAttachment(ModelSQL, ModelView):
"Ir Attachment"
_name = 'ir.attachment'
uploaded_by = fields.Many2One('nereid.user', 'Uploaded By')
def create(self, values):
"""
Update create to save uploaded by
:param values: A dictionary
"""
if has_request_context():
values['uploaded_by'] = request.nereid_user.id
#else:
# TODO: try to find the nereid user from the employee
# if an employee made the update
return super(IrAttachment, self).create(values)
IrAttachment()
class ProjectUsers(ModelSQL):
_name = 'project.work-nereid.user'
_table = 'project_work_nereid_user_rel'
project = fields.Many2One(
'project.work', 'Project',
ondelete='CASCADE', select=1, required=True)
user = fields.Many2One(
'nereid.user', 'User', select=1, required=True
)
ProjectUsers()
class Project(ModelSQL, ModelView):
"""
Tryton itself is very flexible in allowing multiple layers of Projects and
sub projects. But having this and implementing this seems to be too
convulted for everyday use. So nereid simplifies the process to:
- Project::Associated to a party
|
|-- Task (Type is task)
"""
_name = 'project.work'
history = fields.One2Many('project.work.history', 'project',
'History', readonly=True)
participants = fields.Many2Many(
'project.work-nereid.user', 'project', 'user',
'Participants', depends=['parent', 'type'],
states={
'invisible': Bool(Eval('parent')) or Eval('type') != 'project',
'readonly': Bool(Eval('parent')) or Eval('type') != 'project',
}
)
tags_for_projects = fields.One2Many('project.work.tag', 'project',
'Tags', states={
'invisible': Eval('type') != 'project',
'readonly': Eval('type') != 'project',
}
)
#: Tags for tasks.
tags = fields.Many2Many(
'project.work-project.work.tag', 'task', 'tag',
'Tags', depends=['type'],
states={
'invisible': Eval('type') != 'task',
'readonly': Eval('type') != 'task',
}
)
created_by = fields.Many2One('nereid.user', 'Created by')
all_participants = fields.Function(
fields.Many2Many(
'project.work-nereid.user', None, None,
'All Participants', depends=['company']
), 'get_all_participants'
)
assigned_to = fields.Many2One(
'nereid.user', 'Assigned to', depends=['all_participants'],
domain=[('id', 'in', Eval('all_participants'))],
states={
'invisible': Eval('type') != 'task',
'readonly': Eval('type') != 'task',
}
)
#: Get all the attachments on the object and return them
attachments = fields.Function(
fields.One2Many('ir.attachment', None, 'Attachments'),
'get_attachments'
)
def get_attachments(self, ids, name=None):
"""
Return all the attachments in the object
"""
attachment_obj = Pool().get('ir.attachment')
vals = {}
for project_id in ids:
attachments = attachment_obj.search([
('resource', '=', '%s,%d' % (self._name, project_id))
])
vals[project_id] = attachments
return vals
def get_all_participants(self, ids, name=None):
"""
All participants includes the participants in the project and also
the admins
"""
vals = {}
for task in self.browse(ids):
vals[task.id] = []
if task.type != 'task':
continue
vals[task.id].extend([p.id for p in task.participants])
vals[task.id].extend([p.id for p in task.company.project_admins])
return vals
def create(self, values):
if has_request_context():
values['created_by'] = request.nereid_user.id
if values['type'] == 'task':
values.setdefault('participants', [])
values['participants'].append(
('add', [request.nereid_user.id])
)
else:
# TODO: identify the nereid user through employee
pass
return super(Project, self).create(values)
def can_read(self, project, user):
"""
Returns true if the given nereid user can read the project
:param project: The browse record of the project
:param user: The browse record of the current nereid user
"""
nereid_user_obj = Pool().get('nereid.user')
if nereid_user_obj.is_project_admin(user):
return True
if not user in project.participants:
raise abort(404)
return True
def can_write(self, project, user):
"""
Returns true if the given user can write to the project
:param project: The browse record of the project
:param user: The browse record of the current nereid user
"""
nereid_user_obj = Pool().get('nereid.user')
if nereid_user_obj.is_project_admin(user):
return True
if not user in project.participants:
raise abort(404)
return True
def get_project(self, project_id):
"""
Common base for fetching the project while validating if the user
can use it.
:param project_id: ID of the project
"""
project = self.search([
('id', '=', project_id),
('type', '=', 'project'),
])
if not project:
raise abort(404)
project = self.browse(project[0])
if not self.can_read(project, request.nereid_user):
# If the user is not allowed to access this project then dont let
raise abort(404)
return project
def get_task(self, task_id):
"""
Common base for fetching the task while validating if the user
can use it.
:param task_id: ID of the task
"""
task = self.search([
('id', '=', task_id),
('type', '=', 'task'),
])
if not task:
raise abort(404)
task = self.browse(task[0])
if not self.can_write(task.parent, request.nereid_user):
# If the user is not allowed to access this project then dont let
raise abort(403)
return task
def get_tasks_by_tag(self, tag_id):
"""Return the tasks associated with a tag
"""
task_tag_obj = Pool().get('project.work-project.work.tag')
tasks = task_tag_obj.search([
('tag', '=', tag_id)
])
return tasks
@login_required
def render_project(self, project_id):
"""
Renders a project
"""
project = self.get_project(project_id)
return render_template(
'project.jinja', project=project, active_type_name="recent"
)
@login_required
def create_project(self):
"""Create a new project
POST will create a new project
"""
if not request.nereid_user.is_project_admin(request.nereid_user):
flash("Sorry! You are not allowed to create new projects. \
Contact your project admin for the same.")
return redirect(request.referrer)
if request.method == 'POST':
project_id = self.create({
'name': request.form['name'],
'type': 'project',
})
flash("Project successfully created.")
return redirect(
url_for('project.work.render_project', project_id=project_id)
)
flash("Could not create project. Try again.")
return redirect(request.referrer)
@login_required
def create_task(self, project_id):
"""Create a new task for the specified project
POST will create a new task
"""
project = self.get_project(project_id)
# Check if user is among the participants
self.can_write(project, request.nereid_user)
if request.method == 'POST':
task_id = self.create({
'parent': project_id,
'name': request.form['name'],
'type': 'task',
'comment': request.form.get('description', False),
})
flash("Task successfully added to project %s" % project.name)
return redirect(
url_for('project.work.render_task',
project_id=project_id, task_id=task_id
)
)
flash("Could not create task. Try again.")
return redirect(request.referrer)
@login_required
def unwatch(self, task_id):
"""
Remove the current user from the participants of the task
:param task_id: Id of the task
"""
task = self.get_task(task_id)
if request.nereid_user in task.participants:
self.write(
task.id, {
'participants': [('unlink', [request.nereid_user.id])]
}
)
if request.is_xhr:
return jsonify({'success': True})
return redirect(request.referrer)
@login_required
def watch(self, task_id):
"""
Add the current user from the participants of the task
:param task_id: Id of the task
"""
task = self.get_task(task_id)
if request.nereid_user not in task.participants:
self.write(
task.id, {
'participants': [('add', [request.nereid_user.id])]
}
)
if request.is_xhr:
return jsonify({'success': True})
return redirect(request.referrer)
@login_required
def permissions(self, project_id):
"""
Permissions for the project
"""
project = self.get_project(project_id)
return render_template(
'project-permissions.jinja', project=project,
active_type_name='permissions'
)
@login_required
def projects_list(self, page=1):
"""
Render a list of projects
"""
projects = self.search([
('party', '=', request.nereid_user.party),
])
return render_template('projects.jinja', projects=projects)
@login_required
def render_task_list(self, project_id):
"""
Renders a project's task list page
"""
tag_task_obj = Pool().get('project.work-project.work.tag')
project = self.get_project(project_id)
state = request.args.get('state', None)
page = request.args.get('page', 1, int)
filter_domain = [
('type', '=', 'task'),
('parent', '=', project.id),
]
query = request.args.get('q', None)
if query:
# This search is probably the suckiest search in the
# history of mankind in terms of scalability and utility
# TODO: Figure out something better
filter_domain.append(('name', 'ilike', '%%%s%%' % query))
tag = request.args.get('tag', None, int)
if tag:
filter_domain.append(('tags', '=', tag))
counts = {}
counts['opened_tasks_count'] = self.search(
filter_domain + [('state', '=', 'opened')], count=True
)
counts['done_tasks_count'] = self.search(
filter_domain + [('state', '=', 'done')], count=True
)
counts['all_tasks_count'] = self.search(
filter_domain, count=True
)
if state and state in ('opened', 'done'):
filter_domain.append(('state', '=', state))
tasks = Pagination(self, filter_domain, page, 10)
return render_template(
'project-task-list.jinja', project=project,
active_type_name='render_task_list', counts=counts,
state_filter=state, tasks=tasks
)
@login_required
def render_task(self, task_id, project_id=None):
"""
Renders the task in a project
"""
task = self.get_task(task_id)
comments = sorted(
task.history + task.timesheet_lines + task.attachments,
key=lambda x: x.create_date
)
timesheet_rows = sorted(
task.timesheet_lines, key=lambda x: x.employee
)
timesheet_summary = groupby(timesheet_rows, key=lambda x: x.employee)
return render_template(
'task.jinja', task=task, active_type_name='render_task_list',
project=task.parent, comments=comments,
timesheet_summary=timesheet_summary
)
@login_required
def render_files(self, project_id):
project = self.get_project(project_id)
other_attachments = chain.from_iterable(
[list(task.attachments) for task in project.children if task.attachments]
)
return render_template(
'project-files.jinja', project=project, active_type_name='files',
guess_type=guess_type, other_attachments=other_attachments
)
@login_required
def render_timesheet(self, project_id):
project = self.get_project(project_id)
return render_template(
'project.jinja', project=project, active_type_name="recent"
)
@login_required
def render_plan(self, project_id):
"""
Render the plan of the project
"""
project = self.get_project(project_id)
if request.is_xhr:
# XHTTP Request probably from the calendar widget, answer that
# with json
start = datetime.fromtimestamp(
request.args.get('start', type=int)
)
end = datetime.fromtimestamp(
request.args.get('end', type=int)
)
# TODO: These times are local times of the user, convert them to
# UTC (Server time) before using them for comparison
task_ids = self.search(['AND',
('type', '=', 'task'),
('parent', '=', project.id),
['OR',
[
('constraint_start_time', '>=', start),
],
[
('constraint_finish_time', '<=', end),
],
[
('actual_start_time', '>=', start),
],
[
('actual_finish_time', '<=', end),
],
]
])
tasks = self.browse(task_ids)
event_type = request.args['event_type']
assert event_type in ('constraint', 'actual')
def to_event(task, type="constraint"):
event = {
'id': task.id,
'title': task.name,
'url': url_for(
'project.work.render_task',
project_id=task.parent.id, task_id=task.id),
}
event["start"] = getattr(
task, '%s_start_time' % type
).isoformat()
if getattr(task, '%s_finish_time' % type):
event['end'] = getattr(
task, '%s_finish_time' % type
).isoformat()
return event
return jsonify(
result = [
# Send all events where there is a start time
to_event(task, event_type) for task in tasks \
if getattr(task, '%s_start_time' % event_type)
]
)
return render_template(
'project-plan.jinja', project=project,
active_type_name='plan'
)
@login_required
def download_file(self, attachment_id):
"""
Returns the file for download. The wonership of the task or the
project is checked automatically.
"""
attachment_obj = Pool().get('ir.attachment')
work = None
if request.args.get('project', None):
work = self.get_project(request.args.get('project', type=int))
if request.args.get('task', None):
work = self.get_task(request.args.get('task', type=int))
if not work:
# Neither task, nor the project is specified
raise abort(404)
attachment_ids = attachment_obj.search([
('id', '=', attachment_id),
('resource', '=', '%s,%d' % (self._name, work.id))
])
if not attachment_ids:
raise abort(404)
attachment = attachment_obj.browse(attachment_ids[0])
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(attachment.data)
return send_file(
f.name, attachment_filename=attachment.name, as_attachment=True
)
@login_required
def upload_file(self):
"""
Upload the file to a project or task with owner/uploader
as the current user
"""
attachment_obj = Pool().get('ir.attachment')
work = None
if request.form.get('project', None):
work = self.get_project(request.form.get('project', type=int))
if request.form.get('task', None):
work = self.get_task(request.form.get('task', type=int))
if not work:
# Neither task, nor the project is specified
raise abort(404)
attached_file = request.files["file"]
data = {
'resource': '%s,%d' % (self._name, work.id),
'description': request.form.get('description', False)
}
if request.form.get('file_type') == 'link':
link = request.form.get('url')
data.update({
'link': link,
'name': link.split('/')[-1],
'type': 'link'
})
else:
data.update({
'data': attached_file.stream.read(),
'name': attached_file.filename,
'type': 'data'
})
attachment_id = attachment_obj.create(data)
if request.is_xhr:
return jsonify({
'success': True
})
flash("Attachment added to %s" % work.name)
return redirect(request.referrer)
@login_required
def update_task(self, task_id, project_id=None):
"""
Accepts a POST request against a task_id and updates the ticket
:param task_id: The ID of the task which needs to be updated
"""
history_obj = Pool().get('project.work.history')
task = self.get_task(task_id)
history_data = {
'project': task.id,
'updated_by': request.nereid_user.id,
'comment': request.form['comment']
}
updatable_attrs = ['state']
post_attrs = [request.form.get(attr, None) for attr in updatable_attrs]
if any(post_attrs):
# Combined update of task and history since there is some value
# posted in addition to the comment
task_changes = {}
for attr in updatable_attrs:
if getattr(task, attr) != request.form[attr]:
task_changes[attr] = request.form[attr]
if task_changes:
# Only write change if anything has really changed
self.write(task.id, task_changes)
comment_id = self.browse(task.id).history[-1].id
history_obj.write(comment_id, history_data)
else:
# just create comment since nothing really changed since this
# update. This is to cover to cover cases where two users who
# havent refreshed the web page close the ticket
comment_id = history_obj.create(history_data)
else:
# Just comment, no update to task
comment_id = history_obj.create(history_data)
if request.nereid_user.id not in (p.id for p in task.participants):
# Add the user to the participants if not already in the list
self.write(
task.id, {'participants': [('add', [request.nereid_user.id])]}
)
if request.is_xhr:
comment_record = history_obj.browse(comment_id)
html = render_template('comment.jinja', comment=comment_record)
return jsonify({
'success': True,
'html': html,
'state': self.browse(task.id).state,
})
return redirect(request.referrer)
@login_required
def add_tag(self, task_id, tag_id):
"""Assigns the provided to this task
:param task_id: ID of task
:param tag_id: ID of tag
"""
task = self.get_task(task_id)
self.write(
task.id, {'tags': [('add', [tag_id])]}
)
if request.method == 'POST':
flash('Tag added to task %s' % task.name)
return redirect(request.referrer)
flash("Tag cannot be added")
return redirect(request.referrer)
@login_required
def remove_tag(self, task_id, tag_id):
"""Assigns the provided to this task
:param task_id: ID of task
:param tag_id: ID of tag
"""
task = self.get_task(task_id)
self.write(
task.id, {'tags': [('unlink', [tag_id])]}
)
if request.method == 'POST':
flash('Tag removed from task %s' % task.name)
return redirect(request.referrer)
flash("Tag cannot be removed")
return redirect(request.referrer)
def write(self, ids, values):
"""
Update write to historize everytime an update is made
:param ids: ids of the projects
:param values: A dictionary
"""
work_history_obj = Pool().get('project.work.history')
if isinstance(ids, (int, long)):
ids = [ids]
for project in self.browse(ids):
work_history_obj.create_history_line(project, values)
return super(Project, self).write(ids, values)
@login_required
def mark_time(self, task_id):
"""Marks the time against the employee for the task
:param task_id: ID of task
"""
timesheet_line_obj = Pool().get('timesheet.line')
if not request.nereid_user.employee:
flash("Only employees can mark time on tasks!")
return redirect(request.referrer)
task = self.get_task(task_id)
timesheet_line_obj.create({
'employee': request.nereid_user.employee.id,
'hours': request.form['hours'],
'work': task.id
})
flash("Time has been marked on task %s" % task.name)
return redirect(request.referrer)
@login_required
def assign_task(self, task_id):
"""Assign task to a user
:param task_id: Id of Task
"""
nereid_user_obj = Pool().get('nereid.user')
task = self.get_task(task_id)
new_assignee = nereid_user_obj.browse(int(request.form['user']))
if self.can_write(task.parent, new_assignee):
self.write(task.id, {
'assigned_to': new_assignee.id
})
if request.is_xhr:
return jsonify({
'success': True,
})
flash("Task assigned to %s" % new_assignee.name)
return redirect(request.referrer)
flash("Only employees can be assigned to tasks.")
return redirect(request.referrer)
@login_required
def clear_assigned_user(self, task_id):
"""Clear the assigned user from the task
:param task_id: Id of Task
"""
task = self.get_task(task_id)
self.write(task.id, {
'assigned_to': False
})
if request.is_xhr:
return jsonify({
'success': True,
})
flash("Removed the assigned user from task")
return redirect(request.referrer)
Project()
class ProjectTag(ModelSQL, ModelView):
"Tags"
_name = "project.work.tag"
_description = __doc__
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',
)
def __init__(self):
super(ProjectTag, self).__init__()
#self._sql_contraints += [
# ('unique_name_project', 'UNIQUE(name, project)', 'Duplicate Tag')
#]
def default_color(self):
return "#999"
@login_required
def create_tag(self, project_id):
"""Create a new tag for the specific project
"""
project_obj = Pool().get('project.work')
project = project_obj.get_project(project_id)
# Check if user is among the project admins
if not request.nereid_user.is_project_admin(request.nereid_user):
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_id = self.create({
'name': request.form['name'],
'color': request.form['color'],
'project': project_id
})
flash("Successfully created tag")
return redirect(request.referrer)
flash("Could not create tag. Try Again")
return redirect(request.referrer)
@login_required
def delete_tag(self, tag_id):
"""Delete the tag from project
"""
# Check if user is among the project admins
if not request.nereid_user.is_project_admin(request.nereid_user):
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:
tag_id = self.delete(tag_id)
return jsonify({
'success': True,
})
flash("Could not delete tag! Try again.")
return redirect(request.referrer)
ProjectTag()
class TaskTags(ModelSQL):
_name = 'project.work-project.work.tag'
_table = 'project_work_tag_rel'
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',
)
TaskTags()
class ProjectHistory(ModelSQL, ModelView):
'Project Work History'
_name = 'project.work.history'
_description = __doc__
date = fields.DateTime('Change Date')
create_uid = fields.Many2One('res.user', 'Create User')
#: The reverse many to one for history field to work
project = fields.Many2One('project.work', 'Project Work')
# Nereid user who made this update
updated_by = fields.Many2One('nereid.user', 'Updated By')
# States
previous_state = fields.Selection([
('opened', 'Opened'),
('done', 'Done'),
], 'Prev. State', select=True)
new_state = fields.Selection([
('opened', 'Opened'),
('done', 'Done'),
], 'New State', select=True)
# Comment
comment = fields.Text('Comment')
# Name
previous_name = fields.Char('Prev. Name')
new_name = fields.Char('New Name')
# Assigned to
previous_assigned_to = fields.Many2One('nereid.user', 'Prev. Assignee')
new_assigned_to = fields.Many2One('nereid.user', 'New Assignee')
# other fields
previous_constraint_start_time = fields.DateTime("Constraint Start Time")
new_constraint_start_time = fields.DateTime("Next Constraint Start Time")
previous_constraint_finish_time = fields.DateTime("Constraint Finish Time")
new_constraint_finish_time = fields.DateTime("Constraint Finish Time")
def default_date(self):
return datetime.utcnow()
def create_history_line(self, project, changed_values):
"""
Creates a history line from the changed values of a project.work
"""
if changed_values:
data = {}
# TODO: Also create a line when assigned user is cleared from task
for field in ('assigned_to', 'state',
'constraint_start_time', 'constraint_finish_time'):
if field not in changed_values or not changed_values[field]:
continue
data['previous_%s' % field] = getattr(project, field)
data['new_%s' % field] = changed_values[field]
if data:
if has_request_context():
data['updated_by'] = request.nereid_user.id
else:
# TODO: try to find the nereid user from the employee
# if an employee made the update
pass
data['project'] = project.id
return self.create(data)
def get_function_fields(self, ids, names):
"""
Function to compute fields
:param ids: the ids of works
:param names: the list of field name to compute
:return: a dictionary with all field names as key and
a dictionary as value with id as key
"""
pass