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)))
def test_folder_removed_when_modules_added(self): " EmptyDir's shouldn't exist if there are modules inside the 'dir' " addon = Package(author=self.author, type='a') addon.save() revision = PackageRevision.objects.filter(package__name=addon.name)[0] folder = EmptyDir(name=self.path, author=self.author, root_dir='l') folder.save() revision.folder_add(folder) self.assertEqual(1, revision.folders.count()) mod = Module(filename='/'.join([self.path, 'helpers']), author=self.author, code='//test code') mod.save() revision.module_add(mod) self.assertEqual(0, revision.folders.count()) mod = Module(filename='model', author=self.author, code='//test code') mod.save() revision.module_add(mod) self.assertEqual(0, revision.folders.count())
def test_folder_added_when_modules_removed(self): " EmptyDir's should be added if all modules in a 'dir' are removed " addon = Package(author=self.author, type='a') addon.save() revision = PackageRevision.objects.filter(package__name=addon.name)[0] mod = Module(filename='/'.join([self.path, 'helpers']), author=self.author, code='//test code') mod.save() revision.module_add(mod) self.assertEqual(0, revision.folders.count()) revision.module_remove(mod) self.assertEqual(1, revision.folders.count()) self.assertEqual(self.path, revision.folders.all()[0].name)
def run(*args, **kwargs): libs = PackageRevision.objects.filter(package__type='l', module_main='main') log.info('%d library revisions updated module_main to "index".' % libs.count()) libs.update(module_main=LIB_MODULE_MAIN) libs = PackageRevision.objects.filter( package__type='l').select_related('modules') main_per_package = {} for revision in libs: if revision.modules.filter(filename=LIB_MODULE_MAIN).count() == 0: mod = main_per_package.get(revision.package_id) if not mod: mod = Module(filename=LIB_MODULE_MAIN, author=revision.author) mod.save() main_per_package[revision.package_id] = mod revision.modules.add(mod)
def test_illegal_filename(self): mod = Module(filename='.../...///foo.js', author=User.objects.get(username='******')) self.assertRaises(IllegalFilenameException, mod.save)