| author | ymh <ymh.work@gmail.com> |
| Mon, 19 Oct 2015 09:54:57 +0200 | |
| changeset 1461 | fff3ef58e8e3 |
| parent 1458 | fe2ec4cb6183 |
| child 1465 | 74b0b90517ed |
| permissions | -rw-r--r-- |
| 1458 | 1 |
import itertools |
2 |
||
3 |
from django.contrib.auth import get_user_model |
|
4 |
from django.core.management.base import NoArgsCommand, CommandError |
|
5 |
from django.db.models import Q |
|
6 |
||
7 |
from ldt.ldt_utils.models import Project, Content |
|
8 |
from ldt.ldt_utils.modelsutils import ProjectMerger |
|
9 |
||
10 |
||
11 |
class Command(NoArgsCommand): |
|
12 |
""" |
|
13 |
Merge projects. |
|
14 |
Beware this version of the command correctly support merging projects from only one content. |
|
15 |
Usage: |
|
16 |
-p <project_id>: project id, can have more than one |
|
17 |
-c <content_id>: content id, can have more than one |
|
18 |
-t <title>: project title (default merge of "projects title") |
|
19 |
-u <user>: user for project (defaut admin) |
|
20 |
--only-visible: only visible cuttings (default false) |
|
21 |
--exclude-shot-by-shot: remove shot by shot (default false) |
|
22 |
--all-published: merge all projects published or not (default false) |
|
23 |
""" |
|
24 |
help = 'Merge projects' |
|
25 |
||
26 |
def add_arguments(self, parser): |
|
27 |
||
28 |
# Named (optional) arguments |
|
29 |
parser.add_argument( |
|
30 |
'--only-visible', |
|
31 |
action='store_true', |
|
32 |
dest='visible', |
|
33 |
default=False, |
|
34 |
help='merge only visible cuttings') |
|
35 |
||
36 |
parser.add_argument( |
|
37 |
'--exclude-shot-by-shot', |
|
38 |
action='store_true', |
|
39 |
dest='exclude_shot_by_shot', |
|
40 |
default=False, |
|
41 |
help='exclude shot by shot cuttings') |
|
42 |
||
43 |
parser.add_argument( |
|
44 |
'--all-published', |
|
45 |
action='store_true', |
|
46 |
dest='all_published', |
|
47 |
default=False, |
|
48 |
help='merge all projects published or not') |
|
49 |
||
50 |
parser.add_argument( |
|
51 |
'-p', '--project', |
|
52 |
action='append', |
|
53 |
dest='projects', |
|
54 |
metavar='PROJECT_ID', |
|
55 |
default=[], |
|
56 |
help='project id to concatenate') |
|
57 |
||
58 |
parser.add_argument( |
|
59 |
'-c', '--content', |
|
60 |
action='store', |
|
61 |
dest='contents', |
|
62 |
default=None, |
|
63 |
metavar='CONTENT_ID', |
|
64 |
help='content id to concatenate') |
|
65 |
||
66 |
parser.add_argument( |
|
67 |
'-t', '--title', |
|
68 |
action='store', |
|
69 |
dest='title', |
|
70 |
metavar='TITLE', |
|
71 |
help='The title for the merged project. defaut "merged: title1, title2,..."' |
|
72 |
) |
|
73 |
||
74 |
parser.add_argument( |
|
75 |
'-u', '--user', |
|
76 |
action='store', |
|
77 |
dest='user', |
|
78 |
metavar='USER', |
|
79 |
help='The user creator of the merged project' |
|
80 |
) |
|
81 |
||
82 |
||
83 |
def handle_noargs(self, **options): |
|
84 |
||
85 |
# pylint: disable=C0103 |
|
86 |
User = get_user_model() |
|
87 |
||
88 |
username = options.get('user', None) |
|
89 |
||
90 |
if not username: |
|
91 |
users = User.objects.filter(is_superuser=True).order_by('date_joined') |
|
92 |
if len(users) > 0: |
|
93 |
user = users[0] |
|
94 |
username = user.username |
|
95 |
else: |
|
96 |
raise CommandError("No username given and can not fond a superuser") |
|
97 |
else: |
|
98 |
user = User.objects.get(username=username) |
|
99 |
||
100 |
# filter projects |
|
101 |
project_filter = Q() |
|
102 |
||
103 |
projects_ids = options.get('projects', None) |
|
104 |
if projects_ids: |
|
105 |
project_filter &= Q(ldt_id__in=projects_ids) |
|
106 |
||
107 |
content_id = options.get('contents', None) |
|
108 |
if content_id: |
|
109 |
project_filter = Q(contents__iri_id__in=[content_id,]) |
|
110 |
||
111 |
if not projects_ids and not content_id: |
|
112 |
raise CommandError("At least one content or project must be specified") |
|
113 |
||
114 |
if not options.get('all_published', False): |
|
115 |
project_filter &= Q(state=Project.PUBLISHED) |
|
116 |
||
117 |
projects = Project.objects.filter(project_filter) |
|
118 |
||
119 |
if len(projects) == 0: |
|
120 |
raise CommandError("No project found, aborting") |
|
121 |
||
122 |
||
123 |
title = options.get('title', "") |
|
124 |
if not title: |
|
125 |
title = "Merged: " + ", ".join([p.title.strip() for p in projects]) |
|
126 |
||
127 |
contents = set(itertools.chain(*[proj.contents.all() for proj in projects])) |
|
128 |
if len(contents) == 0: |
|
129 |
raise CommandError("Content not found") |
|
130 |
if len(contents) > 1 and not content_id: |
|
131 |
raise CommandError("Too many contents %d" % len(contents)) |
|
132 |
||
133 |
if content_id: |
|
134 |
content = Content.objects.get(iri_id=content_id) |
|
135 |
else: |
|
136 |
content = contents[0] |
|
137 |
||
138 |
if options.get('verbosity', 1) > 0: |
|
139 |
print("Merging %d projects in \'%s\'" % (len(projects), title)) |
|
140 |
||
141 |
pmerger = ProjectMerger(content, projects) |
|
142 |
||
143 |
proj = pmerger.get_merged_project( |
|
144 |
only_visible=not options.get('visible', False), |
|
145 |
shot_by_shot=not options.get('exclude_shot_by_shot', False)) |
|
146 |
||
147 |
proj.created_by = username |
|
148 |
proj.changed_by = username |
|
149 |
proj.owner = user |
|
150 |
proj.title = title |
|
151 |
proj.save() |
|
152 |
||
153 |
if options.get('verbosity', 1) > 0: |
|
|
1461
fff3ef58e8e3
just add the project id to output
ymh <ymh.work@gmail.com>
parents:
1458
diff
changeset
|
154 |
print("Project \'%s\' created with id '%s'" % (title, proj.ldt_id)) |