<s> import logging <EOL> import uuid <EOL> from flask . ext . restful import reqparse <EOL> from sqlalchemy . orm import joinedload <EOL> from changes . config import statsreporter <EOL> from changes . api . base import APIView , error <EOL> from changes . api . auth import requires_admin <EOL> from changes . db . utils import create_or_update <EOL> from changes . models . project import Project , ProjectOption <EOL> from changes . models . snapshot import Snapshot , SnapshotStatus <EOL> def validate_snapshot_id ( id_hex ) : <EOL> if not id_hex : <EOL> return '<STR_LIT>' <EOL> snapshot = Snapshot . query . get ( id_hex ) <EOL> assert snapshot is not None , "<STR_LIT>" <EOL> assert snapshot . status == SnapshotStatus . active , "<STR_LIT>" <EOL> return id_hex <EOL> class ProjectOptionsIndexAPIView ( APIView ) : <EOL> parser = reqparse . RequestParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , type = validate_snapshot_id ) <EOL> def _get_project ( self , project_id ) : <EOL> project = Project . query . options ( <EOL> joinedload ( Project . repository , innerjoin = True ) , <EOL> ) . filter_by ( slug = project_id ) . first ( ) <EOL> if project is None : <EOL> try : <EOL> as_uuid = uuid . UUID ( project_id ) <EOL> except ValueError : <EOL> return None <EOL> project = Project . query . options ( <EOL> joinedload ( Project . repository , innerjoin = True ) , <EOL> ) . get ( as_uuid ) <EOL> return project <EOL> @ requires_admin <EOL> def post ( self , project_id ) : <EOL> project = self . _get_project ( project_id ) <EOL> if project is None : <EOL> return error ( "<STR_LIT>" , http_code = <NUM_LIT> ) <EOL> args = self . parser . parse_args ( ) <EOL> for name , value in args . iteritems ( ) : <EOL> if value is None : <EOL> continue <EOL> if name == '<STR_LIT>' : <EOL> current = Snapshot . get_current ( project . id ) <EOL> if current : <EOL> replacement = value and Snapshot . query . get ( value ) <EOL> if not replacement or replacement . date_created < current . date_created : <EOL> _report_snapshot_downgrade ( project ) <EOL> create_or_update ( ProjectOption , where = { <EOL> '<STR_LIT>' : project , <EOL> '<STR_LIT:name>' : name , <EOL> } , values = { <EOL> '<STR_LIT:value>' : value , <EOL> } ) <EOL> return self . respond ( { } ) <EOL> def _report_snapshot_downgrade ( project ) : <EOL> """<STR_LIT>""" <EOL> statsreporter . stats ( ) . incr ( "<STR_LIT>" ) <EOL> logging . warning ( '<STR_LIT>' , project . slug ) </s>
