<s> import json <EOL> from django . db import transaction <EOL> from django . shortcuts import render <EOL> from django . shortcuts import redirect <EOL> from django . views . generic import View <EOL> from django . http import HttpResponse <EOL> from django . http import HttpResponseRedirect <EOL> from django . http import HttpResponseNotFound <EOL> from django . http import HttpResponseForbidden <EOL> from django . http import HttpResponseBadRequest <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . exceptions import ValidationError <EOL> from . models import Package <EOL> from sublimall . mixins import APIMixin <EOL> from sublimall . mixins import LoginRequiredMixin <EOL> class UploadPackageAPIView ( APIMixin , View ) : <EOL> @ transaction . atomic <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> email = request . FILES . get ( '<STR_LIT:email>' ) <EOL> api_key = request . FILES . get ( '<STR_LIT>' ) <EOL> version = request . FILES . get ( '<STR_LIT:version>' ) <EOL> platform = request . FILES . get ( '<STR_LIT>' ) <EOL> arch = request . FILES . get ( '<STR_LIT>' ) <EOL> package_file = request . FILES . get ( '<STR_LIT>' ) <EOL> package_size = None <EOL> if email : <EOL> email = email . read ( ) <EOL> if api_key : <EOL> api_key = api_key . read ( ) <EOL> if version : <EOL> version = version . read ( ) <EOL> if platform : <EOL> platform = platform . read ( ) <EOL> if arch : <EOL> arch = arch . read ( ) <EOL> if package_file : <EOL> package_size = package_file . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> if not email or not api_key or not package_size or not version : <EOL> message = { '<STR_LIT:success>' : False , '<STR_LIT>' : [ ] } <EOL> if not email : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if not api_key : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if not version : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if not package_size : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> return HttpResponseBadRequest ( json . dumps ( message ) ) <EOL> member = self . get_member ( email , api_key ) <EOL> if member is None : <EOL> return HttpResponseForbidden ( <EOL> json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> if not member . is_active : <EOL> return HttpResponseForbidden ( <EOL> json . dumps ( <EOL> { '<STR_LIT:success>' : False , '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> try : <EOL> version = int ( version ) <EOL> except ValueError : <EOL> return HttpResponseBadRequest ( json . dumps ( { <EOL> '<STR_LIT:success>' : False , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> if version not in [ <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> return HttpResponseBadRequest ( json . dumps ( { <EOL> '<STR_LIT:success>' : False , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> package = Package ( <EOL> member = member , <EOL> version = version , <EOL> platform = platform , <EOL> arch = arch , <EOL> package = package_file ) <EOL> try : <EOL> package . full_clean ( ) <EOL> except ValidationError as err : <EOL> return HttpResponseBadRequest ( <EOL> json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT>' : err . messages } ) ) <EOL> package . save ( ) <EOL> old_packages = member . package_set . filter ( <EOL> version = version ) . exclude ( pk = package . pk ) <EOL> for old_package in old_packages : <EOL> old_package . delete ( ) <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:success>' : True } ) , status = <NUM_LIT> ) <EOL> class DownloadPackageAPIView ( APIMixin , View ) : <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> email = request . POST . get ( '<STR_LIT:email>' ) <EOL> api_key = request . POST . get ( '<STR_LIT>' ) <EOL> version = request . POST . get ( '<STR_LIT:version>' ) <EOL> package_id = request . POST . get ( '<STR_LIT>' ) <EOL> if not email or not api_key and not version : <EOL> message = { '<STR_LIT:success>' : False , '<STR_LIT>' : [ ] } <EOL> if not email : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if not api_key : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if not version : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> return HttpResponseBadRequest ( json . dumps ( message ) ) <EOL> member = self . get_member ( email , api_key ) <EOL> if member is None : <EOL> return HttpResponseForbidden ( <EOL> json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> if not member . is_active : <EOL> return HttpResponseForbidden ( <EOL> json . dumps ( <EOL> { '<STR_LIT:success>' : False , '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> if package_id and not member . package_set . get ( pk = package_id ) : <EOL> return HttpResponseForbidden ( <EOL> json . dumps ( <EOL> { <EOL> '<STR_LIT:success>' : False , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> try : <EOL> if package_id : <EOL> package = member . package_set . only ( '<STR_LIT>' ) . get ( pk = package_id ) <EOL> else : <EOL> package = member . package_set . filter ( <EOL> version = version ) . only ( '<STR_LIT>' ) . latest ( '<STR_LIT>' ) <EOL> except Package . DoesNotExist : <EOL> return HttpResponseNotFound ( <EOL> json . dumps ( <EOL> { '<STR_LIT:success>' : False , '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> response = HttpResponse ( <EOL> package . package . read ( ) , <EOL> content_type = '<STR_LIT>' ) <EOL> response . streaming = True <EOL> response [ '<STR_LIT>' ] = ( <EOL> '<STR_LIT>' % package . version ) <EOL> return response <EOL> class DeletePackageView ( LoginRequiredMixin , View ) : <EOL> def get ( self , request , ** kwargs ) : <EOL> try : <EOL> package = request . user . package_set . get ( pk = kwargs . get ( '<STR_LIT>' ) ) <EOL> except Package . DoesNotExist : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : package } ) <EOL> def post ( self , request , pk ) : <EOL> try : <EOL> package = request . user . package_set . get ( pk = pk ) <EOL> except Package . DoesNotExist : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> package . delete ( ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> class DeletePackageAPIView ( APIMixin , View ) : <EOL> def post ( self , request ) : <EOL> email = request . POST . get ( '<STR_LIT:email>' ) <EOL> api_key = request . POST . get ( '<STR_LIT>' ) <EOL> version = request . POST . get ( '<STR_LIT:version>' ) <EOL> if not email or not api_key or not version : <EOL> message = { '<STR_LIT:success>' : False , '<STR_LIT>' : [ ] } <EOL> if not email : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if not api_key : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if not version : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> return HttpResponseBadRequest ( json . dumps ( message ) ) <EOL> member = self . get_member ( email , api_key ) <EOL> if member is None : <EOL> return HttpResponseForbidden ( <EOL> json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> try : <EOL> package = member . package_set . filter ( version = version ) <EOL> except Package . DoesNotExist : <EOL> return HttpResponseNotFound ( <EOL> json . dumps ( <EOL> { '<STR_LIT:success>' : False , '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> package . delete ( ) <EOL> return HttpResponse ( { '<STR_LIT:success>' : True } ) <EOL> class MaxPackageSizeAPIView ( APIMixin , View ) : <EOL> def post ( self , request ) : <EOL> email = request . POST . get ( '<STR_LIT:email>' ) <EOL> api_key = request . POST . get ( '<STR_LIT>' ) <EOL> if not email or not api_key : <EOL> message = { '<STR_LIT:success>' : False , '<STR_LIT>' : [ ] } <EOL> if not email : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> if not api_key : <EOL> message [ '<STR_LIT>' ] . append ( '<STR_LIT>' ) <EOL> return HttpResponseBadRequest ( json . dumps ( message ) ) <EOL> member = self . get_member ( email , api_key ) <EOL> if member is None : <EOL> return HttpResponseForbidden ( <EOL> json . dumps ( { '<STR_LIT:success>' : False , '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> return HttpResponse ( <EOL> json . dumps ( <EOL> { '<STR_LIT:success>' : True , '<STR_LIT>' : member . get_storage_limit ( ) } ) ) </s>
