<s> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . shortcuts import get_object_or_404 , render_to_response <EOL> from django . core . urlresolvers import reverse_lazy , reverse <EOL> from django . contrib import messages <EOL> from django . utils . datastructures import SortedDict <EOL> from django . http import HttpResponse , HttpResponseRedirect , Http404 <EOL> from django . template import RequestContext <EOL> from django . views . generic import TemplateView , ListView , DetailView , CreateView , UpdateView , RedirectView <EOL> from guardian . mixins import LoginRequiredMixin , PermissionRequiredMixin <EOL> from braces . views import SetHeadlineMixin <EOL> from djangoratings . views import AddRatingView <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from . models import Resource , Topic , ResourceType , FeaturedResource <EOL> from profiles . models import SavedResource , TopicFollow <EOL> from . forms import ResourceCreateForm , ResourceUpdateForm , TopicCreateForm , TopicUpdateForm <EOL> def resource_home ( request ) : <EOL> topics = Topic . objects . filter ( resource__title__isnull = False ) . distinct ( ) . order_by ( '<STR_LIT:name>' ) <EOL> if request . session . get ( '<STR_LIT>' , False ) : <EOL> messages . warning ( request , '<STR_LIT>' ) <EOL> request . session [ '<STR_LIT>' ] = False <EOL> ctx = { <EOL> '<STR_LIT>' : topics , <EOL> } <EOL> return render_to_response ( '<STR_LIT>' , ctx , context_instance = RequestContext ( request ) ) <EOL> def rate_resource ( request , object_id , score ) : <EOL> model = '<STR_LIT>' <EOL> app_label = '<STR_LIT>' <EOL> field_name = '<STR_LIT>' <EOL> try : <EOL> content_type = ContentType . objects . get ( model = model , app_label = app_label ) <EOL> except ContentType . DoesNotExist : <EOL> raise Http404 ( '<STR_LIT>' ) <EOL> params = { <EOL> '<STR_LIT>' : content_type . id , <EOL> '<STR_LIT>' : object_id , <EOL> '<STR_LIT>' : field_name , <EOL> '<STR_LIT>' : score , <EOL> } <EOL> response = AddRatingView ( ) ( request , ** params ) <EOL> if response . status_code == <NUM_LIT:200> : <EOL> if response . content == '<STR_LIT>' : <EOL> messages . success ( request , '<STR_LIT>' ) <EOL> else : <EOL> messages . error ( request , '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( request . META [ '<STR_LIT>' ] ) <EOL> class ResourceSaveView ( LoginRequiredMixin , RedirectView ) : <EOL> permanent = False <EOL> def get_redirect_url ( self , pk ) : <EOL> resource = get_object_or_404 ( Resource , pk = pk ) <EOL> SavedResource . objects . get_or_create ( user = self . request . user , resource = resource ) <EOL> if self . request . META [ '<STR_LIT>' ] : <EOL> return self . request . META [ '<STR_LIT>' ] <EOL> else : <EOL> return reverse_lazy ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : pk } ) <EOL> class ResourceFeatureView ( LoginRequiredMixin , RedirectView ) : <EOL> permanent = False <EOL> permission_required = '<STR_LIT>' <EOL> return_403 = True <EOL> def get_redirect_url ( self , pk , slug ) : <EOL> resource = get_object_or_404 ( Resource , pk = pk ) <EOL> topic = get_object_or_404 ( Topic , slug = slug ) <EOL> resource . make_featured ( topic = topic ) <EOL> if self . request . META [ '<STR_LIT>' ] : <EOL> return self . request . META [ '<STR_LIT>' ] <EOL> else : <EOL> return reverse_lazy ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : pk } ) <EOL> class SidebarMixin ( object ) : <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( SidebarMixin , self ) . get_context_data ( ** kwargs ) <EOL> topics = Topic . objects . filter ( resource__title__isnull = False ) . distinct ( ) . order_by ( '<STR_LIT:name>' ) <EOL> context [ '<STR_LIT>' ] = topics <EOL> return context <EOL> class ResourceAllListView ( SetHeadlineMixin , SidebarMixin , ListView ) : <EOL> context_object_name = '<STR_LIT>' <EOL> template_name = '<STR_LIT>' <EOL> paginate_by = <NUM_LIT:12> <EOL> def get_queryset ( self ) : <EOL> level_to_get = None <EOL> if '<STR_LIT>' in self . request . GET : <EOL> level_to_get = self . request . GET [ '<STR_LIT>' ] <EOL> resources = Resource . objects . all ( ) <EOL> if level_to_get : <EOL> resources = resources . filter ( level = level_to_get ) <EOL> self . headline = '<STR_LIT>' <EOL> return resources <EOL> class TopicFollowView ( LoginRequiredMixin , RedirectView ) : <EOL> permanent = False <EOL> def get_redirect_url ( self , slug ) : <EOL> topic = get_object_or_404 ( Topic , slug = slug ) <EOL> try : <EOL> tf = TopicFollow . objects . get ( user = self . request . user , topic = topic ) <EOL> tf . delete ( ) <EOL> messages . success ( self . request , '<STR_LIT>' ) <EOL> except TopicFollow . DoesNotExist : <EOL> TopicFollow . objects . create ( user = self . request . user , topic = topic ) <EOL> messages . success ( self . request , '<STR_LIT>' ) <EOL> return reverse_lazy ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : slug } ) <EOL> def topic_home ( request , slug ) : <EOL> current_topic = get_object_or_404 ( Topic , slug = slug ) <EOL> headline = """<STR_LIT>""" + unicode ( current_topic . name ) . capitalize ( ) + """<STR_LIT>""" <EOL> topics = Topic . objects . filter ( resource__title__isnull = False ) . distinct ( ) . order_by ( '<STR_LIT:name>' ) <EOL> ctx = { <EOL> '<STR_LIT>' : current_topic , <EOL> '<STR_LIT>' : headline , <EOL> '<STR_LIT>' : topics <EOL> } <EOL> resourcetypes = [ ] <EOL> res_types = ResourceType . objects . all ( ) . order_by ( '<STR_LIT:name>' ) <EOL> for res_type in res_types : <EOL> try : <EOL> result = FeaturedResource . objects . get ( topic = current_topic , resource_type = res_type ) <EOL> resourcetypes . append ( ( result . resource_type . slug , result . resource ) ) <EOL> except FeaturedResource . DoesNotExist : <EOL> result = current_topic . resource_set . filter ( resource_type = res_type ) . order_by ( '<STR_LIT>' ) <EOL> if len ( result ) > <NUM_LIT:0> : <EOL> resourcetypes . append ( ( result [ <NUM_LIT:0> ] . resource_type . slug , result [ <NUM_LIT:0> ] ) ) <EOL> ctx [ '<STR_LIT>' ] = SortedDict ( resourcetypes ) <EOL> return render_to_response ( '<STR_LIT>' , ctx , context_instance = RequestContext ( request ) ) <EOL> class ResourceTopicListView ( SetHeadlineMixin , SidebarMixin , ListView ) : <EOL> context_object_name = '<STR_LIT>' <EOL> template_name = '<STR_LIT>' <EOL> paginate_by = <NUM_LIT:12> <EOL> def get_queryset ( self ) : <EOL> level_to_get = None <EOL> res_type = None <EOL> slug = self . kwargs [ '<STR_LIT>' ] <EOL> try : <EOL> res_type = self . kwargs [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> if '<STR_LIT>' in self . request . GET : <EOL> level_to_get = self . request . GET [ '<STR_LIT>' ] <EOL> topic = get_object_or_404 ( Topic , slug = slug ) <EOL> resources = topic . resource_set . all ( ) <EOL> self . headline = '<STR_LIT>' + unicode ( topic . name ) . capitalize ( ) + '<STR_LIT>' <EOL> if res_type : <EOL> res_type = get_object_or_404 ( ResourceType , slug = res_type ) <EOL> resources = resources . filter ( resource_type = res_type ) <EOL> self . headline = unicode ( topic . name ) . capitalize ( ) + '<STR_LIT>' + '<STR_LIT>' + unicode ( res_type . name ) + '<STR_LIT>' <EOL> if level_to_get and level_to_get != '<STR_LIT:all>' : <EOL> resources = resources . filter ( level = level_to_get ) <EOL> return resources <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ResourceTopicListView , self ) . get_context_data ( ** kwargs ) <EOL> topic = get_object_or_404 ( Topic , slug = self . kwargs [ '<STR_LIT>' ] ) <EOL> context [ '<STR_LIT>' ] = topic <EOL> return context <EOL> class ResourceDetailView ( SetHeadlineMixin , SidebarMixin , DetailView ) : <EOL> model = Resource <EOL> context_object_name = '<STR_LIT>' <EOL> template_name = '<STR_LIT>' <EOL> def get_object ( self ) : <EOL> resource = super ( ResourceDetailView , self ) . get_object ( ) <EOL> self . headline = unicode ( resource . title ) + """<STR_LIT>""" + unicode ( resource . resource_type ) + """<STR_LIT>""" <EOL> return resource <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ResourceDetailView , self ) . get_context_data ( ** kwargs ) <EOL> return context <EOL> class ResourceCreateView ( LoginRequiredMixin , SetHeadlineMixin , SidebarMixin , CreateView ) : <EOL> form_class = ResourceCreateForm <EOL> model = Resource <EOL> headline = '<STR_LIT>' <EOL> def form_valid ( self , form ) : <EOL> form . instance . created_by = self . request . user <EOL> return super ( ResourceCreateView , self ) . form_valid ( form ) <EOL> class ResourceUpdateView ( LoginRequiredMixin , PermissionRequiredMixin , SetHeadlineMixin , SidebarMixin , UpdateView ) : <EOL> form_class = ResourceUpdateForm <EOL> model = Resource <EOL> headline = '<STR_LIT>' <EOL> permission_required = '<STR_LIT>' <EOL> return_403 = True <EOL> class TopicCreateView ( SetHeadlineMixin , SidebarMixin , CreateView ) : <EOL> form_class = TopicCreateForm <EOL> template_name = '<STR_LIT>' <EOL> permission_required = '<STR_LIT>' <EOL> headline = '<STR_LIT>' <EOL> class TopicUpdateView ( PermissionRequiredMixin , SetHeadlineMixin , SidebarMixin , UpdateView ) : <EOL> form_class = TopicUpdateForm <EOL> model = Topic <EOL> template_name = '<STR_LIT>' <EOL> permission_required = '<STR_LIT>' <EOL> render_403 = True <EOL> return_403 = True <EOL> headline = '<STR_LIT>' </s>
