<s> import json <EOL> from collections import defaultdict <EOL> from django . http import HttpResponse , HttpResponseBadRequest <EOL> from django . views . decorators . cache import never_cache <EOL> from moztrap . model . library . models import CaseVersion <EOL> @ never_cache <EOL> def caseselection ( request ) : <EOL> """<STR_LIT>""" <EOL> if not request . GET . get ( "<STR_LIT>" ) : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" ) <EOL> product_id = request . GET [ "<STR_LIT>" ] <EOL> not_in_case = request . GET . get ( "<STR_LIT>" , None ) <EOL> in_case = request . GET . get ( "<STR_LIT>" , None ) <EOL> order_by = request . GET . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> limit = int ( request . GET . get ( "<STR_LIT>" , <NUM_LIT:0> ) ) <EOL> offset = int ( request . GET . get ( "<STR_LIT>" , <NUM_LIT:0> ) ) <EOL> caseversions = ( <EOL> CaseVersion . objects <EOL> . filter ( latest = True ) <EOL> . filter ( productversion__product_id = product_id ) <EOL> . select_related ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> . order_by ( order_by ) <EOL> ) <EOL> if not_in_case : <EOL> caseversions = caseversions . exclude ( case__suites = not_in_case ) <EOL> elif in_case : <EOL> caseversions = caseversions . filter ( case__suites = in_case ) <EOL> count = caseversions . count ( ) <EOL> if limit : <EOL> caseversions = caseversions [ offset : limit + offset ] <EOL> meta = { <EOL> "<STR_LIT:count>" : count , <EOL> "<STR_LIT>" : limit , <EOL> "<STR_LIT>" : offset <EOL> } <EOL> tags = ( <EOL> CaseVersion . tags . through . objects <EOL> . filter ( tag__deleted_on__isnull = True ) <EOL> . filter ( caseversion__latest = True ) <EOL> . filter ( caseversion__productversion__product_id = product_id ) <EOL> . select_related ( "<STR_LIT>" ) <EOL> ) <EOL> tags_map = defaultdict ( list ) <EOL> for t in tags . values ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> tags_map [ t [ "<STR_LIT>" ] ] . append ( { <EOL> "<STR_LIT:name>" : t [ "<STR_LIT>" ] , <EOL> "<STR_LIT:description>" : t [ "<STR_LIT>" ] , <EOL> } ) <EOL> objects = [ ] <EOL> for each in caseversions : <EOL> item = { <EOL> "<STR_LIT:id>" : each . id , <EOL> "<STR_LIT>" : each . case_id , <EOL> "<STR_LIT:name>" : each . name , <EOL> "<STR_LIT>" : unicode ( each . case . priority ) , <EOL> "<STR_LIT>" : { } , <EOL> "<STR_LIT>" : [ ] , <EOL> } <EOL> if each . created_by : <EOL> item [ "<STR_LIT>" ] = { <EOL> "<STR_LIT:id>" : each . created_by . id , <EOL> "<STR_LIT:username>" : unicode ( each . created_by . username ) <EOL> } <EOL> item [ "<STR_LIT>" ] = tags_map [ each . id ] <EOL> objects . append ( item ) <EOL> context = { "<STR_LIT>" : objects , "<STR_LIT>" : meta } <EOL> return HttpResponse ( <EOL> json . dumps ( context , indent = <NUM_LIT:2> ) , <EOL> content_type = "<STR_LIT:application/json>" <EOL> ) </s>
