forked from treeio/treeio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
793 lines (627 loc) · 30.5 KB
/
views.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
# encoding: utf-8
# Copyright 2011 Tree.io Limited
# This file is part of Treeio.
# License www.tree.io/license
"""
Documents module views
"""
from django.shortcuts import get_object_or_404
from treeio.core.rendering import render_to_response
from treeio.documents.forms import FolderForm, DocumentForm, FileForm, FilterForm, WebLinkForm, \
MassActionForm
from django.template import RequestContext
from treeio.documents.models import Document, Folder, File, WebLink
from treeio.core.models import Object
from treeio.core.views import user_denied
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.db.models import Q
from treeio.core.decorators import treeio_login_required, handle_response_format
from django.http import HttpResponse
from treeio.core.conf import settings
from django.utils.encoding import smart_str
def _get_filter_query(args):
"Creates a generic query to filter Documents, Files and Weblinks based on FilterForm arguments"
query = Q()
for arg in args:
if hasattr(Document, arg) and args[arg]:
append = Q(**{str('document__' + arg + '__id'): long(args[arg])})
append = append | Q(**{str('file__' + arg + '__id'): long(args[arg])})
append = append | Q(**{str('weblink__' + arg + '__id'): long(args[arg])})
query = query & append
return query
def _get_direct_filter_query(args):
"Creates a query to filter Documents, Files or Weblinks based on FilterForm arguments"
query = Q()
for arg in args:
if hasattr(Document, arg) and args[arg]:
append = Q(**{str(arg + '__id'): long(args[arg])})
query = query & append
return query
def _get_default_context(request):
"Returns default context as a dict()"
folders = Object.filter_by_request(request, Folder.objects, mode="r")
massform = MassActionForm(request.user.get_profile())
context = {'folders': folders,
'massform': massform}
return context
def _process_mass_form(f):
"Pre-process request to handle mass action form for Document items"
def wrap(request, *args, **kwargs):
"Wrap"
user = request.user.get_profile()
if 'massform' in request.POST:
for key in request.POST:
if 'mass-object' in key:
try:
query = Q(object_type='treeio.documents.models.Document') | \
Q(object_type='treeio.documents.models.File') | \
Q(object_type='treeio.documents.models.WebLink')
objects = Object.filter_by_request(request, Object.objects.filter(query))
object = objects.get(pk=request.POST[key])
form = MassActionForm(user, request.POST, instance=object)
if form.is_valid() and user.has_permission(object, mode='w'):
form.save()
except Exception:
pass
if 'mass-file' in key:
try:
file = File.objects.get(pk=request.POST[key])
form = MassActionForm(user, request.POST, instance=file)
if form.is_valid() and user.has_permission(file, mode='w'):
form.save()
except Exception:
pass
if 'mass-weblink' in key:
try:
link = WebLink.objects.get(pk=request.POST[key])
form = MassActionForm(user, request.POST, instance=link)
if form.is_valid() and user.has_permission(link, mode='w'):
form.save()
except Exception:
pass
if 'mass-document' in key:
try:
document = Document.objects.get(pk=request.POST[key])
form = MassActionForm(user, request.POST, instance=document)
if form.is_valid() and user.has_permission(document, mode='w'):
form.save()
except Exception:
pass
return f(request, *args, **kwargs)
wrap.__doc__ = f.__doc__
wrap.__name__ = f.__name__
return wrap
@handle_response_format
@treeio_login_required
@_process_mass_form
def index(request, response_format='html'):
"Index page: displays all Documents"
query = Q(object_type='treeio.documents.models.Document') | \
Q(object_type='treeio.documents.models.File') | \
Q(object_type='treeio.documents.models.WebLink')
if request.GET:
query = _get_filter_query(request.GET) & query
objects = Object.filter_by_request(request, Object.objects.filter(query).order_by('-last_updated'))
else:
objects = Object.filter_by_request(request, Object.objects.filter(query).order_by('-last_updated'))
filters = FilterForm(request.user.get_profile(), 'title', request.GET)
context = _get_default_context(request)
context.update({'filters':filters,
'objects':objects})
return render_to_response('documents/index', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
@_process_mass_form
def index_files(request, response_format='html'):
"Index_files page: displays all Files"
if request.GET:
query = _get_direct_filter_query(request.GET)
files = Object.filter_by_request(request, File.objects.filter(query).order_by('-last_updated'))
else:
files = Object.filter_by_request(request, File.objects.order_by('-last_updated'))
filters = FilterForm(request.user.get_profile(), 'title', request.GET)
context = _get_default_context(request)
context.update({'filters':filters,
'files':files})
return render_to_response('documents/index_files', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
@_process_mass_form
def index_weblinks(request, response_format='html'):
"Index_weblinks page: displays all WebLinks"
if request.GET:
query = _get_direct_filter_query(request.GET)
links = Object.filter_by_request(request, WebLink.objects.filter(query).order_by('-last_updated'))
else:
links = Object.filter_by_request(request, WebLink.objects.order_by('-last_updated'))
filters = FilterForm(request.user.get_profile(), 'title', request.GET)
context = _get_default_context(request)
context.update({'filters':filters,
'links':links})
return render_to_response('documents/index_weblinks', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
@_process_mass_form
def index_documents(request, response_format='html'):
"Index_documents page: displays all Documents"
if request.GET:
query = _get_direct_filter_query(request.GET)
documents = Object.filter_by_request(request, Document.objects.filter(query).order_by('-last_updated'))
else:
documents = Object.filter_by_request(request, Document.objects.order_by('-last_updated'))
filters = FilterForm(request.user.get_profile(), 'title', request.GET)
context = _get_default_context(request)
context.update({'filters':filters,
'documents':documents})
return render_to_response('documents/index_documents', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def folder_add(request, response_format='html'):
"New folder form"
if request.POST:
if not 'cancel' in request.POST:
folder = Folder()
form = FolderForm(request.user.get_profile(), None, request.POST, instance=folder)
if form.is_valid():
folder = form.save()
folder.set_user_from_request(request)
return HttpResponseRedirect(reverse('documents_folder_view', args=[folder.id]))
else:
return HttpResponseRedirect(reverse('document_index'))
else:
form = FolderForm(request.user.get_profile(), None)
context = _get_default_context(request)
context.update({'form': form})
return render_to_response('documents/folder_add', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def folder_add_typed(request, folder_id=None, response_format='html'):
"Folder add to preselected folder"
folder = None
if folder_id:
folder = get_object_or_404(Folder, pk=folder_id)
if not request.user.get_profile().has_permission(folder, mode='x'):
folder = None
if request.POST:
if not 'cancel' in request.POST:
folder = Folder()
form = FolderForm(request.user.get_profile(), folder_id, request.POST, instance=folder)
if form.is_valid():
folder = form.save()
folder.set_user_from_request(request)
return HttpResponseRedirect(reverse('documents_folder_view', args=[folder.id]))
else:
return HttpResponseRedirect(reverse('document_index'))
else:
form = FolderForm(request.user.get_profile(), folder_id)
context = _get_default_context(request)
context.update({'form': form,
'folder':folder})
return render_to_response('documents/folder_add_typed', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
@_process_mass_form
def folder_view(request, folder_id, response_format='html'):
"Single folder view page"
folder = get_object_or_404(Folder, pk=folder_id)
if not request.user.get_profile().has_permission(folder):
return user_denied(request, message="You don't have access to this Folder")
query = Q(
object_type='treeio.documents.models.Document') | Q(
object_type='treeio.documents.models.File') | Q(
object_type='treeio.documents.models.WebLink')
query = query & (Q(document__folder=folder) | Q(file__folder=folder) | Q(weblink__folder=folder))
if request.GET:
query = query & _get_filter_query(request.GET)
objects = Object.filter_by_request(request, Object.objects.filter(query).order_by('-last_updated'))
# objects = objects.order_by('-last_updated')
else:
objects = Object.filter_by_request(request, Object.objects.filter(query).order_by('-last_updated'))
# objects = objects.order_by('-last_updated')
subfolders = Folder.objects.filter(parent=folder)
filters = FilterForm(request.user.get_profile(), 'title', request.GET)
context = _get_default_context(request)
context.update({'folder': folder,
'objects':objects,
'subfolders':subfolders,
'filters':filters})
return render_to_response('documents/folder_view', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def folder_edit(request, folder_id, response_format='html'):
"Folder edit page"
folder = get_object_or_404(Folder, pk=folder_id)
if not request.user.get_profile().has_permission(folder, mode='w'):
return user_denied(request, message="You don't have access to this Folder")
if request.POST:
if not 'cancel' in request.POST:
form = FolderForm(request.user.get_profile(), folder_id, request.POST, instance=folder)
if form.is_valid():
folder = form.save()
return HttpResponseRedirect(reverse('documents_folder_view', args=[folder.id]))
else:
return HttpResponseRedirect(reverse('documents_folder_view', args=[folder.id]))
else:
form = FolderForm(request.user.get_profile(), folder_id, instance=folder)
context = _get_default_context(request)
context.update({'form': form,
'folder': folder})
return render_to_response('documents/folder_edit', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def folder_delete(request, folder_id, response_format='html'):
"Folder delete"
folder = get_object_or_404(Folder, pk=folder_id)
if not request.user.get_profile().has_permission(folder, mode='w'):
return user_denied(request, message="You don't have access to this Folder")
if request.POST:
if 'delete' in request.POST:
if 'trash' in request.POST:
folder.trash = True
folder.save()
else:
folder.delete()
return HttpResponseRedirect(reverse('document_index'))
elif 'cancel' in request.POST:
return HttpResponseRedirect(reverse('documents_folder_view', args=[folder.id]))
query = Q(object_type='treeio.documents.models.Document') | Q(
object_type='treeio.documents.models.File') | Q(
object_type='treeio.documents.models.WebLink')
query = query & (Q(document__folder=folder) | Q(file__folder=folder) | Q(weblink__folder=folder))
if request.GET:
query = _get_filter_query(request.GET)
objects = Object.filter_by_request(request, Object.objects.filter(query))
# objects = objects.order_by('-last_updated')
else:
objects = Object.filter_by_request(request, Object.objects.filter(query))
# objects = objects.order_by('-last_updated')
context = _get_default_context(request)
context.update({'folder': folder,
'objects':objects})
return render_to_response('documents/folder_delete', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def document_add(request, response_format='html'):
"new document form"
if request.POST:
if not 'cancel' in request.POST:
document = Document()
form = DocumentForm(request.user.get_profile(), None, request.POST, instance=document)
if form.is_valid():
document = form.save()
document.set_user_from_request(request)
return HttpResponseRedirect(reverse('documents_document_view', args=[document.id]))
else:
return HttpResponseRedirect(reverse('document_index'))
else:
form = DocumentForm(request.user.get_profile(), None)
context = _get_default_context(request)
context.update({'form': form})
return render_to_response('documents/document_add', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def document_add_typed(request, folder_id=None, response_format='html'):
"Document add to preselected folder"
folder = None
if folder_id:
folder = get_object_or_404(Folder, pk=folder_id)
if not request.user.get_profile().has_permission(folder, mode='x'):
folder = None
document = Document()
if request.POST:
if not 'cancel' in request.POST:
form = DocumentForm(request.user.get_profile(), folder_id, request.POST, instance=document)
if form.is_valid():
document = form.save()
document.set_user_from_request(request)
return HttpResponseRedirect(reverse('documents_document_view', args=[document.id]))
else:
return HttpResponseRedirect(reverse('document_index'))
else:
form = DocumentForm(request.user.get_profile(), folder_id)
context = _get_default_context(request)
context.update({'form': form,
'folder':folder})
return render_to_response('documents/document_add_typed', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def document_view(request, document_id, response_format='html'):
"Single document view page"
document = get_object_or_404(Document, pk=document_id)
if not request.user.get_profile().has_permission(document):
return user_denied(request, message="You don't have access to this Document")
context = _get_default_context(request)
context.update({'document':document})
return render_to_response('documents/document_view', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def document_edit(request, document_id, response_format='html'):
"Document edit page"
document = get_object_or_404(Document, pk=document_id)
if not request.user.get_profile().has_permission(document, mode='w'):
return user_denied(request, message="You don't have access to this Document")
if request.POST:
if not 'cancel' in request.POST:
form = DocumentForm(request.user.get_profile(), None, request.POST, instance=document)
if form.is_valid():
document = form.save()
return HttpResponseRedirect(reverse('documents_document_view', args=[document.id]))
else:
return HttpResponseRedirect(reverse('documents_document_view', args=[document.id]))
else:
form = DocumentForm(request.user.get_profile(), None, instance=document)
context = _get_default_context(request)
context.update({'form': form,
'document':document})
return render_to_response('documents/document_edit', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def document_delete(request, document_id, response_format='html'):
"Document delete"
document = get_object_or_404(Document, pk=document_id)
if not request.user.get_profile().has_permission(document, mode='w'):
return user_denied(request, message="You don't have access to this Document")
if request.POST:
if 'delete' in request.POST:
if 'trash' in request.POST:
document.trash = True
document.save()
else:
document.delete()
return HttpResponseRedirect(reverse('document_index'))
elif 'cancel' in request.POST:
return HttpResponseRedirect(reverse('documents_document_view', args=[document.id]))
context = _get_default_context(request)
context.update({'document':document})
return render_to_response('documents/document_delete', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def file_upload(request, response_format='html'):
"New file form"
if request.POST:
if not 'cancel' in request.POST:
file = File()
form = FileForm(request.user.get_profile(), None, request.POST, request.FILES, instance=file)
if form.is_valid():
file = form.save()
file.set_user_from_request(request)
return HttpResponseRedirect(reverse('documents_file_view', args=[file.id]))
else:
return HttpResponseRedirect(reverse('document_index'))
else:
form = FileForm(request.user.get_profile(), None)
context = _get_default_context(request)
context.update({'form': form})
return render_to_response('documents/file_upload', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def file_upload_typed(request, folder_id=None, response_format='html'):
"File upload to preselected folder"
folder = None
if folder_id:
folder = get_object_or_404(Folder, pk=folder_id)
if not request.user.get_profile().has_permission(folder, mode='x'):
folder = None
if request.POST:
if not 'cancel' in request.POST:
form = FileForm(request.user.get_profile(), folder_id, request.POST, request.FILES)
if form.is_valid():
file = form.save()
file.set_user_from_request(request)
return HttpResponseRedirect(reverse('documents_file_view', args=[file.id]))
else:
return HttpResponseRedirect(reverse('document_index'))
else:
form = FileForm(request.user.get_profile(), folder_id)
context = _get_default_context(request)
context.update({'form': form,
'folder':folder})
return render_to_response('documents/file_upload_typed', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def file_view(request, file_id, response_format='html'):
"Single file view page"
file = get_object_or_404(File, pk=file_id)
if not request.user.get_profile().has_permission(file):
return user_denied(request, message="You don't have access to this File")
if request.GET and 'download' in request.GET:
"Return url to download a file"
fullpath = getattr(settings, 'MEDIA_ROOT', './static/media/')
data = ''
try:
data = open(fullpath + str(file.content)).read()
except IOError:
pass
response = HttpResponse(data, content_type='application/x-download')
response['Content-Disposition'] = 'attachment; filename="%s"' % smart_str(file.content)
return response
context = _get_default_context(request)
context.update({'file':file})
return render_to_response('documents/file_view', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def file_delete(request, file_id, response_format='html'):
"File delete"
file = get_object_or_404(File, pk=file_id)
if not request.user.get_profile().has_permission(file, mode='w'):
return user_denied(request, message="You don't have access to this File")
if request.POST:
if 'delete' in request.POST:
if 'trash' in request.POST:
file.trash = True
file.save()
else:
file.delete()
return HttpResponseRedirect(reverse('document_index'))
elif 'cancel' in request.POST:
return HttpResponseRedirect(reverse('documents_file_view', args=[file.id]))
context = _get_default_context(request)
context.update({'file':file})
return render_to_response('documents/file_delete', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def file_edit(request, file_id, response_format='html'):
"File edit page"
file = get_object_or_404(File, pk=file_id)
if not request.user.get_profile().has_permission(file, mode='w'):
return user_denied(request, message="You don't have access to this File")
if request.POST:
if not 'cancel' in request.POST:
form = FileForm(request.user.get_profile(), None, request.POST, request.FILES, instance=file)
if form.is_valid():
file = form.save()
return HttpResponseRedirect(reverse('documents_file_view', args=[file.id]))
else:
return HttpResponseRedirect(reverse('documents_file_view', args=[file.id]))
else:
form = FileForm(request.user.get_profile(), None, instance=file)
context = _get_default_context(request)
context.update({'form': form,
'file':file})
return render_to_response('documents/file_edit', context,
context_instance=RequestContext(request),
response_format=response_format)
# Web Links
@handle_response_format
@treeio_login_required
def weblink_add(request, response_format='html'):
"New web link form"
if request.POST:
if not 'cancel' in request.POST:
link = WebLink()
form = WebLinkForm(request.user.get_profile(), None, request.POST, instance=link)
if form.is_valid():
link = form.save()
link.set_user_from_request(request)
return HttpResponseRedirect(reverse('documents_weblink_view', args=[link.id]))
else:
return HttpResponseRedirect(reverse('document_index'))
else:
form = WebLinkForm(request.user.get_profile(), None)
context = _get_default_context(request)
context.update({'form': form,
'file':file})
return render_to_response('documents/weblink_add', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def weblink_add_typed(request, folder_id=None, response_format='html'):
"Web link add to preselected folder"
folder = None
if folder_id:
folder = get_object_or_404(Folder, pk=folder_id)
if not request.user.get_profile().has_permission(folder, mode='x'):
folder = None
if request.POST:
if not 'cancel' in request.POST:
link = WebLink()
form = WebLinkForm(request.user.get_profile(), folder_id, request.POST, instance=link)
if form.is_valid():
link = form.save()
link.set_user_from_request(request)
return HttpResponseRedirect(reverse('documents_weblink_view', args=[link.id]))
else:
return HttpResponseRedirect(reverse('document_index'))
else:
form = WebLinkForm(request.user.get_profile(), folder_id)
context = _get_default_context(request)
context.update({'form': form,
'folder':folder})
return render_to_response('documents/weblink_add_typed', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def weblink_view(request, weblink_id, response_format='html'):
"Weblink page"
link = get_object_or_404(WebLink, pk=weblink_id)
if not request.user.get_profile().has_permission(link):
return user_denied(request, message="You don't have access to this Web Link")
context = _get_default_context(request)
context.update({'link':link})
return render_to_response('documents/weblink_view', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def weblink_edit(request, weblink_id, response_format='html'):
"WebLink edit page"
link = get_object_or_404(WebLink, pk=weblink_id)
if not request.user.get_profile().has_permission(link, mode='w'):
return user_denied(request, message="You don't have access to this Web Link")
if request.POST:
if not 'cancel' in request.POST:
form = WebLinkForm(request.user.get_profile(), None, request.POST, instance=link)
if form.is_valid():
link = form.save()
return HttpResponseRedirect(reverse('documents_weblink_view', args=[link.id]))
else:
return HttpResponseRedirect(reverse('documents_weblink_view', args=[link.id]))
else:
form = WebLinkForm(request.user.get_profile(), None, instance=link)
context = _get_default_context(request)
context.update({'form': form,
'link':link})
return render_to_response('documents/weblink_edit', context,
context_instance=RequestContext(request),
response_format=response_format)
@handle_response_format
@treeio_login_required
def weblink_delete(request, weblink_id, response_format='html'):
"WebLink delete"
link = get_object_or_404(WebLink, pk=weblink_id)
if not request.user.get_profile().has_permission(link, mode='w'):
return user_denied(request, message="You don't have access to this Web Link")
if request.POST:
if 'delete' in request.POST:
if 'trash' in request.POST:
link.trash = True
link.save()
else:
link.delete()
return HttpResponseRedirect(reverse('document_index'))
elif 'cancel' in request.POST:
return HttpResponseRedirect(reverse('documents_weblink_view', args=[link.id]))
context = _get_default_context(request)
context.update({'link':link})
return render_to_response('documents/weblink_delete', context,
context_instance=RequestContext(request),
response_format=response_format)