<s> from __future__ import absolute_import <EOL> import logging <EOL> from datetime import timedelta <EOL> from django . db import IntegrityError , transaction <EOL> from django . utils import timezone <EOL> from rest_framework import serializers , status <EOL> from rest_framework . response import Response <EOL> from sentry . api . base import DocSection <EOL> from sentry . api . bases . project import ProjectEndpoint , ProjectPermission <EOL> from sentry . api . decorators import sudo_required <EOL> from sentry . api . serializers import serialize <EOL> from sentry . models import ( <EOL> AuditLogEntryEvent , Group , GroupStatus , Project , ProjectBookmark , <EOL> ProjectStatus , UserOption <EOL> ) <EOL> from sentry . plugins import plugins <EOL> from sentry . tasks . deletion import delete_project <EOL> from sentry . utils . apidocs import scenario , attach_scenarios <EOL> @ scenario ( '<STR_LIT>' ) <EOL> def get_project_scenario ( runner ) : <EOL> runner . request ( <EOL> method = '<STR_LIT:GET>' , <EOL> path = '<STR_LIT>' % ( <EOL> runner . org . slug , runner . default_project . slug ) <EOL> ) <EOL> @ scenario ( '<STR_LIT>' ) <EOL> def delete_project_scenario ( runner ) : <EOL> with runner . isolated_project ( '<STR_LIT>' ) as project : <EOL> runner . request ( <EOL> method = '<STR_LIT>' , <EOL> path = '<STR_LIT>' % ( <EOL> runner . org . slug , project . slug ) <EOL> ) <EOL> @ scenario ( '<STR_LIT>' ) <EOL> def update_project_scenario ( runner ) : <EOL> with runner . isolated_project ( '<STR_LIT>' ) as project : <EOL> runner . request ( <EOL> method = '<STR_LIT>' , <EOL> path = '<STR_LIT>' % ( <EOL> runner . org . slug , project . slug ) , <EOL> data = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> } <EOL> ) <EOL> def clean_newline_inputs ( value ) : <EOL> result = [ ] <EOL> for v in value . split ( '<STR_LIT:\n>' ) : <EOL> v = v . lower ( ) . strip ( ) <EOL> if v : <EOL> result . append ( v ) <EOL> return result <EOL> class ProjectMemberSerializer ( serializers . Serializer ) : <EOL> isBookmarked = serializers . BooleanField ( ) <EOL> isSubscribed = serializers . BooleanField ( ) <EOL> class ProjectAdminSerializer ( serializers . Serializer ) : <EOL> isBookmarked = serializers . BooleanField ( ) <EOL> isSubscribed = serializers . BooleanField ( ) <EOL> name = serializers . CharField ( max_length = <NUM_LIT:200> ) <EOL> slug = serializers . SlugField ( max_length = <NUM_LIT:200> ) <EOL> class RelaxedProjectPermission ( ProjectPermission ) : <EOL> scope_map = { <EOL> '<STR_LIT:GET>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT:POST>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> } <EOL> class ProjectDetailsEndpoint ( ProjectEndpoint ) : <EOL> doc_section = DocSection . PROJECTS <EOL> permission_classes = [ RelaxedProjectPermission ] <EOL> def _get_unresolved_count ( self , project ) : <EOL> queryset = Group . objects . filter ( <EOL> status = GroupStatus . UNRESOLVED , <EOL> project = project , <EOL> ) <EOL> resolve_age = project . get_option ( '<STR_LIT>' , None ) <EOL> if resolve_age : <EOL> queryset = queryset . filter ( <EOL> last_seen__gte = timezone . now ( ) - timedelta ( hours = int ( resolve_age ) ) , <EOL> ) <EOL> return queryset . count ( ) <EOL> @ attach_scenarios ( [ get_project_scenario ] ) <EOL> def get ( self , request , project ) : <EOL> """<STR_LIT>""" <EOL> active_plugins = [ <EOL> { <EOL> '<STR_LIT:name>' : plugin . get_title ( ) , <EOL> '<STR_LIT:id>' : plugin . slug , <EOL> } <EOL> for plugin in plugins . configurable_for_project ( project , version = None ) <EOL> if plugin . is_enabled ( project ) <EOL> and plugin . has_project_conf ( ) <EOL> ] <EOL> data = serialize ( project , request . user ) <EOL> data [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : '<STR_LIT:\n>' . join ( project . get_option ( '<STR_LIT>' , [ '<STR_LIT:*>' ] ) or [ ] ) , <EOL> '<STR_LIT>' : int ( project . get_option ( '<STR_LIT>' , <NUM_LIT:0> ) ) , <EOL> '<STR_LIT>' : bool ( project . get_option ( '<STR_LIT>' , True ) ) , <EOL> '<STR_LIT>' : bool ( project . get_option ( '<STR_LIT>' , True ) ) , <EOL> '<STR_LIT>' : project . get_option ( '<STR_LIT>' , [ ] ) , <EOL> '<STR_LIT>' : bool ( project . get_option ( '<STR_LIT>' , True ) ) , <EOL> '<STR_LIT>' : '<STR_LIT:\n>' . join ( project . get_option ( '<STR_LIT>' , [ ] ) or [ ] ) , <EOL> } <EOL> data [ '<STR_LIT>' ] = active_plugins <EOL> data [ '<STR_LIT>' ] = serialize ( project . team , request . user ) <EOL> data [ '<STR_LIT>' ] = serialize ( project . organization , request . user ) <EOL> include = set ( filter ( bool , request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) ) ) <EOL> if '<STR_LIT>' in include : <EOL> data [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : self . _get_unresolved_count ( project ) , <EOL> } <EOL> return Response ( data ) <EOL> @ attach_scenarios ( [ update_project_scenario ] ) <EOL> def put ( self , request , project ) : <EOL> """<STR_LIT>""" <EOL> has_project_write = ( <EOL> ( request . auth and request . auth . has_scope ( '<STR_LIT>' ) ) or <EOL> ( request . access and request . access . has_scope ( '<STR_LIT>' ) ) <EOL> ) <EOL> if has_project_write : <EOL> serializer_cls = ProjectAdminSerializer <EOL> else : <EOL> serializer_cls = ProjectMemberSerializer <EOL> serializer = serializer_cls ( data = request . DATA , partial = True ) <EOL> if not serializer . is_valid ( ) : <EOL> return Response ( serializer . errors , status = <NUM_LIT> ) <EOL> result = serializer . object <EOL> changed = False <EOL> if result . get ( '<STR_LIT>' ) : <EOL> project . slug = result [ '<STR_LIT>' ] <EOL> changed = True <EOL> if result . get ( '<STR_LIT:name>' ) : <EOL> project . name = result [ '<STR_LIT:name>' ] <EOL> changed = True <EOL> if changed : <EOL> project . save ( ) <EOL> if result . get ( '<STR_LIT>' ) : <EOL> try : <EOL> with transaction . atomic ( ) : <EOL> ProjectBookmark . objects . create ( <EOL> project_id = project . id , <EOL> user = request . user , <EOL> ) <EOL> except IntegrityError : <EOL> pass <EOL> elif result . get ( '<STR_LIT>' ) is False : <EOL> ProjectBookmark . objects . filter ( <EOL> project_id = project . id , <EOL> user = request . user , <EOL> ) . delete ( ) <EOL> if result . get ( '<STR_LIT>' ) : <EOL> UserOption . objects . set_value ( request . user , project , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> elif result . get ( '<STR_LIT>' ) is False : <EOL> UserOption . objects . set_value ( request . user , project , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if has_project_write : <EOL> options = request . DATA . get ( '<STR_LIT>' , { } ) <EOL> if '<STR_LIT>' in options : <EOL> project . update_option ( <EOL> '<STR_LIT>' , <EOL> clean_newline_inputs ( options [ '<STR_LIT>' ] ) <EOL> ) <EOL> if '<STR_LIT>' in options : <EOL> project . update_option ( '<STR_LIT>' , int ( options [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in options : <EOL> project . update_option ( '<STR_LIT>' , bool ( options [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in options : <EOL> project . update_option ( '<STR_LIT>' , bool ( options [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in options : <EOL> project . update_option ( <EOL> '<STR_LIT>' , <EOL> [ s . strip ( ) . lower ( ) for s in options [ '<STR_LIT>' ] ] <EOL> ) <EOL> if '<STR_LIT>' in options : <EOL> project . update_option ( '<STR_LIT>' , bool ( options [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in options : <EOL> project . update_option ( <EOL> '<STR_LIT>' , <EOL> clean_newline_inputs ( options [ '<STR_LIT>' ] ) ) <EOL> self . create_audit_entry ( <EOL> request = request , <EOL> organization = project . organization , <EOL> target_object = project . id , <EOL> event = AuditLogEntryEvent . PROJECT_EDIT , <EOL> data = project . get_audit_log_data ( ) , <EOL> ) <EOL> data = serialize ( project , request . user ) <EOL> data [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : '<STR_LIT:\n>' . join ( project . get_option ( '<STR_LIT>' , [ '<STR_LIT:*>' ] ) or [ ] ) , <EOL> '<STR_LIT>' : int ( project . get_option ( '<STR_LIT>' , <NUM_LIT:0> ) ) , <EOL> } <EOL> return Response ( data ) <EOL> @ attach_scenarios ( [ delete_project_scenario ] ) <EOL> @ sudo_required <EOL> def delete ( self , request , project ) : <EOL> """<STR_LIT>""" <EOL> if project . is_internal_project ( ) : <EOL> return Response ( '<STR_LIT>' , <EOL> status = status . HTTP_403_FORBIDDEN ) <EOL> logging . getLogger ( '<STR_LIT>' ) . info ( <EOL> '<STR_LIT>' , <EOL> project . organization . slug , project . slug , project . id , request . user . id ) <EOL> updated = Project . objects . filter ( <EOL> id = project . id , <EOL> status = ProjectStatus . VISIBLE , <EOL> ) . update ( status = ProjectStatus . PENDING_DELETION ) <EOL> if updated : <EOL> delete_project . delay ( object_id = project . id , countdown = <NUM_LIT> ) <EOL> self . create_audit_entry ( <EOL> request = request , <EOL> organization = project . organization , <EOL> target_object = project . id , <EOL> event = AuditLogEntryEvent . PROJECT_REMOVE , <EOL> data = project . get_audit_log_data ( ) , <EOL> ) <EOL> return Response ( status = <NUM_LIT> ) </s>
