<s> from __future__ import absolute_import , division , unicode_literals <EOL> from flask . ext . restful import reqparse <EOL> from sqlalchemy . sql import func <EOL> from changes . api . auth import requires_admin <EOL> from changes . api . base import APIView <EOL> from changes . config import db <EOL> from changes . models import Plan , PlanStatus , Project <EOL> SORT_CHOICES = ( '<STR_LIT:name>' , '<STR_LIT:date>' ) <EOL> STATUS_CHOICES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class ProjectPlanIndexAPIView ( APIView ) : <EOL> get_parser = reqparse . RequestParser ( ) <EOL> get_parser . add_argument ( '<STR_LIT>' , type = unicode , location = '<STR_LIT:args>' ) <EOL> get_parser . add_argument ( '<STR_LIT>' , type = unicode , location = '<STR_LIT:args>' , <EOL> choices = SORT_CHOICES , default = '<STR_LIT:name>' ) <EOL> get_parser . add_argument ( '<STR_LIT:status>' , type = unicode , location = '<STR_LIT:args>' , <EOL> choices = STATUS_CHOICES , default = '<STR_LIT>' ) <EOL> post_parser = reqparse . RequestParser ( ) <EOL> post_parser . add_argument ( '<STR_LIT:name>' , required = True ) <EOL> def get ( self , project_id ) : <EOL> project = Project . get ( project_id ) <EOL> if project is None : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> args = self . get_parser . parse_args ( ) <EOL> queryset = Plan . query . filter ( <EOL> Plan . project_id == project . id , <EOL> ) <EOL> if args . query : <EOL> queryset = queryset . filter ( <EOL> func . lower ( Plan . label ) . contains ( args . query . lower ( ) ) , <EOL> ) <EOL> if args . status : <EOL> queryset = queryset . filter ( <EOL> Plan . status == PlanStatus [ args . status ] , <EOL> ) <EOL> if args . sort == '<STR_LIT:name>' : <EOL> queryset = queryset . order_by ( Plan . label . asc ( ) ) <EOL> elif args . sort == '<STR_LIT:date>' : <EOL> queryset = queryset . order_by ( Plan . date_created . asc ( ) ) <EOL> return self . paginate ( queryset ) <EOL> @ requires_admin <EOL> def post ( self , project_id ) : <EOL> project = Project . get ( project_id ) <EOL> if project is None : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> args = self . post_parser . parse_args ( ) <EOL> plan = Plan ( label = args . name , project_id = project . id , ) <EOL> db . session . add ( plan ) <EOL> return self . respond ( plan ) </s>
