<s> import json <EOL> from django . db . models import Q <EOL> from django . http import HttpResponse <EOL> from django . views . generic . base import View <EOL> from django . views . generic . detail import DetailView <EOL> from django . views . generic . list import ListView <EOL> from django . utils . decorators import method_decorator <EOL> from django . utils . translation import ugettext as _ <EOL> from django . contrib import messages <EOL> from django . contrib . auth . decorators import login_required <EOL> from crate . web . lists . models import List <EOL> from crate . web . packages . models import Package <EOL> class AddToList ( View ) : <EOL> @ method_decorator ( login_required ) <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> return super ( AddToList , self ) . dispatch ( * args , ** kwargs ) <EOL> def render_json ( self , ** data ) : <EOL> return HttpResponse ( json . dumps ( data ) , mimetype = "<STR_LIT:application/json>" ) <EOL> def get_package ( self , package ) : <EOL> return next ( iter ( Package . objects . filter ( name = package ) [ : <NUM_LIT:1> ] ) , None ) <EOL> def get_list ( self , list , user ) : <EOL> return next ( iter ( List . objects . filter ( name = list , user = user ) [ : <NUM_LIT:1> ] ) , None ) <EOL> def get_message ( self ) : <EOL> return _ ( "<STR_LIT>" ) % self . kwargs <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> self . request = request <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> package = self . get_package ( self . kwargs . get ( "<STR_LIT>" ) ) <EOL> if package is None : <EOL> return self . render_json ( <EOL> package = self . kwargs . get ( "<STR_LIT>" ) , <EOL> list = self . kwargs . get ( "<STR_LIT:list>" ) , <EOL> success = False , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> user_list = self . get_list ( self . kwargs . get ( "<STR_LIT:list>" , None ) , user = request . user ) <EOL> if user_list is None : <EOL> return self . render_json ( <EOL> package = self . kwargs . get ( "<STR_LIT>" ) , <EOL> list = self . kwargs . get ( "<STR_LIT:list>" ) , <EOL> success = False , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> user_list . packages . add ( package ) <EOL> messages . success ( request , self . get_message ( ) ) <EOL> return self . render_json ( <EOL> package = self . kwargs . get ( "<STR_LIT>" ) , <EOL> list = self . kwargs . get ( "<STR_LIT:list>" ) , <EOL> success = True , <EOL> message = self . get_message ( ) <EOL> ) <EOL> class AddToNewList ( AddToList ) : <EOL> def get_message ( self ) : <EOL> kw = self . kwargs . copy ( ) <EOL> kw . update ( { <EOL> "<STR_LIT:list>" : self . request . POST . get ( "<STR_LIT:name>" ) , <EOL> } ) <EOL> return _ ( "<STR_LIT>" ) % kw <EOL> def get_list ( self , list , user ) : <EOL> if list is None : <EOL> list = self . request . POST . get ( "<STR_LIT:name>" ) <EOL> defaults = { <EOL> "<STR_LIT>" : self . request . POST . get ( "<STR_LIT>" , False ) , <EOL> "<STR_LIT:description>" : self . request . POST . get ( "<STR_LIT:description>" , "<STR_LIT>" ) , <EOL> } <EOL> user_list , c = List . objects . get_or_create ( user = user , name = list , defaults = defaults ) <EOL> if not c and user_list . private != self . request . POST . get ( "<STR_LIT>" , False ) : <EOL> user_list . private = self . request . POST . get ( "<STR_LIT>" , False ) <EOL> user_list . save ( ) <EOL> if not c and user_list . description != self . request . POST . get ( "<STR_LIT:description>" , "<STR_LIT>" ) : <EOL> user_list . description = self . request . POST . get ( "<STR_LIT:description>" , "<STR_LIT>" ) <EOL> user_list . save ( ) <EOL> return user_list <EOL> class RemoveFromList ( View ) : <EOL> @ method_decorator ( login_required ) <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> return super ( RemoveFromList , self ) . dispatch ( * args , ** kwargs ) <EOL> def render_json ( self , ** data ) : <EOL> return HttpResponse ( json . dumps ( data ) , mimetype = "<STR_LIT:application/json>" ) <EOL> def get_message ( self ) : <EOL> return _ ( "<STR_LIT>" ) % self . kwargs <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> self . request = request <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> try : <EOL> package = Package . objects . get ( name = kwargs . get ( "<STR_LIT>" ) ) <EOL> user_list = List . objects . get ( name = kwargs . get ( "<STR_LIT:list>" ) , user = request . user ) <EOL> except Package . DoesNotExist : <EOL> return self . render_json ( package = kwargs . get ( "<STR_LIT>" ) , list = kwargs . get ( "<STR_LIT:list>" ) , success = False , message = _ ( "<STR_LIT>" ) ) <EOL> except List . DoesNotExist : <EOL> return self . render_json ( package = kwargs . get ( "<STR_LIT>" ) , list = kwargs . get ( "<STR_LIT:list>" ) , success = False , message = _ ( "<STR_LIT>" ) ) <EOL> user_list . packages . remove ( package ) <EOL> messages . success ( request , self . get_message ( ) ) <EOL> return self . render_json ( package = kwargs . get ( "<STR_LIT>" ) , list = kwargs . get ( "<STR_LIT:list>" ) , success = True , message = self . get_message ( ) ) <EOL> class ListsList ( ListView ) : <EOL> queryset = List . objects . all ( ) . order_by ( "<STR_LIT:name>" ) <EOL> def get_queryset ( self ) : <EOL> qs = super ( ListsList , self ) . get_queryset ( ) <EOL> qs = qs . filter ( user__username = self . kwargs . get ( "<STR_LIT:username>" ) ) <EOL> if self . request . user . is_authenticated ( ) : <EOL> qs = qs . filter ( Q ( private = False ) | Q ( private = True , user = self . request . user ) ) <EOL> else : <EOL> qs = qs . filter ( private = False ) <EOL> return qs <EOL> def get_context_data ( self , ** kwargs ) : <EOL> ctx = super ( ListsList , self ) . get_context_data ( ** kwargs ) <EOL> ctx . update ( { <EOL> "<STR_LIT:username>" : self . kwargs . get ( "<STR_LIT:username>" ) , <EOL> } ) <EOL> return ctx <EOL> class ListDetail ( DetailView ) : <EOL> queryset = List . objects . all ( ) . select_related ( "<STR_LIT>" ) <EOL> def get_queryset ( self ) : <EOL> qs = super ( ListDetail , self ) . get_queryset ( ) <EOL> qs = qs . filter ( user__username = self . kwargs . get ( "<STR_LIT:username>" ) ) <EOL> if self . request . user . is_authenticated ( ) : <EOL> qs = qs . filter ( Q ( private = False ) | Q ( private = True , user = self . request . user ) ) <EOL> else : <EOL> qs = qs . filter ( private = False ) <EOL> return qs <EOL> def get_context_data ( self , ** kwargs ) : <EOL> ctx = super ( ListDetail , self ) . get_context_data ( ** kwargs ) <EOL> ctx . update ( { <EOL> "<STR_LIT>" : self . object . packages . all ( ) . extra ( select = { "<STR_LIT>" : "<STR_LIT>" } ) . order_by ( "<STR_LIT>" ) , <EOL> } ) <EOL> return ctx </s>
