<s> import os <EOL> from django . conf import settings <EOL> from django . http import ( HttpResponse , HttpResponseForbidden , <EOL> HttpResponseBadRequest ) <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . contrib . auth import login <EOL> from djangopypi . http import login_basic_auth , HttpResponseUnauthorized <EOL> from djangopypi . forms import ProjectForm , ReleaseForm <EOL> from djangopypi . models import Project , Release , Classifier , UPLOAD_TO <EOL> ALREADY_EXISTS_FMT = _ ( <EOL> "<STR_LIT>" ) <EOL> def submit_project_or_release ( user , post_data , files ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> project = Project . objects . get ( name = post_data [ '<STR_LIT:name>' ] ) <EOL> if project . owner != user : <EOL> return HttpResponseForbidden ( <EOL> "<STR_LIT>" ) <EOL> except Project . DoesNotExist : <EOL> project = None <EOL> project_form = ProjectForm ( post_data , instance = project ) <EOL> if project_form . is_valid ( ) : <EOL> project = project_form . save ( commit = False ) <EOL> project . owner = user <EOL> project . save ( ) <EOL> for c in post_data . getlist ( '<STR_LIT>' ) : <EOL> classifier , created = Classifier . objects . get_or_create ( name = c ) <EOL> project . classifiers . add ( classifier ) <EOL> if files : <EOL> allow_overwrite = getattr ( settings , <EOL> "<STR_LIT>" , False ) <EOL> try : <EOL> release = Release . objects . get ( version = post_data [ '<STR_LIT:version>' ] , <EOL> project = project , <EOL> distribution = UPLOAD_TO + '<STR_LIT:/>' + <EOL> files [ '<STR_LIT>' ] . _name ) <EOL> if not allow_overwrite : <EOL> return HttpResponseForbidden ( ALREADY_EXISTS_FMT % ( <EOL> release . filename , release ) ) <EOL> except Release . DoesNotExist : <EOL> release = None <EOL> release_form = ReleaseForm ( post_data , files , instance = release ) <EOL> if release_form . is_valid ( ) : <EOL> if release and os . path . exists ( release . distribution . path ) : <EOL> os . remove ( release . distribution . path ) <EOL> release = release_form . save ( commit = False ) <EOL> release . project = project <EOL> release . save ( ) <EOL> else : <EOL> return HttpResponseBadRequest ( <EOL> "<STR_LIT>" % release_form . errors ) <EOL> else : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" % project_form . errors ) <EOL> return HttpResponse ( ) <EOL> def register_or_upload ( request , post_data , files ) : <EOL> user = login_basic_auth ( request ) <EOL> if not user : <EOL> return HttpResponseUnauthorized ( '<STR_LIT>' ) <EOL> login ( request , user ) <EOL> if not request . user . is_authenticated ( ) : <EOL> return HttpResponseForbidden ( <EOL> "<STR_LIT>" ) <EOL> return submit_project_or_release ( user , post_data , files ) </s>
