<s> from __future__ import absolute_import <EOL> from django . db import IntegrityError , transaction <EOL> from django . utils import timezone <EOL> from rest_framework import serializers <EOL> from rest_framework . response import Response <EOL> from sentry . api . base import DocSection <EOL> from sentry . api . bases . project import ProjectEndpoint <EOL> from sentry . api . fields . user import UserField <EOL> from sentry . api . serializers import serialize <EOL> from sentry . models import Activity , Release <EOL> from sentry . utils . apidocs import scenario , attach_scenarios <EOL> @ scenario ( '<STR_LIT>' ) <EOL> def create_new_release_scenario ( runner ) : <EOL> runner . request ( <EOL> method = '<STR_LIT:POST>' , <EOL> path = '<STR_LIT>' % ( <EOL> runner . org . slug , runner . default_project . slug ) , <EOL> data = { <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ) <EOL> @ scenario ( '<STR_LIT>' ) <EOL> def list_releases_scenario ( runner ) : <EOL> runner . request ( <EOL> method = '<STR_LIT:GET>' , <EOL> path = '<STR_LIT>' % ( <EOL> runner . org . slug , runner . default_project . slug ) <EOL> ) <EOL> class ReleaseSerializer ( serializers . Serializer ) : <EOL> version = serializers . RegexField ( r'<STR_LIT>' , max_length = <NUM_LIT:64> , required = True ) <EOL> ref = serializers . CharField ( max_length = <NUM_LIT:64> , required = False ) <EOL> url = serializers . URLField ( required = False ) <EOL> owner = UserField ( required = False ) <EOL> dateStarted = serializers . DateTimeField ( required = False ) <EOL> dateReleased = serializers . DateTimeField ( required = False ) <EOL> class ProjectReleasesEndpoint ( ProjectEndpoint ) : <EOL> doc_section = DocSection . RELEASES <EOL> @ attach_scenarios ( [ list_releases_scenario ] ) <EOL> def get ( self , request , project ) : <EOL> """<STR_LIT>""" <EOL> query = request . GET . get ( '<STR_LIT>' ) <EOL> queryset = Release . objects . filter ( <EOL> project = project , <EOL> ) . select_related ( '<STR_LIT>' ) <EOL> if query : <EOL> queryset = queryset . filter ( <EOL> version__istartswith = query , <EOL> ) <EOL> return self . paginate ( <EOL> request = request , <EOL> queryset = queryset , <EOL> order_by = '<STR_LIT>' , <EOL> on_results = lambda x : serialize ( x , request . user ) , <EOL> ) <EOL> @ attach_scenarios ( [ create_new_release_scenario ] ) <EOL> def post ( self , request , project ) : <EOL> """<STR_LIT>""" <EOL> serializer = ReleaseSerializer ( data = request . DATA ) <EOL> if serializer . is_valid ( ) : <EOL> result = serializer . object <EOL> with transaction . atomic ( ) : <EOL> try : <EOL> release = Release . objects . create ( <EOL> project = project , <EOL> version = result [ '<STR_LIT:version>' ] , <EOL> ref = result . get ( '<STR_LIT>' ) , <EOL> url = result . get ( '<STR_LIT:url>' ) , <EOL> owner = result . get ( '<STR_LIT>' ) , <EOL> date_started = result . get ( '<STR_LIT>' ) , <EOL> date_released = result . get ( '<STR_LIT>' ) or timezone . now ( ) , <EOL> ) <EOL> except IntegrityError : <EOL> return Response ( { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , status = <NUM_LIT> ) <EOL> else : <EOL> Activity . objects . create ( <EOL> type = Activity . RELEASE , <EOL> project = project , <EOL> ident = result [ '<STR_LIT:version>' ] , <EOL> data = { '<STR_LIT:version>' : result [ '<STR_LIT:version>' ] } , <EOL> datetime = release . date_released , <EOL> ) <EOL> return Response ( serialize ( release , request . user ) , status = <NUM_LIT> ) <EOL> return Response ( serializer . errors , status = <NUM_LIT> ) </s>
