|
1 | 1 | import copy |
| 2 | +import datetime |
2 | 3 |
|
3 | | -from workflows.utils import get_workflow_for_object, set_workflow_for_object |
| 4 | +from django.contrib.contenttypes.models import ContentType |
4 | 5 |
|
5 | | -from ietf.ietfworkflows.models import WGWorkflow |
| 6 | +from workflows.models import State |
| 7 | +from workflows.utils import (get_workflow_for_object, set_workflow_for_object, |
| 8 | + get_state) |
| 9 | + |
| 10 | +from ietf.ietfworkflows.models import (WGWorkflow, AnnotationTagObjectRelation, |
| 11 | + AnnotationTag, ObjectAnnotationTagHistoryEntry) |
| 12 | + |
| 13 | + |
| 14 | +WAITING_WRITEUP = 'WG Consensus: Waiting for Write-Up' |
| 15 | +FOLLOWUP_TAG = 'Doc Shepherd Follow-up Underway' |
6 | 16 |
|
7 | 17 |
|
8 | 18 | def get_default_workflow_for_wg(): |
@@ -51,3 +61,85 @@ def get_workflow_for_wg(wg): |
51 | 61 | workflow = clone_workflow(workflow, name='%s workflow' % wg) |
52 | 62 | set_workflow_for_object(wg, workflow) |
53 | 63 | return workflow |
| 64 | + |
| 65 | +def get_workflow_for_draft(draft): |
| 66 | + workflow = get_workflow_for_object(draft) |
| 67 | + try: |
| 68 | + workflow = workflow and workflow.wgworkflow |
| 69 | + except WGWorkflow.DoesNotExist: |
| 70 | + workflow = None |
| 71 | + if not workflow: |
| 72 | + workflow = get_workflow_for_wg(draft.group.ietfwg) |
| 73 | + set_workflow_for_object(draft, workflow) |
| 74 | + return workflow |
| 75 | + |
| 76 | + |
| 77 | +def get_annotation_tags_for_draft(draft): |
| 78 | + ctype = ContentType.objects.get_for_model(draft) |
| 79 | + tags = AnnotationTagObjectRelation.objects.filter(content_type=ctype, content_id=draft.pk) |
| 80 | + return tags |
| 81 | + |
| 82 | + |
| 83 | +def get_state_for_draft(draft): |
| 84 | + return get_state(draft) |
| 85 | + |
| 86 | + |
| 87 | +def get_state_by_name(state_name): |
| 88 | + try: |
| 89 | + return State.objects.get(name=state_name) |
| 90 | + except State.DoesNotExist: |
| 91 | + return None |
| 92 | + |
| 93 | + |
| 94 | +def get_annotation_tag_by_name(tag_name): |
| 95 | + try: |
| 96 | + return AnnotationTag.objects.get(name=tag_name) |
| 97 | + except AnnotationTag.DoesNotExist: |
| 98 | + return None |
| 99 | + |
| 100 | +def set_tag_by_name(obj, tag_name): |
| 101 | + ctype = ContentType.objects.get_for_model(obj) |
| 102 | + try: |
| 103 | + tag = AnnotationTag.objects.get(name=tag_name) |
| 104 | + (relation, created) = AnnotationTagObjectRelation.objects.get_or_create( |
| 105 | + content_type=ctype, |
| 106 | + content_id=obj.pk, |
| 107 | + annotation_tag=tag) |
| 108 | + except AnnotationTag.DoesNotExist: |
| 109 | + return None |
| 110 | + return relation |
| 111 | + |
| 112 | + |
| 113 | +def reset_tag_by_name(obj, tag_name): |
| 114 | + ctype = ContentType.objects.get_for_model(obj) |
| 115 | + try: |
| 116 | + tag = AnnotationTag.objects.get(name=tag_name) |
| 117 | + tag_relation = AnnotationTagObjectRelation.objects.get( |
| 118 | + content_type=ctype, |
| 119 | + content_id=obj.pk, |
| 120 | + annotation_tag=tag) |
| 121 | + tag_relation.delete() |
| 122 | + return True |
| 123 | + except AnnotationTagObjectRelation.DoesNotExist: |
| 124 | + return False |
| 125 | + except AnnotationTag.DoesNotExist: |
| 126 | + return False |
| 127 | + |
| 128 | + |
| 129 | +def update_tags(obj, comment, set_tags=[], reset_tags=[]): |
| 130 | + ctype = ContentType.objects.get_for_model(obj) |
| 131 | + setted = [] |
| 132 | + resetted = [] |
| 133 | + for name in set_tags: |
| 134 | + if set_tag_by_name(obj, name): |
| 135 | + setted.append(name) |
| 136 | + for name in reset_tags: |
| 137 | + if reset_tag_by_name(obj, name): |
| 138 | + resetted.append(name) |
| 139 | + ObjectAnnotationTagHistoryEntry.objects.create( |
| 140 | + content_type=ctype, |
| 141 | + content_id=obj.pk, |
| 142 | + setted = ','.join(setted), |
| 143 | + unsetted = ','.join(resetted), |
| 144 | + change_date = datetime.datetime.now(), |
| 145 | + comment = comment) |
0 commit comments