Blame view

src/routine_manager/views.py 18.1 KB
3c179769   Jeremy   userSimulator / a...
1
2
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
986d5dec   haribo   Date: 14/06/2016
3
from django.shortcuts import render, redirect
ddf59dd4   haribo   Remaniement :
4
from common.models import *
986d5dec   haribo   Date: 14/06/2016
5
6
7
from django.db.models import Q
from django.contrib.auth.decorators import login_required
from .forms import RequestForm, SequenceForm, AlbumForm, PlanForm
3dbda6a0   haribo   Date: 17/06/2016
8
from .validators import check_plan_validity, check_album_validity, check_sequence_validity, check_request_validity
eea995c9   haribo   Date: 16/06/2016
9
from .RequestSerializer import RequestSerializer
bbf6e698   haribo   Date: 14/06/2016
10
import scheduler
3df2d31a   haribo   #3430 : dates are...
11

d91a4c2b   Jeremy   Inserted logger
12
13
""" logger """
from django.conf import settings
bca9a283   Jeremy   Reworked the sche...
14
import utils.Logger as l
b5e9fde1   Jeremy   Update django ver...
15
log = l.setupLogger("routine_manager-views", "routine_manager-views")
d91a4c2b   Jeremy   Inserted logger
16

3dbda6a0   haribo   Date: 17/06/2016
17
""" XML Export / Import utils """
eea995c9   haribo   Date: 16/06/2016
18
19
20
21
22
from wsgiref.util import FileWrapper
from django.utils.encoding import smart_str
from django.http import HttpResponse
import mimetypes
import os
c08615f7   haribo   Debug routine MGR...
23
24
from pprint import pprint

984e74ea   haribo   Moving non-app di...
25
26
27
SAVED_REQUESTS_FOLDER = "misc/saved_requests/"


986d5dec   haribo   Date: 14/06/2016
28
29
@login_required
def requests_list(request, status=0, message=""):
bbf6e698   haribo   Date: 14/06/2016
30
31
32
    """
        Retrieves and display the routines list (routine manager main page)
    """
986d5dec   haribo   Date: 14/06/2016
33

d91a4c2b   Jeremy   Inserted logger
34
35
36
    if settings.DEBUG:
        log.info("From requests_list")

986d5dec   haribo   Date: 14/06/2016
37
38
39
40
41
    if status == "-1":
        error = True
    elif status == "1":
        success = True

5a1d9029   haribo   Date: 20/06/2016
42
    # TODO: uncomment for alert filter
53787d30   Jeremy   Alert now inherit...
43
#     requests_objs = Request.objects.filter(pyros_user=request.user).filter(is_alert=False).order_by("-updated")
6a1a30ed   carens_p   grammar for instr...
44

53787d30   Jeremy   Alert now inherit...
45
    requests_objs = Request.objects.filter(pyros_user=request.user).order_by("-updated")
cd0149f5   Etienne Pallier   progress made for...
46
    print("REQ/views: Les requetes sont:")
986d5dec   haribo   Date: 14/06/2016
47
48
49
50

    requests = []

    for req in requests_objs:
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
51
        #print("- REQ/views: requete", req)
986d5dec   haribo   Date: 14/06/2016
52
53
54
55
56
57
        sequences = req.sequences
        nb_executed = sequences.filter(status=Sequence.EXECUTED).count
        nb_cancelled = sequences.filter(Q(status=Sequence.INVALID) | Q(status=Sequence.CANCELLED)
                                        | Q(status=Sequence.UNPLANNABLE)).count()
        requests.append({'req': req, 'nb_seq': sequences.count(), 'nb_executed': nb_executed, 'nb_cancelled': nb_cancelled})

cd0149f5   Etienne Pallier   progress made for...
58
59
    print("REQ/views: ICI:")
    # opens template src/routine_manager/templates/routine_manager/requests_list.html with all local variables
986d5dec   haribo   Date: 14/06/2016
60
61
62
63
    return render(request, "routine_manager/requests_list.html", locals())

@login_required
def action_request(request, req_id, action, status=0, message=""):
bbf6e698   haribo   Date: 14/06/2016
64
65
66
67
    """
        Apply actions (view, edit, delete) on a given request
    """

986d5dec   haribo   Date: 14/06/2016
68
69
70
    req = Request.objects.get(id=req_id)
    depth_level = 1

d91a4c2b   Jeremy   Inserted logger
71
72
73
    if settings.DEBUG:
        log.info("From action_request")

