<s> import datetime <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . messages . views import SuccessMessageMixin <EOL> from django . db . models import Count , Case , When <EOL> from django . http import ( <EOL> HttpResponsePermanentRedirect , <EOL> HttpResponseRedirect , <EOL> Http404 , <EOL> ) <EOL> from django . shortcuts import get_object_or_404 , render <EOL> from django . views . generic . edit import UpdateView <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from fruit . models import Kind , Fruit <EOL> from user . forms import UserSettingsForm <EOL> from user . models import FruitUser <EOL> from utils . mixins import LoginRequiredMixin <EOL> def fruit_counter ( ** filters ) : <EOL> return { <EOL> '<STR_LIT>' : Count ( Case ( When ( fruits__deleted = False , then = <NUM_LIT:1> , ** filters ) ) ) <EOL> } <EOL> def _get_pickers ( ** filters ) : <EOL> return FruitUser . objects . active ( ) . exclude ( username = '<STR_LIT>' ) . filter ( ** filters ) . annotate ( ** fruit_counter ( ** filters ) ) . order_by ( '<STR_LIT>' , '<STR_LIT:username>' ) <EOL> def index ( request ) : <EOL> top_all_time = _get_pickers ( ) <EOL> last_month = datetime . date . today ( ) - datetime . timedelta ( <NUM_LIT> / <NUM_LIT:12> ) <EOL> top_last_month = _get_pickers ( fruits__created__gte = last_month ) . exclude ( fruit_count = <NUM_LIT:0> ) <EOL> context = dict ( <EOL> top_all_time = top_all_time [ : <NUM_LIT:10> ] , <EOL> top_last_month = top_last_month [ : <NUM_LIT:4> ] , <EOL> pickers_count = FruitUser . objects . active ( ) . order_by ( ) . count ( ) , <EOL> fruit_count = Fruit . objects . valid ( ) . order_by ( ) . count ( ) , <EOL> ) <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> def profile ( request , pk , slug = None ) : <EOL> qs = FruitUser . objects . annotate ( ** fruit_counter ( ) ) <EOL> user = get_object_or_404 ( qs , pk = pk ) <EOL> if user . slug != slug : <EOL> return HttpResponsePermanentRedirect ( user . get_absolute_url ( ) ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : Kind . objects . all ( ) , <EOL> } ) <EOL> accounts_profile = login_required ( lambda r : HttpResponseRedirect ( r . user . get_absolute_url ( ) ) ) <EOL> @ login_required <EOL> def messages ( request , pk ) : <EOL> qs = FruitUser . objects . prefetch_related ( '<STR_LIT>' ) <EOL> user = get_object_or_404 ( qs , pk = pk ) <EOL> if user != request . user : <EOL> raise Http404 <EOL> response = render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:user>' : user , <EOL> } ) <EOL> user . clear_messages ( ) <EOL> return response <EOL> class UserSettingsView ( SuccessMessageMixin , LoginRequiredMixin , UpdateView ) : <EOL> template_name = '<STR_LIT>' <EOL> model = FruitUser <EOL> form_class = UserSettingsForm <EOL> success_message = _ ( '<STR_LIT>' ) <EOL> def get_object ( self , queryset = None ) : <EOL> return self . request . user <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT:user>' ] = self . request . user <EOL> return context </s>
