<s> import json <EOL> from django . contrib . auth import logout , login , authenticate <EOL> from django . contrib . auth . models import User <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponse <EOL> from django . utils . translation import get_language <EOL> from django . views . generic import ( <EOL> FormView , CreateView , RedirectView , DetailView , UpdateView ) <EOL> from django . db . models import Q , Count <EOL> from networkx import DiGraph <EOL> from networkx . readwrite import json_graph <EOL> from i18n . utils import normalize_language_code <EOL> from nouns . models import Noun , Channel <EOL> from profiles . mixins import LoginRequiredMixin <EOL> from profiles . forms import ( RegistrationForm , AuthenticationForm , <EOL> ProfileUpdateForm ) <EOL> from profiles . models import Profile <EOL> from premises . models import Contention , Premise , SUPPORT , OBJECTION , SITUATION , Report <EOL> from premises . mixins import PaginationMixin <EOL> from newsfeed . models import Entry <EOL> class RegistrationView ( CreateView ) : <EOL> form_class = RegistrationForm <EOL> template_name = "<STR_LIT>" <EOL> def form_valid ( self , form ) : <EOL> response = super ( RegistrationView , self ) . form_valid ( form ) <EOL> user = authenticate ( username = form . cleaned_data [ "<STR_LIT:username>" ] , <EOL> password = form . cleaned_data [ "<STR_LIT>" ] ) <EOL> login ( self . request , user ) <EOL> return response <EOL> def get_success_url ( self ) : <EOL> return reverse ( "<STR_LIT>" ) <EOL> class LoginView ( FormView ) : <EOL> form_class = AuthenticationForm <EOL> template_name = "<STR_LIT>" <EOL> def form_valid ( self , form ) : <EOL> login ( self . request , form . get_user ( ) ) <EOL> return super ( LoginView , self ) . form_valid ( form ) <EOL> def get_success_url ( self ) : <EOL> return self . request . GET . get ( "<STR_LIT>" ) or reverse ( "<STR_LIT>" ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( LoginView , self ) . get_context_data ( ** kwargs ) <EOL> context [ "<STR_LIT>" ] = self . request . GET . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return context <EOL> class LogoutView ( LoginRequiredMixin , RedirectView ) : <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> logout ( request ) <EOL> return super ( LogoutView , self ) . get ( request , * args , ** kwargs ) <EOL> def get_redirect_url ( self , ** kwargs ) : <EOL> return reverse ( "<STR_LIT>" ) <EOL> class BaseProfileDetailView ( DetailView ) : <EOL> slug_field = '<STR_LIT:username>' <EOL> slug_url_kwarg = '<STR_LIT:username>' <EOL> context_object_name = "<STR_LIT>" <EOL> model = Profile <EOL> paginate_by = <NUM_LIT:20> <EOL> tab_name = "<STR_LIT>" <EOL> def get_context_data ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> user = self . get_object ( ) <EOL> can_follow = self . request . user != user <EOL> if self . request . user . is_authenticated ( ) : <EOL> is_followed = self . request . user . following . filter ( <EOL> pk = user . id ) . exists ( ) <EOL> else : <EOL> is_followed = False <EOL> return super ( BaseProfileDetailView , self ) . get_context_data ( <EOL> can_follow = can_follow , <EOL> is_followed = is_followed , <EOL> tab_name = self . tab_name , <EOL> ** kwargs <EOL> ) <EOL> class ProfileDetailView ( BaseProfileDetailView ) : <EOL> def get_context_data ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> user = self . get_object ( ) <EOL> return super ( ProfileDetailView , self ) . get_context_data ( <EOL> related_channels = self . get_related_channels ( user ) , <EOL> discussed_users = self . get_discussed_users ( user ) , <EOL> supported_premises = self . get_supported_premises ( user ) , <EOL> ** kwargs <EOL> ) <EOL> def get_supported_premises ( self , user ) : <EOL> return Premise . objects . filter ( <EOL> is_approved = True , <EOL> user = user , <EOL> argument__language = normalize_language_code ( get_language ( ) ) <EOL> ) . annotate ( <EOL> supporter_count = Count ( '<STR_LIT>' , distinct = True ) <EOL> ) . filter ( <EOL> supporter_count__gt = <NUM_LIT:0> <EOL> ) . order_by ( <EOL> '<STR_LIT>' <EOL> ) [ : <NUM_LIT:10> ] <EOL> def get_discussed_users ( self , user ) : <EOL> lines = Premise . objects . filter ( <EOL> user = user , <EOL> parent__user__isnull = False , <EOL> ) . exclude ( <EOL> parent__user = user <EOL> ) . values ( <EOL> '<STR_LIT>' <EOL> ) . annotate ( <EOL> count = Count ( '<STR_LIT>' ) <EOL> ) . order_by ( <EOL> '<STR_LIT>' <EOL> ) [ : <NUM_LIT:5> ] <EOL> profiles = [ Profile . objects . get ( id = line [ '<STR_LIT>' ] ) <EOL> for line in lines ] <EOL> def make_bundle ( target ) : <EOL> because = self . premise_count_by_user ( user , target , SUPPORT ) <EOL> but = self . premise_count_by_user ( user , target , OBJECTION ) <EOL> however = self . premise_count_by_user ( user , target , SITUATION ) <EOL> total = because + but + however <EOL> return { <EOL> '<STR_LIT:user>' : profile , <EOL> '<STR_LIT>' : <NUM_LIT:100> * float ( because ) / total , <EOL> '<STR_LIT>' : <NUM_LIT:100> * float ( but ) / total , <EOL> '<STR_LIT>' : <NUM_LIT:100> * float ( however ) / total <EOL> } <EOL> return [ <EOL> make_bundle ( profile ) <EOL> for profile in profiles <EOL> ] <EOL> def premise_count_by_user ( self , user , target , premise_type ) : <EOL> return Premise . objects . filter ( <EOL> user = user , <EOL> parent__user = target , <EOL> premise_type = premise_type <EOL> ) . count ( ) <EOL> def get_related_channels ( self , user ) : <EOL> contention_nouns = Contention . objects . filter ( <EOL> premises__user = user , <EOL> nouns__isnull = False , <EOL> language = normalize_language_code ( get_language ( ) ) <EOL> ) . order_by ( <EOL> '<STR_LIT>' <EOL> ) . values_list ( <EOL> '<STR_LIT>' , <EOL> flat = True <EOL> ) <EOL> supported_nouns = Contention . objects . filter ( <EOL> premises__supporters = user , <EOL> nouns__isnull = False , <EOL> language = normalize_language_code ( get_language ( ) ) <EOL> ) . order_by ( <EOL> '<STR_LIT>' <EOL> ) . values_list ( <EOL> '<STR_LIT>' , <EOL> flat = True <EOL> ) <EOL> noun_ids = list ( contention_nouns ) + list ( supported_nouns ) <EOL> noun_set = set ( noun_ids ) <EOL> channels = Channel . objects . filter ( <EOL> nouns__in = noun_ids , <EOL> language = normalize_language_code ( get_language ( ) ) <EOL> ) . distinct ( ) <EOL> bundle = [ ] <EOL> total_score = <NUM_LIT:0> <EOL> for channel in channels : <EOL> channel_dict = { <EOL> '<STR_LIT>' : channel . serialize ( ) , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } <EOL> for noun in channel . nouns . all ( ) : <EOL> if noun . pk in noun_set : <EOL> channel_dict [ '<STR_LIT>' ] += noun_ids . count ( noun . pk ) <EOL> total_score += channel_dict [ '<STR_LIT>' ] <EOL> bundle . append ( channel_dict ) <EOL> return sorted ( bundle , <EOL> key = lambda c : c [ '<STR_LIT>' ] , <EOL> reverse = True ) <EOL> class ProfileArgumentsView ( BaseProfileDetailView ) : <EOL> tab_name = "<STR_LIT>" <EOL> template_name = "<STR_LIT>" <EOL> def get_context_data ( self , ** kwargs ) : <EOL> user = self . get_object ( ) <EOL> contentions = Contention . objects . filter ( <EOL> user = user <EOL> ) . order_by ( "<STR_LIT>" ) <EOL> if user != self . request . user : <EOL> contentions = contentions . filter ( is_published = True ) <EOL> return super ( ProfileArgumentsView , self ) . get_context_data ( <EOL> contentions = contentions <EOL> ) <EOL> class ProfileFallaciesView ( BaseProfileDetailView ) : <EOL> tab_name = "<STR_LIT>" <EOL> template_name = "<STR_LIT>" <EOL> def get_context_data ( self , ** kwargs ) : <EOL> user = self . get_object ( ) <EOL> fallacies = Report . objects . filter ( <EOL> reason__isnull = False , <EOL> reporter = user <EOL> ) . order_by ( '<STR_LIT>' ) <EOL> return super ( ProfileFallaciesView , self ) . get_context_data ( <EOL> fallacies = fallacies <EOL> ) <EOL> class ProfilePremisesView ( BaseProfileDetailView , PaginationMixin ) : <EOL> tab_name = "<STR_LIT>" <EOL> template_name = "<STR_LIT>" <EOL> paginate_by = <NUM_LIT:20> <EOL> def get_objects ( self , paginate = True ) : <EOL> user = self . get_object ( ) <EOL> premises = Premise . objects . filter ( user = user ) . order_by ( "<STR_LIT>" ) <EOL> if user != self . request . user : <EOL> premises = premises . filter ( is_approved = True ) <EOL> if paginate : <EOL> premises = premises [ self . get_offset ( ) : self . get_limit ( ) ] <EOL> return premises <EOL> def has_next_page ( self ) : <EOL> total = self . get_objects ( paginate = False ) . count ( ) <EOL> return total > ( self . get_offset ( ) + self . paginate_by ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> premises = self . get_objects ( ) <EOL> return super ( ProfilePremisesView , self ) . get_context_data ( <EOL> premises = premises , <EOL> has_next_page = self . has_next_page ( ) , <EOL> next_page_url = self . get_next_page_url ( ) , <EOL> ) <EOL> class ProfileUpdateView ( LoginRequiredMixin , UpdateView ) : <EOL> form_class = ProfileUpdateForm <EOL> def get_object ( self , queryset = None ) : <EOL> return self . request . user <EOL> def get_success_url ( self ) : <EOL> return self . request . user . get_absolute_url ( ) <EOL> class ProfileChannelsGraphView ( ProfileDetailView ) : <EOL> def render_to_response ( self , context , ** response_kwargs ) : <EOL> user = self . get_object ( ) <EOL> return HttpResponse ( json . dumps ( self . get_bundle ( user ) ) , <EOL> content_type = "<STR_LIT:application/json>" ) <EOL> def get_bundle ( self , user ) : <EOL> graph = self . build_graph ( user ) <EOL> return json_graph . node_link_data ( graph ) <EOL> def build_graph ( self , user ) : <EOL> contention_nouns = Contention . objects . filter ( <EOL> premises__user = user , <EOL> nouns__isnull = False , <EOL> language = normalize_language_code ( get_language ( ) ) <EOL> ) . order_by ( <EOL> '<STR_LIT>' <EOL> ) . values_list ( <EOL> '<STR_LIT>' , <EOL> flat = True <EOL> ) <EOL> supported_nouns = Contention . objects . filter ( <EOL> premises__supporters = user , <EOL> nouns__isnull = False , <EOL> language = normalize_language_code ( get_language ( ) ) <EOL> ) . order_by ( <EOL> '<STR_LIT>' <EOL> ) . values_list ( <EOL> '<STR_LIT>' , <EOL> flat = True <EOL> ) <EOL> noun_ids = set ( contention_nouns ) ^ set ( supported_nouns ) <EOL> channels = Channel . objects . filter ( <EOL> nouns__in = noun_ids , <EOL> language = normalize_language_code ( get_language ( ) ) <EOL> ) . distinct ( ) <EOL> graph = DiGraph ( ) <EOL> last_channel = None <EOL> first_channel = None <EOL> label = lambda x : x . title ( ) <EOL> for channel in channels : <EOL> graph . add_node ( channel . title , { <EOL> "<STR_LIT:label>" : label ( channel . title ) , <EOL> "<STR_LIT:type>" : "<STR_LIT>" <EOL> } ) <EOL> if last_channel : <EOL> graph . add_edge ( last_channel . title , channel . title ) <EOL> channel_nouns = channel . nouns . all ( ) <EOL> for channel_noun in channel_nouns : <EOL> if channel_noun . id in noun_ids : <EOL> graph . add_edge ( channel_noun . text , channel . title ) <EOL> graph . add_node ( channel_noun . text , { <EOL> "<STR_LIT:label>" : label ( channel_noun . text ) , <EOL> "<STR_LIT:type>" : "<STR_LIT>" <EOL> } ) <EOL> last_channel = channel <EOL> if not first_channel : <EOL> first_channel = channel <EOL> graph . add_edge ( first_channel . title , last_channel . title ) <EOL> return graph </s>