986d5dec   haribo   Date: 14/06/2016
74
75
76
77
78
    if status == "-1":
        error = True
    elif status == "1":
        success = True

bbf6e698   haribo   Date: 14/06/2016
79
80
81
82
83
    if req.submitted == True and action == "edit":
        error = True
        message = "You can't edit a submitted request"
        action = "view"

eea995c9   haribo   Date: 16/06/2016
84
85
    if check_request_validity(req) == True:
        return redirect(unsubmit_request, req_id)
986d5dec   haribo   Date: 14/06/2016
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    if action == "edit":
        form = RequestForm(instance=req)
        edit = True
        return render(request, "routine_manager/view_request.html", locals())
    elif action == "view":
        form = RequestForm(instance=req, readonly=True)
        edit = False
        return render(request, "routine_manager/view_request.html", locals())
    elif action == "delete":
        req.delete()
        return redirect(requests_list, status='1', message="Successfully deleted the request")

    return redirect(requests_list)

@login_required
def request_validate(request, req_id):
bbf6e698   haribo   Date: 14/06/2016
102
103
104
105
106
    """
        Called when the request form was validated.
        Possible actions : Cancel, Save, Save and add sequence, Delete
    """

986d5dec   haribo   Date: 14/06/2016
107
108
109
110
    action = request.POST.get("action")
    req = Request.objects.get(id=req_id)
    form = RequestForm(instance=req, data=request.POST)

d91a4c2b   Jeremy   Inserted logger
111
112
113
    if (settings.DEBUG):
        log.info("From request_validate")

986d5dec   haribo   Date: 14/06/2016
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    if action == "cancel":
        if req.name == "New request":
            req.delete()
        return redirect(requests_list, status=1, message="Cancelled request modification")
    elif action == "delete":
        return redirect(action_request, req_id, "delete")
    elif form.is_valid():
        req = form.save()
        if action == "save":
            return redirect(action_request, req_id=req_id, action="edit", status=1, message="Request saved")
        if action == "save_and_add":
            return redirect(create_sequence, req_id=req_id)
    else:
        error = True
        message = "Please check your field's validity"
        depth_level = 1
        edit = True
        return render(request, "routine_manager/view_request.html", locals())


@login_required
def action_sequence(request, seq_id, action, status=0, message=""):
bbf6e698   haribo   Date: 14/06/2016
136
137
138
139
    """
        Apply actions (view, edit, delete) on a given sequence
    """

986d5dec   haribo   Date: 14/06/2016
140
141
142
143
144
145
    seq_id = int(seq_id)
    seq = Sequence.objects.get(id=seq_id)
    req = seq.request
    req_id = req.id
    depth_level = 2

d91a4c2b   Jeremy   Inserted logger
146
147
148
    if (settings.DEBUG):
        log.info("From action_sequence")

986d5dec   haribo   Date: 14/06/2016
149
150
151
152
153
    if status == "-1":
        error = True
    elif status == "1":
        success = True

bbf6e698   haribo   Date: 14/06/2016
154
155
156
157
158
    if seq.status != Sequence.INCOMPLETE and seq.status != Sequence.COMPLETE and action == "edit":
        error = True
        message = "You can't edit a submitted request"
        action = "view"

eea995c9   haribo   Date: 16/06/2016
159
160
    if check_sequence_validity(seq) == True:
        return redirect(unsubmit_request, req_id)
986d5dec   haribo   Date: 14/06/2016
161
162
163
164
165
    if action == "edit":
        form = SequenceForm(instance=seq)
        edit = True
        return render(request, "routine_manager/view_sequence.html", locals())
    elif action == "view":
bbf6e698   haribo   Date: 14/06/2016
166
        form = SequenceForm(instance=seq, readonly=True)
986d5dec   haribo   Date: 14/06/2016
167
168
169
170
        edit = False
        return render(request, "routine_manager/view_sequence.html", locals())
    elif action == "delete":
        seq.delete()
eea995c9   haribo   Date: 16/06/2016
171
172
        if check_request_validity(req) == True:
            return redirect(unsubmit_request, req_id)
986d5dec   haribo   Date: 14/06/2016
173
174
175
176
177
178
        return redirect(action_request, req_id=seq.request.id, action="edit", status='1', message="Successfully deleted the sequence")

    return redirect(requests_list)

