def submit_package_or_release(user, post_data, files): """Registers/updates a package or release""" log.debug('submit_package_or_release(user=%s)' % (unicode(user),)) try: package = Package.objects.get(name=post_data['name']) if user not in package.owners.all(): log.error('%s is updating a package they don\'t own: %s' % (unicode(user),unicode(package),)) return HttpResponseForbidden( "That package is owned by someone else!") except Package.DoesNotExist: log.info('%s is creating a new package: %s' % (unicode(user), unicode(post_data['name']),)) package = None package_form = PackageForm(post_data, instance=package) if package_form.is_valid(): package = package_form.save(commit=False) package.owner = user package.save() for c in post_data.getlist('classifiers'): classifier, created = Classifier.objects.get_or_create(name=c) package.classifiers.add(classifier) if files: allow_overwrite = getattr(settings, "DJANGOPYPI_ALLOW_VERSION_OVERWRITE", False) try: release = Release.objects.get(version=post_data['version'], package=package, distribution=UPLOAD_TO + '/' + files['distribution']._name) if not allow_overwrite: return HttpResponseForbidden(ALREADY_EXISTS_FMT % ( release.filename, release)) except Release.DoesNotExist: release = None # If the old file already exists, django will append a _ after the # filename, however with .tar.gz files django does the "wrong" # thing and saves it as package-0.1.2.tar_.gz. So remove it before # django sees anything. release_form = ReleaseForm(post_data, files, instance=release) if release_form.is_valid(): if release and os.path.exists(release.distribution.path): os.remove(release.distribution.path) release = release_form.save(commit=False) release.package = package release.save() else: return HttpResponseBadRequest( "ERRORS: %s" % release_form.errors) else: return HttpResponseBadRequest("ERRORS: %s" % package_form.errors) return HttpResponse()
def submit_package_or_release(user, post_data, files): """Registers/updates a package or release""" try: package = Package.objects.get(name=post_data['name']) if not (conf.GLOBAL_OWNERSHIP or user in package.owners.all()): return HttpResponseForbidden( "That package is owned by someone else!") except Package.DoesNotExist: package = None package_form = PackageForm(post_data, instance=package) if package_form.is_valid(): package = package_form.save(commit=False) package.owner = user package.save() for c in post_data.getlist('classifiers'): classifier, created = Classifier.objects.get_or_create(name=c) package.classifiers.add(classifier) if files: allow_overwrite = getattr(settings, "DJANGOPYPI_ALLOW_VERSION_OVERWRITE", False) try: release = Release.objects.get( version=post_data['version'], package=package, distribution=settings.DJANGOPYPI_RELEASE_UPLOAD_TO + '/' + files['distribution']._name) if not allow_overwrite: return HttpResponseForbidden(ALREADY_EXISTS_FMT % (release.filename, release)) except Release.DoesNotExist: release = None # If the old file already exists, django will append a _ after the # filename, however with .tar.gz files django does the "wrong" # thing and saves it as package-0.1.2.tar_.gz. So remove it before # django sees anything. release_form = ReleaseForm(post_data, files, instance=release) if release_form.is_valid(): if release and os.path.exists(release.distribution.path): os.remove(release.distribution.path) release = release_form.save(commit=False) release.package = package release.save() else: return HttpResponseBadRequest("ERRORS: %s" % release_form.errors) else: return HttpResponseBadRequest("ERRORS: %s" % package_form.errors) return HttpResponse()
def submit_project_or_release(user, post_data, files): """Registers/updates a project or release""" try: project = Project.objects.get(name=post_data['name']) if project.owner != user: return HttpResponseForbidden( "That project is owned by someone else!") except Project.DoesNotExist: project = None project_form = ProjectForm(post_data, instance=project) if project_form.is_valid(): project = project_form.save(commit=False) project.owner = user project.save() for c in post_data.getlist('classifiers'): classifier, created = Classifier.objects.get_or_create(name=c) project.classifiers.add(classifier) if files: allow_overwrite = getattr(settings, "DJANGOPYPI_ALLOW_VERSION_OVERWRITE", False) try: release = Release.objects.get(version=post_data['version'], project=project, distribution=UPLOAD_TO + '/' + files['distribution']._name) if not allow_overwrite: return HttpResponseForbidden(ALREADY_EXISTS_FMT % ( release.filename, release)) except Release.DoesNotExist: release = None # If the old file already exists, django will append a _ after the # filename, however with .tar.gz files django does the "wrong" # thing and saves it as project-0.1.2.tar_.gz. So remove it before # django sees anything. release_form = ReleaseForm(post_data, files, instance=release) if release_form.is_valid(): if release and os.path.exists(release.distribution.path): os.remove(release.distribution.path) release = release_form.save(commit=False) release.project = project release.save() else: return HttpResponseBadRequest( "ERRORS: %s" % release_form.errors) else: return HttpResponseBadRequest("ERRORS: %s" % project_form.errors) return HttpResponse()