This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
forked from felipelerena/FlightDeck
-
Notifications
You must be signed in to change notification settings - Fork 20
/
views.py
1315 lines (1120 loc) · 46.2 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
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
"""
Views for the Jetpack application
"""
import commonware.log
import os
import shutil
import codecs
import tempfile
import urllib2
import time
import waffle
from simplejson import JSONDecodeError
from statsd import statsd
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.core.cache import cache
from django.db import transaction
from django.views.static import serve
from django.shortcuts import get_object_or_404
from django.http import (HttpResponseRedirect, HttpResponse,
HttpResponseForbidden, HttpResponseServerError,
Http404, HttpResponseBadRequest) # , QueryDict
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
from django.db import IntegrityError, transaction
from django.db.models import Q, ObjectDoesNotExist
from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt
from django.template.defaultfilters import escape
from django.conf import settings
from django.utils import simplejson
from django.forms.fields import URLField
from tasks import zip_source
from base.shortcuts import get_object_with_related_or_404
from utils import validator
from utils.helpers import pathify, render, render_json
from utils.exceptions import parse_validation_messages
from jetpack.package_helpers import (get_package_revision,
create_package_from_xpi)
from jetpack.models import (Package, PackageRevision, Module, Attachment, SDK,
EmptyDir, EDITABLE_EXTENSIONS)
from jetpack.errors import (FilenameExistException, DependencyException,
IllegalFilenameException, KeyNotAllowed)
from person.models import Profile
log = commonware.log.getLogger('f.jetpack')
def browser(request, page_number=1, type_id=None, username=None):
"""
Display a list of addons or libraries with pages
Filter based on the request (type_id, username).
"""
# calculate which template to use
template_suffix = ''
packages = Package.objects.active()
author = None
if username:
try:
profile = Profile.objects.get_user_by_username_or_nick(username)
except ObjectDoesNotExist:
raise Http404
author = profile.user
packages = packages.filter(author__pk=author.pk)
template_suffix = '%s_user' % template_suffix
if type_id:
other_type = 'l' if type_id == 'a' else 'a'
other_packages_number = len(packages.filter(type=other_type))
packages = packages.filter(type=type_id)
template_suffix = '%s_%s' % (template_suffix,
settings.PACKAGE_PLURAL_NAMES[type_id])
packages = packages.sort_recently_active()
limit = request.GET.get('limit', settings.PACKAGES_PER_PAGE)
try:
pager = Paginator(
packages,
per_page=limit,
orphans=1
).page(page_number)
except (EmptyPage, InvalidPage):
raise Http404
packages = {
"all_public_addons": author.packages_originated.addons().active().count(),
"all_public_libraries": author.packages_originated.libraries().active().count()}
return render(request,
'package_browser%s.html' % template_suffix, {
'pager': pager,
'single': False,
'author': author,
'person': author,
'packages': packages,
'other_packages_number': other_packages_number
})
def view_or_edit(request, pk=None, id_number=None, type_id=None,
revision_number=None, version_name=None, latest=False):
"""
Edit if user is the author, otherwise view
"""
revision = get_package_revision(pk, id_number, type_id,
revision_number, version_name, latest)
edit_available = not waffle.switch_is_active('read_only')
if revision.package.deleted:
edit_available = False
if not request.user.is_authenticated():
raise Http404
try:
Package.objects.active_with_deleted(viewer=request.user).get(
pk=revision.package.pk)
except ObjectDoesNotExist:
raise Http404
if not revision.package.active:
if not request.user.is_authenticated():
raise Http404
try:
Package.objects.active_with_disabled(viewer=request.user).get(
pk=revision.package.pk)
except ObjectDoesNotExist:
raise Http404
if (edit_available
and request.user.is_authenticated()
and request.user.pk == revision.author.pk):
return edit(request, revision)
else:
return view(request, revision)
@login_required
def edit(request, revision):
"""
Edit package - only for the author
"""
if request.user.pk != revision.author.pk:
# redirecting to view mode without displaying an error
messages.info(request,
"Not sufficient priviliges to edit the source. "
"You've been redirected to view mode.")
return HttpResponseRedirect(
reverse(
"jp_%s_revision_details" % revision.package.get_type_name(),
args=[revision.package.id_number, revision.revision_number])
)
#return HttpResponseForbidden('You are not the author of this Package')
libraries = revision.dependencies.all()
library_counter = len(libraries)
sdk_list = None
if revision.package.is_addon():
library_counter += 1
sdk_list = SDK.objects.exclude_disabled(current=revision.sdk.version)
return render(request,
"%s_edit.html" % revision.package.get_type_name(), {
'revision': revision,
'item': revision.package,
'single': True,
'libraries': libraries,
'library_counter': library_counter,
'readonly': False,
'edit_mode': True,
'sdk_list': sdk_list})
def view(request, revision):
"""
Show package - read only
"""
libraries = revision.dependencies.all()
library_counter = len(libraries)
if revision.package.is_addon():
library_counter += 1
# prepare the json for the Tree
tree = simplejson.dumps({'Lib': revision.get_modules_tree(),
'Data': revision.get_attachments_tree(),
'Plugins': revision.get_dependencies_tree()})
return render(request,
"%s_view.html" % revision.package.get_type_name(), {
'revision': revision,
'libraries': libraries,
'library_counter': library_counter,
'readonly': True,
'tree': tree
})
def download_module(request, pk):
"""
return a JSON with all module info
"""
module = get_object_with_related_or_404(Module, pk=pk)
if not module.can_view(request.user):
log_msg = ("[security] Attempt to download private module (%s) by "
"non-owner (%s)" % (pk, request.user))
log.warning(log_msg)
return HttpResponseForbidden('You are not the author of this module.')
return HttpResponse(module.get_json())
def get_module(request, revision_id, filename):
"""
return a JSON with all module info
"""
try:
revision = PackageRevision.objects.get(pk=revision_id)
mod = revision.modules.get(filename=filename)
except PackageRevision.DoesNotExist, Module.DoesNotExist:
log_msg = 'No such module %s' % filename
log.error(log_msg)
raise Http404
if not mod.can_view(request.user):
log_msg = ("[security] Attempt to download private module (%s) by "
"non-owner (%s)" % (mod, request.user))
log.warning(log_msg)
return HttpResponseForbidden('You are not the author of this module.')
return HttpResponse(mod.get_json())
@transaction.commit_on_success
@login_required
def copy(request, revision_id):
"""
Copy package - create a duplicate of the Package, set user as author
"""
source = get_object_with_related_or_404(PackageRevision, pk=revision_id)
log.debug('[copy: %s] Copying started from (%s)' % (revision_id, source))
# save package
try:
package = source.package.copy(request.user)
except IntegrityError, err:
log.critical(("[copy: %s] Package copy failed") % revision_id)
return HttpResponseForbidden('You already have a %s with that name' %
escape(source.package.get_type_name()))
# save revision with all dependencies
source.save_new_revision(package)
copied = source
del source
log.info('[copy: %s] Copied to %s, (%s)' % (revision_id, copied.pk,
copied.full_name))
return render_json(request,
"json/%s_copied.json" % package.get_type_name(),
{'revision': copied})
@login_required
def disable(request, pk):
"""
Disable Package and return confirmation
"""
package = get_object_or_404(Package, pk=pk)
if request.user.pk != package.author.pk:
log_msg = 'User %s wanted to disable not his own Package %s.' % (
request.user, pk)
log.warning(log_msg)
return HttpResponseForbidden(
'You are not the author of this %s' % escape(
package.get_type_name()))
package.disable()
return render_json(request,
"json/package_disabled.json",
{'package': package})
@login_required
def activate(request, pk):
"""
Undelete Package and return confirmation
"""
package = get_object_or_404(Package, pk=pk)
if request.user.pk != package.author.pk:
log_msg = ("[security] Attempt to activate package (%s) by "
"non-owner (%s)" % (pk, request.user))
log.warning(log_msg)
return HttpResponseForbidden(
'You are not the author of this %s' % escape(
package.get_type_name()))
package.enable()
return render_json(request,
"json/package_activated.json",
{'package': package})
@login_required
def delete(request, pk):
"""
Delete Package and return confirmation
"""
package = get_object_or_404(Package, pk=pk)
if request.user.pk != package.author.pk:
log_msg = ("[security] Attempt to delete package (%s) by "
"non-owner (%s)" % (pk, request.user))
log.warning(log_msg)
return HttpResponseForbidden(
'You are not the author of this %s' % escape(
package.get_type_name()))
package.delete()
return render_json(request, "json/package_deleted.json")
@require_POST
@login_required
def add_module(request, revision_id):
"""
Add new module to the PackageRevision
"""
revision = get_object_with_related_or_404(PackageRevision, pk=revision_id)
if request.user.pk != revision.author.pk:
log_msg = ("[security] Attempt to add a module to package (%s) by "
"non-owner (%s)" % (id_number, request.user))
log.warning(log_msg)
return HttpResponseForbidden(
'You are not the author of this %s' % escape(
revision.package.get_type_name()))
filename = request.POST.get('filename')
mod = Module(
filename=filename,
author=request.user,
code="""// %s.js - %s's module
// author: %s""" % (filename, revision.package.full_name,
request.user.get_profile())
)
try:
mod.save()
revision.module_add(mod)
except FilenameExistException, err:
mod.delete()
return HttpResponseForbidden(escape(str(err)))
return render_json(request,
"json/module_added.json",
{'revision': revision, 'module': mod})
@require_POST
@login_required
def rename_module(request, revision_id):
"""
Rename a module in a PackageRevision
"""
revision = get_object_with_related_or_404(PackageRevision, pk=revision_id)
if request.user.pk != revision.author.pk:
log_msg = ("[security] Attempt to rename a module to package (%s) by "
"non-owner (%s)" % (revision_id, request.user))
log.warning(log_msg)
return HttpResponseForbidden('You are not the author of this Package')
old_name = request.POST.get('old_filename')
new_name = request.POST.get('new_filename')
if old_name == 'main':
return HttpResponseForbidden(
'Sorry, you cannot change the name of the main module.'
)
if not revision.validate_module_filename(new_name):
return HttpResponseForbidden(
('Sorry, there is already a module in your add-on '
'with the name "%s". Each module in your add-on '
'needs to have a unique name.') % new_name
)
modules = revision.modules.all()
module = None
for mod in modules:
if mod.filename == old_name:
module = mod
if not module:
log_msg = 'Attempt to rename a non existing module %s from %s.' % (
old_name, revision_id)
log.warning(log_msg)
return HttpResponseForbidden(
'There is no such module in %s' % escape(
revision.package.full_name))
module.filename = new_name
revision.add_commit_message('module renamed')
revision.update(module)
return render_json(request,
"json/module_renamed.json",
{'revision': revision, 'module': module})
@require_POST
@login_required
def remove_module(request, revision_id):
"""
Remove module from PackageRevision
"""
revision = get_object_with_related_or_404(PackageRevision, pk=revision_id)
if request.user.pk != revision.author.pk:
log_msg = ("[security] Attempt to remove a module from package (%s) "
"by non-owner (%s)" % (revision_id, request.user))
log.warning(log_msg)
return HttpResponseForbidden('You are not the author of this Package')
filenames = request.POST.get('filename').split(',')
revision.add_commit_message('module removed')
try:
removed_modules, removed_dirs = revision.modules_remove_by_path(
filenames)
except Module.DoesNotExist:
log_msg = 'Attempt to delete a non existing module(s) %s from %s.' % (
str(filenames), revision_id)
log.warning(log_msg)
return HttpResponseForbidden(
'There is no such module in %s' % escape(
revision.package.full_name))
return render_json(request,
"json/module_removed.json",
{'revision': revision,
'removed_modules': simplejson.dumps(removed_modules),
'removed_dirs': simplejson.dumps(removed_dirs)})
@require_POST
@login_required
def add_folder(request, revision_id):
" adds an EmptyDir to a revision "
revision = get_object_with_related_or_404(PackageRevision, pk=revision_id)
if request.user.pk != revision.author.pk:
log_msg = ("[security] Attempt to add a folder to revision (%s) by "
"non-owner (%s)" % (revision_id, request.user))
log.warning(log_msg)
return HttpResponseForbidden('You are not the author of this Package')
foldername, root = (
request.POST.get('name', ''),
request.POST.get('root_dir'))
dir = EmptyDir(name=foldername, author=request.user, root_dir=root)
try:
dir.save()
revision.folder_add(dir)
except FilenameExistException, err:
dir.delete()
return HttpResponseForbidden(escape(str(err)))
return render_json(request,
"json/folder_added.json",
{'revision': revision, 'folder': dir})
@require_POST
@login_required
def remove_folder(request, revision_id):
" removes an EmptyDir from a revision "
revision = get_object_with_related_or_404(PackageRevision, pk=revision_id)
if request.user.pk != revision.author.pk:
log_msg = ("[security] Attempt to remove a folder from revision (%s) "
"by non-owner (%s)" % (revision_id, request.user))
log.warning(log_msg)
return HttpResponseForbidden('You are not the author of this Package')
foldername, root = (
pathify(request.POST.get('name', '')),
request.POST.get('root_dir'))
try:
folder = revision.folders.get(name=foldername, root_dir=root)
except EmptyDir.DoesNotExist:
response = None
if root == 'data':
response = revision.attachment_rmdir(foldername)
if not response:
log_msg = 'Attempt to delete a non existing folder %s from %s.' % (
foldername, revision_id)
log.warning(log_msg)
return HttpResponseForbidden(
'There is no such folder in %s' % escape(
revision.package.full_name))
revision, removed_attachments, removed_emptydirs = response
return render_json(request,
'json/%s_rmdir.json' % root, {
'revision': revision, 'path': foldername,
'removed_attachments': simplejson.dumps(removed_attachments),
'removed_dirs': simplejson.dumps(removed_emptydirs),
'foldername': foldername})
else:
revision.folder_remove(folder)
return render_json(request,
"json/folder_removed.json",
{'revision': revision, 'folder': folder})
@require_POST
@login_required
def switch_sdk(request, revision_id):
" switch SDK used to create XPI - sdk_id from POST "
revision = get_object_with_related_or_404(PackageRevision, pk=revision_id)
if request.user.pk != revision.author.pk:
return HttpResponseForbidden('You are not the author of this Add-on')
sdk_id = request.POST.get('id', None)
sdk = get_object_or_404(SDK, id=sdk_id)
old_sdk = revision.sdk
log.info('Addon %s (%s) switched from Add-on Kit version %s to %s' % (
revision.package.full_name, revision.package.id_number,
old_sdk.version, sdk.version))
revision.sdk = sdk
revision.add_commit_message('Switched to Add-on Kit %s' % sdk.version)
revision.save()
return render_json(request,
"json/sdk_switched.json",
{'revision': revision, 'sdk': sdk,
'sdk_lib': revision.get_sdk_revision()})
@require_POST
@login_required
def upload_attachment(request, revision_id):
""" Upload new attachment to the PackageRevision
"""
revision = get_object_with_related_or_404(PackageRevision, pk=revision_id)
log.debug(revision)
if request.user.pk != revision.author.pk:
log_msg = ("[security] Attempt to upload attachment to package (%s) "
"by non-owner (%s)" % (revision_id, request.user))
log.warning(log_msg)
return HttpResponseForbidden(
'You are not the author of this %s' % escape(
revision.package.get_type_name()))
f = request.FILES.get('upload_attachment')
filename = request.META.get('HTTP_X_FILE_NAME')
if not f:
log_msg = 'Path not found: %s, revision: %s.' % (
filename, revision_id)
log.error(log_msg)
return HttpResponseServerError('Path not found.')
content = f.read()
# try to force UTF-8 code, on error continue with original data
try:
content = unicode(content, 'utf-8')
except:
pass
try:
attachment = revision.attachment_create_by_filename(
request.user, filename, content)
except ValidationError, e:
return HttpResponseForbidden(
'Validation errors.\n%s' % parse_validation_messages(e))
except Exception, e:
return HttpResponseForbidden(str(e))
return render_json(request,
"json/attachment_added.json",
{'revision': revision, 'attachment': attachment})
@require_POST
@login_required
def upload_attachments(request, id_number, type_id,
revision_number=None, version_name=None):
""" Upload new attachments to the PackageRevision
"""
revision = get_package_revision(None, id_number, type_id, revision_number,
version_name)
if request.user.pk != revision.author.pk:
log_msg = ("[security] Attempt to upload attachment to package (%s) "
"by non-owner (%s)" % (id_number, request.user))
log.warning(log_msg)
return HttpResponseForbidden(
'You are not the author of this %s' % escape(
revision.package.get_type_name()))
content = request.raw_post_data
filename = request.META.get('HTTP_X_FILE_NAME')
if not filename:
log_msg = 'Path not found: %s, package: %s.' % (
filename, id_number)
log.error(log_msg)
return HttpResponseServerError('Path not found.')
try:
attachment = revision.attachment_create_by_filename(
request.user, filename, content)
except ValidationError, e:
return HttpResponseForbidden(
'Validation errors.\n%s' % parse_validation_messages(e))
except Exception, e:
return HttpResponseForbidden(str(e))
return render_json(request,
"json/attachment_added.json",
{'revision': revision, 'attachment': attachment})
@require_POST
@login_required
def add_empty_attachment(request, id_number, type_id,
revision_number=None, version_name=None):
""" Add new empty attachment to the PackageRevision
"""
revision = get_package_revision(None, id_number, type_id, revision_number,
version_name)
if request.user.pk != revision.author.pk:
log_msg = ("[security] Attempt to add attachment to package (%s) by "
"non-owner (%s)" % (id_number, request.user))
log.warning(log_msg)
return HttpResponseForbidden(
'You are not the author of this %s' % escape(
revision.package.get_type_name()))
filename = request.POST.get('filename', False)
if not filename:
log_msg = 'Path not found: %s, package: %s.' % (
filename, id_number)
log.error(log_msg)
return HttpResponseServerError('Path not found.')
try:
attachment = revision.attachment_create_by_filename(request.user,
filename, '')
except ValidationError, e:
return HttpResponseForbidden(
'Validation errors.\n%s' % parse_validation_messages(e))
except Exception, e:
return HttpResponseForbidden(str(e))
return render_json(request,
"json/attachment_added.json",
{'revision': revision, 'attachment': attachment})
@require_POST
@login_required
def revision_add_attachment(request, pk):
"""Add attachment, download if necessary
"""
revision = get_object_or_404(PackageRevision, pk=pk)
if request.user.pk != revision.author.pk:
log_msg = ("[security] Attempt to add attachment to package (%s) by "
"non-owner (%s)" % (revision.package, request.user))
log.warning(log_msg)
return HttpResponseForbidden(
'You are not the author of this %s' % escape(
revision.package.get_type_name()))
url = request.POST.get('url', None)
filename = request.POST.get('filename', None)
if not filename or filename == "":
log.error('Trying to create an attachment without name')
return HttpResponseBadRequest('Path not found.')
content = ''
if url:
log.info(('[%s] Preparing to download %s as an attachment of '
'PackageRevision %d') % (filename, url, revision.pk))
# validate url
field = URLField(verify_exists=True)
encoding = request.POST.get('force_contenttype', False)
try:
url = field.clean(url)
except ValidationError, err:
log.warning('[%s] Invalid url provided\n%s' % (url,
'\n'.join(err.messages)))
raise Http404()
except Exception, err:
log.warning('[%s] Exception raised\n%s' % (url, str(err)))
raise Http404()
try:
att = urllib2.urlopen(url, timeout=settings.URLOPEN_TIMEOUT)
except Exception, err:
log.warning('[%s] Exception raised by opening url\n%s' % (url, str(err)))
raise Http404()
# validate filesize
att_info = att.info()
if 'content-length' in att_info.dict:
att_size = int(att_info.dict['content-length'])
if att_size > settings.ATTACHMENT_MAX_FILESIZE:
log.warning('[%s] File is too big (%db)' % (url, att_size))
return HttpResponseBadRequest("Loading attachment failed\n"
"File is too big")
# download attachment's content
log.debug('[%s] Downloading' % url)
try:
content = att.read(settings.ATTACHMENT_MAX_FILESIZE + 1)
except Exception, err:
log.warning('[%s] Exception raised by reading url\n%s' % (url, str(err)))
raise Http404()
# work out the contenttype
basename, ext = os.path.splitext(filename)
unicode_contenttypes = ('utf-8',)
ext = ext.split('.')[1].lower() if ext else None
if not encoding:
encoding = att.headers['content-type'].split('charset=')[-1]
if encoding not in unicode_contenttypes and ext in EDITABLE_EXTENSIONS:
log.info('[%s] Forcing the "utf-8" encoding from '
'"%s"' % (url, encoding))
encoding = 'utf-8'
# convert to unicode if needed
if encoding in unicode_contenttypes:
content = unicode(content, encoding)
if len(content) >= settings.ATTACHMENT_MAX_FILESIZE + 1:
log.warning('[%s] Downloaded file is too big' % url)
return HttpResponseBadRequest("Loading attachment failed\n"
"File is too big")
log.info('[%s] Downloaded %db, encoding: %s' % (url, len(content),
encoding))
att.close()
try:
attachment = revision.attachment_create_by_filename(
request.user, filename, content)
except ValidationError, err:
log.warning("[%s] Validation error.\n%s" % (filename, str(err)))
return HttpResponseForbidden(
'Validation error.\n%s' % parse_validation_messages(err))
except Exception, err:
log.warning("[%s] Exception raised\n%s" % (filename, str(err)))
return HttpResponseForbidden(str(err))
return render_json(request,
"json/attachment_added.json",
{'revision': revision, 'attachment': attachment})
@require_POST
@login_required
@transaction.commit_on_success
def rename_attachment(request, revision_id):
"""
Rename an attachment in a PackageRevision
"""
revision = get_object_with_related_or_404(PackageRevision, pk=revision_id)
if request.user.pk != revision.author.pk:
log_msg = ("[security] Attempt to rename attachment in revision (%s) "
"by non-owner (%s)" % (revision_id, request.user))
log.warning(log_msg)
return HttpResponseForbidden('You are not the author of this Package')
uid = request.POST.get('uid', '').strip()
try:
attachment = revision.attachments.get(pk=uid)
except:
log_msg = ('Attempt to rename a non existing attachment. attachment: '
'%s, revision: %s.' % (uid, revision))
log.warning(log_msg)
return HttpResponseForbidden(
'There is no such attachment in %s' % escape(
revision.package.full_name))
new_name = request.POST.get('new_filename')
new_ext = request.POST.get('new_ext') or attachment.ext
if not revision.validate_attachment_filename(new_name, new_ext):
return HttpResponseForbidden(
('Sorry, there is already an attachment in your add-on '
'with the name "%s.%s". Each attachment in your add-on '
'needs to have a unique name.') % (new_name, attachment.ext)
)
attachment.filename = new_name
attachment.ext = new_ext
try:
attachment = revision.update(attachment)
except ValidationError, err:
return HttpResponseForbidden(str(err))
return render_json(request,
"json/attachment_renamed.json",
{'revision': revision, 'attachment': attachment})
@require_POST
@login_required
def rmdir(request, pk, target, path):
"""
Remove attachment from PackageRevision
"""
revision = get_object_or_404(PackageRevision, pk=pk)
if target not in ['data', 'lib']:
return HttpResponseForbidden
if target == 'lib':
return HttpResponseForbidden('not supported yet')
revision.attachment_rmdir(path) if target == 'data' else \
revision.modules_rmdir(path)
return render_json(request,
'%s_rmdir.json' % target, {'revision': revision, 'path': path})
@require_POST
@login_required
def remove_attachment(request, revision_id):
"""
Remove attachment from PackageRevision
"""
revision = get_object_with_related_or_404(PackageRevision, pk=revision_id)
if request.user.pk != revision.author.pk:
log_msg = ('[security] Attempt to remove attachment from revision '
'(%s) by non-owner (%s)' % (revision_id, request.user))
log.warning(log_msg)
return HttpResponseForbidden('You are not the author of this Package')
uid = request.POST.get('uid', '').strip()
attachment = get_object_with_related_or_404(Attachment,
pk=uid, revisions=revision)
if not attachment:
log_msg = ('Attempt to remove a non existing attachment. attachment: '
'%s, revision: %s.' % (uid, revision_id))
log.warning(log_msg)
return HttpResponseForbidden(
'There is no such attachment in %s' % escape(
revision.package.full_name))
revision.attachment_remove(attachment)
return render_json(request,
"json/attachment_removed.json",
{'revision': revision, 'attachment': attachment})
def download_attachment(request, uid):
"""
Display attachment from PackageRevision
"""
attachment = get_object_with_related_or_404(Attachment, id=uid)
if not attachment.can_view(request.user):
log_msg = ("[security] Attempt to download private attachment (%s) by "
"non-owner (%s)" % (uid, request.user))
log.warning(log_msg)
return HttpResponseForbidden(
'You are not the author of this attachment.')
response = serve(request, attachment.path,
settings.UPLOAD_DIR, show_indexes=False)
response['Content-Disposition'] = 'filename=%s.%s' % (
attachment.filename, attachment.ext)
return response
@require_POST
@login_required
def save(request, revision_id, type_id=None):
"""
Save package and modules
@TODO: check how dynamic module loading affects save
"""
revision = get_object_with_related_or_404(PackageRevision, pk=revision_id)
if request.user.pk != revision.author.pk:
log_msg = ("[security] Attempt to save package (%s) by "
"non-owner (%s)" % (revision.pk,
request.user))
log.warning(log_msg)
return HttpResponseForbidden('You are not the author of this Package')
save_revision = False
save_package = False
start_version_name = revision.version_name
start_revision_message = revision.message
response_data = {}
package_full_name = request.POST.get('full_name', False)
jid = request.POST.get('jid', None)
version_name = request.POST.get('version_name', False)
if jid and not validator.is_valid(
'alphanum_plus', jid):
return HttpResponseForbidden(escape(
validator.get_validation_message('alphanum_plus')))
# validate package_full_name and version_name
if version_name and not validator.is_valid(
'alphanum_plus', version_name):
return HttpResponseForbidden(escape(
validator.get_validation_message('alphanum_plus')))
# here we're checking if the *current* full_name is different than the
# revision's full_name
if package_full_name and package_full_name != revision.package.full_name:
try:
revision.set_full_name(package_full_name)
except ValidationError:
return HttpResponseForbidden(escape(
validator.get_validation_message('alphanum_plus_space')))
except IntegrityError:
return HttpResponseForbidden(
'You already have a %s with that name' % escape(
revision.package.get_type_name())
)
else:
save_package = True
save_revision = True
response_data['full_name'] = package_full_name
package_description = request.POST.get('package_description', False)
if package_description:
save_package = True
revision.package.description = package_description
response_data['package_description'] = package_description
extra_json = request.POST.get('package_extra_json')
if extra_json is not None:
# None means it wasn't submitted. We want to accept blank strings.
save_revision = True
try:
revision.set_extra_json(extra_json, save=False)
except JSONDecodeError:
return HttpResponseBadRequest(
'Extra package properties were invalid JSON.')
except IllegalFilenameException, e:
return HttpResponseBadRequest(str(e))
except KeyNotAllowed, e:
return HttpResponseForbidden(str(e))
response_data['package_extra_json'] = extra_json
changes = []
for mod in revision.modules.all():
if request.POST.get(mod.filename, False):
code = request.POST[mod.filename]
if mod.code != code:
mod.code = code
changes.append(mod)
for att in revision.attachments.all():
uid = str(att.pk)
if request.POST.get(uid):
att.data = request.POST[uid]
if att.changed():
changes.append(att)
attachments_changed = {}
if save_revision or changes:
try:
revision.save()
except ValidationError, err:
return HttpResponseForbidden(
'Validation error.\n%s' % parse_validation_messages(err))
if changes:
attachments_changed = simplejson.dumps(
revision.updates(changes, save=False))
revision_message = request.POST.get('revision_message', False)
if revision_message and revision_message != start_revision_message:
revision.message = revision_message
# save revision message without changeing the revision
super(PackageRevision, revision).save()
response_data['revision_message'] = revision_message
if version_name and version_name != start_version_name \
and version_name != revision.package.version_name:
save_package = False
try:
revision.set_version(version_name)
except Exception, err:
return HttpResponseForbidden(escape(err.__str__()))
if jid:
try:
Package.objects.get(jid=jid)
except Package.DoesNotExist:
pass
else:
return HttpResponseForbidden(('Package with JID "%s" already '
'exists in the Builder') % jid)
revision.package.jid = jid
response_data['jid'] = jid