@login_required
def sequence_validate(request, seq_id):
bbf6e698   haribo   Date: 14/06/2016
179
180
181
182
183
    """
        Called when the sequence form was validated.
        Possible actions : Cancel, Save, Save and add album, Delete
    """

d91a4c2b   Jeremy   Inserted logger
184
185
186
    if (settings.DEBUG):
        log.info("From sequence_validate")

986d5dec   haribo   Date: 14/06/2016
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
    seq_id = int(seq_id)
    action = request.POST.get("action")
    seq = Sequence.objects.get(id=seq_id)
    form = SequenceForm(instance=seq, data=request.POST)

    if action == "cancel":
        if seq.name == "New sequence":
            seq.delete()
        return redirect(action_request, req_id=seq.request.id, action="edit", status='1', message="Cancelled sequence modification")
    elif action == "delete":
        return redirect(action_sequence, seq_id, "delete")
    elif form.is_valid():
        seq = form.save()
        if action == "save":
            return redirect(action_sequence, seq_id=seq_id, action="edit", status=1, message="Sequence saved")
        if action == "save_and_add":
            return redirect(create_album, seq_id=seq_id)
    else:
        error = True
        message = "Please check your field's validity"
        depth_level = 2
        edit = True
        req = seq.request
        req_id = req.id
        return render(request, "routine_manager/view_sequence.html", locals())

@login_required
def action_album(request, alb_id, action, status=0, message=""):
bbf6e698   haribo   Date: 14/06/2016
215
216
217
218
    """
        Apply actions (view, edit, delete) on a given album
    """

986d5dec   haribo   Date: 14/06/2016
219
220
221
222
223
224
225
    alb_id = int(alb_id)
    alb = Album.objects.get(id=alb_id)
    req = alb.sequence.request
    req_id = req.id
    seq_id = alb.sequence.id
    depth_level = 3

d91a4c2b   Jeremy   Inserted logger
226
227
228
    if (settings.DEBUG):
        log.info("From action_album")

986d5dec   haribo   Date: 14/06/2016
229
230
231
232
233
    if status == "-1":
        error = True
    elif status == "1":
        success = True

bbf6e698   haribo   Date: 14/06/2016
234
235
236
237
238
    if alb.sequence.status != Sequence.INCOMPLETE and alb.sequence.status != Sequence.COMPLETE and action == "edit":
        error = True
        message = "You can't edit a submitted request"
        action = "view"

eea995c9   haribo   Date: 16/06/2016
239
240
    if check_album_validity(alb) == True:
        return redirect(unsubmit_request, req_id)
986d5dec   haribo   Date: 14/06/2016
241
242
243
244
245
    if action == "edit":
        form = AlbumForm(instance=alb)
        edit = True
        return render(request, "routine_manager/view_album.html", locals())
    elif action == "view":
bbf6e698   haribo   Date: 14/06/2016
246
        form = AlbumForm(instance=alb, readonly=True)
986d5dec   haribo   Date: 14/06/2016
247
248
249
250
251
252
253
254
255
256
        edit = False
        return render(request, "routine_manager/view_album.html", locals())
    elif action == "delete":
        alb.delete()
        return redirect(action_sequence, seq_id=alb.sequence.id, action="edit", status='1', message="Successfully deleted the album")

    return redirect(requests_list)

@login_required
def album_validate(request, alb_id):
bbf6e698   haribo   Date: 14/06/2016
257
258
259
260
261
    """
        Called when the album form was validated.
        Possible actions : Cancel, Save, Save and add plan, Delete
    """

986d5dec   haribo   Date: 14/06/2016
262
263
264
265
266
    alb_id = int(alb_id)
    action = request.POST.get("action")
    alb = Album.objects.get(id=alb_id)
    form = AlbumForm(instance=alb, data=request.POST)

d91a4c2b   Jeremy   Inserted logger
267
268
269
270
    if (settings.DEBUG):
        log.info("From album_validate")


986d5dec   haribo   Date: 14/06/2016
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
    if action == "cancel":
        if alb.name == "New album":
            alb.delete()
        return redirect(action_sequence, seq_id=alb.sequence.id, action="edit", status='1', message="Cancelled album modification")
    elif action == "delete":
        return redirect(action_album, alb_id, "delete")
    elif form.is_valid():
        alb = form.save()
        if action == "save":
            return redirect(action_album, alb_id=alb_id, action="edit", status=1, message="Album saved")
        if action == "save_and_add":
            return redirect(create_plan, alb_id=alb_id)
    else:
        error = True
        message = "Please check your field's validity"
        depth_level = 3
        edit = True
        req = alb.sequence.request
        req_id = req.id
        seq_id = alb.sequence.id
5a1d9029   haribo   Date: 20/06/2016
291
        action = "edit"
986d5dec   haribo   Date: 14/06/2016
292
293
294
295
        return render(request, "routine_manager/view_album.html", locals())

@login_required
def action_plan(request, plan_id, action, status=0, message=""):
bbf6e698   haribo   Date: 14/06/2016
296
297
298
299
    """
        Apply actions (view, edit, delete) on a given plan
    """

986d5dec   haribo   Date: 14/06/2016
300
301
302
303
304
305
306
307
    plan_id = int(plan_id)
    plan = Plan.objects.get(id=plan_id)
    req = plan.album.sequence.request
    req_id = req.id
    seq_id = plan.album.sequence.id
    alb_id = plan.album.id
    depth_level = 4

d91a4c2b   Jeremy   Inserted logger
308
309
310
    if (settings.DEBUG):
        log.info("From action_plan")

986d5dec   haribo   Date: 14/06/2016
311
312
313
314
315
    if status == "-1":
        error = True
    elif status == "1":
        success = True

bbf6e698   haribo   Date: 14/06/2016
316
317
318
319
320
    if plan.album.sequence.status != Sequence.INCOMPLETE and plan.album.sequence.status != Sequence.COMPLETE and action == "edit":
        error = True
        message = "You can't edit a submitted request"
        action = "view"

3dbda6a0   haribo   Date: 17/06/2016
321
    if check_plan_validity(plan) == True:
eea995c9   haribo   Date: 16/06/2016
322
        return redirect(unsubmit_request, req_id)
986d5dec   haribo   Date: 14/06/2016
323
324
325
326
327
    if action == "edit":
        form = PlanForm(instance=plan)
        edit = True
        return render(request, "routine_manager/view_plan.html", locals())
    elif action == "view":
bbf6e698   haribo   Date: 14/06/2016
328
        form = PlanForm(instance=plan, readonly=True)
986d5dec   haribo   Date: 14/06/2016
329
330
331
332
333
334
335
336
337
338
        edit = False
        return render(request, "routine_manager/view_plan.html", locals())
    elif action == "delete":
        plan.delete()
        return redirect(action_album, alb_id=plan.album.id, action="edit", status='1', message="Successfully deleted the plan")

    return redirect(requests_list)

@login_required
def plan_validate(request, plan_id):
bbf6e698   haribo   Date: 14/06/2016
339
340
341
342
343
    """
        Called when the plan form was validated.
        Possible actions : Cancel, Save, Delete
    """

986d5dec   haribo   Date: 14/06/2016
344
345
346
347
348
    plan_id = int(plan_id)
    action = request.POST.get("action")
    plan = Plan.objects.get(id=plan_id)
    form = PlanForm(instance=plan, data=request.POST)

d91a4c2b   Jeremy   Inserted logger
349
350
351
352
    if (settings.DEBUG):
        log.info("From plan_validate")


986d5dec   haribo   Date: 14/06/2016
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
    if action == "cancel":
        if plan.name == "New plan":
            plan.delete()
        return redirect(action_album, alb_id=plan.album.id, action="edit", status='1', message="Cancelled plan modification")
    elif action == "delete":
        return redirect(action_plan, plan_id, "delete")
    elif form.is_valid():
        plan = form.save()
        if action == "save":
            return redirect(action_plan, plan_id=plan_id, action="edit", status=1, message="Plan saved")
    else:
        error = True
        message = "Please check your field's validity"
        edit = True
        req = plan.album.sequence.request
        req_id = req.id
        seq_id = plan.album.sequence.id
        alb_id = plan.album.id
        depth_level = 4
        return render(request, "routine_manager/view_plan.html", locals())

@login_required
def create_request(request):
bbf6e698   haribo   Date: 14/06/2016
376
377
378
379
    """
        Create a new request and redirects to the editing action on it
    """

53787d30   Jeremy   Alert now inherit...
380
    req = Request.objects.create(pyros_user=request.user, name="New request", is_alert=False, autodeposit=False, complete=False, submitted=False)
986d5dec   haribo   Date: 14/06/2016
381
382
383
384
    return redirect(action_request, req.id, "edit")

@login_required
def create_sequence(request, req_id):
bbf6e698   haribo   Date: 14/06/2016
385
386
387
388
    """
        Create a new sequence and redirects to the editing action on it
    """

986d5dec   haribo   Date: 14/06/2016
389
390
391
392
393
394
    req = Request.objects.get(id=req_id)
    seq = Sequence.objects.create(request=req, name="New sequence", status=Sequence.INCOMPLETE)
    return redirect(action_sequence, seq.id, "edit")

@login_required
def create_album(request, seq_id):
bbf6e698   haribo   Date: 14/06/2016
395
396
397
398
    """
        Create a new album and redirects to the editing action on it
    """

986d5dec   haribo   Date: 14/06/2016
399
400
401
402
403
404
    seq = Sequence.objects.get(id=seq_id)
    alb = Album.objects.create(sequence=seq, name="New album", complete=False)
    return redirect(action_album, alb.id, "edit")

@login_required
def create_plan(request, alb_id):
bbf6e698   haribo   Date: 14/06/2016
405
406
407
408
    """
        Create a new plan and redirects to the editing action on it
    """

986d5dec   haribo   Date: 14/06/2016
409
410
411
    alb = Album.objects.get(id=alb_id)
    plan = Plan.objects.create(album=alb, name="New plan", complete=False)
    return redirect(action_plan, plan.id, "edit")
bbf6e698   haribo   Date: 14/06/2016
412

eea995c9   haribo   Date: 16/06/2016
413
@login_required
5a1d9029   haribo   Date: 20/06/2016
414
def submit_request(request, req_id, redir):
bbf6e698   haribo   Date: 14/06/2016
415
416
417
418
    """
        Submits a request and its sequences for scheduling
    """

d91a4c2b   Jeremy   Inserted logger
419
420
421
    if (settings.DEBUG):
        log.info("From submit_request")

bbf6e698   haribo   Date: 14/06/2016
422
423
424
425
426
427
428
429
430
431
432
433
434
    req = Request.objects.get(id=req_id)
    error = False
    if req.complete == False:
        error = True
        message = "A request must be complete to be submitted"
    elif req.submitted == True:
        error = True
        message = "The request is already submitted"

    if error == True:
        return redirect(action_request, req_id=req_id, action="view", status=-1, message=message)

    for seq in req.sequences.all():
ff448d43   Jeremy   Update
435
        seq.status = Sequence.TOBEPLANNED
bbf6e698   haribo   Date: 14/06/2016
436
437
438
        seq.save()
    req.submitted = True
    req.save()
5d61cbfe   haribo   Just adding / rem...
439

cd0149f5   Etienne Pallier   progress made for...
440
441
442
443
444
445
    # TODO : changer le first_schedule ...
    if settings.USE_CELERY:
        print("WITH CELERY")
        scheduler.tasks.scheduling.delay(first_schedule=True, alert=False)
    else:
        print("WITHOUT CELERY")
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
446
        scheduler.tasks.scheduling().run(first_schedule=True, alert=False)
9774228b   haribo   Date: 22/06/2016
447

bbf6e698   haribo   Date: 14/06/2016
448
    message = "The request was submitted"
5a1d9029   haribo   Date: 20/06/2016
449
450
451
452
    if redir == "action_request":
        return redirect(action_request, req_id=req_id, action="view", status=1, message=message)
    else:
        return redirect(requests_list, status=1, message=message)
bbf6e698   haribo   Date: 14/06/2016
453

eea995c9   haribo   Date: 16/06/2016
454
@login_required
bbf6e698   haribo   Date: 14/06/2016
455
456
457
458
459
def unsubmit_request(request, req_id):
    """
        Unsubmits a request and remove its sequences from scheduling
    """

d91a4c2b   Jeremy   Inserted logger
460
461
462
    if (settings.DEBUG):
        log.info("From unsubmit_request")

bbf6e698   haribo   Date: 14/06/2016
463
    req = Request.objects.get(id=req_id)
dd3121ae   haribo   Minor debugs on r...
464

1a317dbd   haribo   TODOs et modifs m...
465
# TODO: uncomment pour la production
dd3121ae   haribo   Minor debugs on r...
466
#     if req.sequences.filter(Q(status=Sequence.EXECUTED) | Q(status=Sequence.EXECUTING)).exists():
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
467
#         message = "You can't unsubmit a request with executed sequences"
dd3121ae   haribo   Minor debugs on r...
468
469
#         return redirect(action_request, req_id=req_id, action="view", status=-1, message=message)

bbf6e698   haribo   Date: 14/06/2016
470
471
    req.submitted = False
    req.save()
ff448d43   Jeremy   Update
472
    sequences = req.sequences.filter(Q(status=Sequence.TOBEPLANNED) | Q(status=Sequence.PLANNED) |
bbf6e698   haribo   Date: 14/06/2016
473
474
475
476
                                        Q(status=Sequence.INVALID) | Q(status=Sequence.UNPLANNABLE))
    for seq in sequences:
        seq.status = Sequence.COMPLETE
        seq.save()
1a317dbd   haribo   TODOs et modifs m...
477
    # TODO: uncomment
bbf6e698   haribo   Date: 14/06/2016
478
479
    # scheduler.tasks.scheduling.delay(first_schedule=True, alert=False)  # TODO : changer le first_schedule ...

eea995c9   haribo   Date: 16/06/2016
480
481
482
483
    if req.complete == True:
        message = "The request was unsubmitted"
    else:
        message = "The request was unsubmitted because it is incomplete"
bbf6e698   haribo   Date: 14/06/2016
484
    return redirect(action_request, req_id=req_id, action="edit", status=1, message=message)
eea995c9   haribo   Date: 16/06/2016
485
486
487
488
489
490
491

@login_required
def export_request(request, req_id):
    """
        Create an XML file with the given request, and send a download request to the user
    """

d91a4c2b   Jeremy   Inserted logger
492
493
494
    if (settings.DEBUG):
        log.info("From export_request")

eea995c9   haribo   Date: 16/06/2016
495
496
497
    req = Request.objects.get(id=req_id)
    if req.complete == False:
        message = "Request must be completely valid to be serialized"
3dbda6a0   haribo   Date: 17/06/2016
498
        return redirect(action_request, req_id=req_id, action="view", status=-1, message=message)
eea995c9   haribo   Date: 16/06/2016
499

984e74ea   haribo   Moving non-app di...
500
    file_name = SAVED_REQUESTS_FOLDER + "request" + str(req.id) + ".xml"
eea995c9   haribo   Date: 16/06/2016
501
502
503
504
505
506
507
    rs = RequestSerializer()
    rs.serialize(req, file_name)

    wrapper = FileWrapper(open(file_name, "r"))
    content_type = mimetypes.guess_type(file_name)[0]

    response = HttpResponse(wrapper, content_type=content_type)
5d61cbfe   haribo   Just adding / rem...
508
509
    response['Content-Length'] = os.path.getsize(file_name)
    response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(os.path.basename(file_name))
eea995c9   haribo   Date: 16/06/2016
510
511

    return response
3dbda6a0   haribo   Date: 17/06/2016
512
513

@login_required
3c179769   Jeremy   userSimulator / a...
514
@csrf_exempt
3dbda6a0   haribo   Date: 17/06/2016
515
516
517
518
519
520
def import_request(request):
    """
        Ask for a XML file, parse it and create a request in DB
        Don't do anything if there is a single error into the file
    """

d91a4c2b   Jeremy   Inserted logger
521
522
523
    if (settings.DEBUG):
        log.info("From import_request")

3c179769   Jeremy   userSimulator / a...
524
525
    log.info(request)

3dbda6a0   haribo   Date: 17/06/2016
526
527
    if request.method == "POST":
        file = request.FILES.get("request_file")
fd99569d   haribo   Date: 22/06/2016
528
529
530
531
        if file is None:
            status = -1
            message = "File does not exist"
        elif file.size > 1000000:
3dbda6a0   haribo   Date: 17/06/2016
532
533
534
535
            status = -1
            message = "File is too big (more than 1 000 000 bytes)"
        else:
            rs = RequestSerializer()
53787d30   Jeremy   Alert now inherit...
536
            message = rs.unserialize(file.read(), request.user)
3dbda6a0   haribo   Date: 17/06/2016
537
538
539
540
541
            if message != "":
                status = -1
            else:
                status = 1
                message = "Request imported successfully. Please check it before submitting for scheduling."
cd0149f5   Etienne Pallier   progress made for...
542
        print("message is", message)
3dbda6a0   haribo   Date: 17/06/2016
543
544
545
546
    else:
        status = -1
        message = "Internal error"

cd0149f5   Etienne Pallier   progress made for...
547
    # Now, display the list of requests (updated with this new request)
3dbda6a0   haribo   Date: 17/06/2016
548
    return redirect(requests_list, status=status, message=message)