<s> """<STR_LIT>""" <EOL> from builtins import str <EOL> from builtins import object <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> from django . db import models <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . translation import pgettext_lazy as pgettext <EOL> from lino . api import dd , rt <EOL> from lino import mixins <EOL> from lino . utils import join_elems <EOL> from lino . utils . xmlgen import html as xghtml <EOL> from lino . utils . xmlgen . html import E <EOL> from lino . mixins import Referrable <EOL> from lino . modlib . users . mixins import ByUser , UserAuthored <EOL> from . utils import ResponseStates , PollStates <EOL> from . roles import PollsUser , PollsStaff <EOL> NullBooleanField = models . NullBooleanField <EOL> class ChoiceSet ( mixins . BabelNamed ) : <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> class ChoiceSets ( dd . Table ) : <EOL> required_roles = dd . required ( PollsStaff ) <EOL> model = '<STR_LIT>' <EOL> detail_layout = """<STR_LIT>""" <EOL> class Choice ( mixins . BabelNamed , mixins . Sequenced ) : <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> choiceset = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> def get_siblings ( self ) : <EOL> return self . choiceset . choices . order_by ( '<STR_LIT>' ) <EOL> @ dd . action ( ) <EOL> def select_by_response ( self , ar ) : <EOL> mi = ar . master_instance <EOL> dd . logger . info ( "<STR_LIT>" , mi ) <EOL> if isinstance ( mi , Response ) : <EOL> AnswerChoice ( response = mi , choice = self ) . save ( ) <EOL> class Choices ( dd . Table ) : <EOL> model = '<STR_LIT>' <EOL> required_roles = dd . required ( PollsStaff ) <EOL> class ChoicesBySet ( Choices ) : <EOL> master_key = '<STR_LIT>' <EOL> required_roles = dd . required ( ) <EOL> @ dd . python_2_unicode_compatible <EOL> class Poll ( UserAuthored , mixins . CreatedModified , Referrable ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> abstract = dd . is_abstract_model ( __name__ , '<STR_LIT>' ) <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> ordering = [ '<STR_LIT>' ] <EOL> title = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:200> ) <EOL> details = models . TextField ( _ ( "<STR_LIT>" ) , blank = True ) <EOL> default_choiceset = models . ForeignKey ( <EOL> '<STR_LIT>' , <EOL> null = True , blank = True , <EOL> related_name = '<STR_LIT>' , <EOL> verbose_name = _ ( "<STR_LIT>" ) ) <EOL> default_multiple_choices = models . BooleanField ( <EOL> _ ( "<STR_LIT>" ) , default = False ) <EOL> questions_to_add = models . TextField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> blank = True ) <EOL> state = PollStates . field ( default = PollStates . draft . as_callable ) <EOL> workflow_state_field = '<STR_LIT:state>' <EOL> def __str__ ( self ) : <EOL> return self . ref or self . title <EOL> def after_ui_save ( self , ar , cw ) : <EOL> if self . questions_to_add : <EOL> q = None <EOL> qkw = dict ( ) <EOL> number = <NUM_LIT:1> <EOL> for ln in self . questions_to_add . splitlines ( ) : <EOL> ln = ln . strip ( ) <EOL> if ln : <EOL> if ln . startswith ( '<STR_LIT:#>' ) : <EOL> q . details = ln [ <NUM_LIT:1> : ] <EOL> q . save ( ) <EOL> continue <EOL> elif ln . startswith ( '<STR_LIT:=>' ) : <EOL> q = Question ( poll = self , title = ln [ <NUM_LIT:1> : ] , <EOL> is_heading = True , ** qkw ) <EOL> number = <NUM_LIT:1> <EOL> else : <EOL> q = Question ( poll = self , title = ln , <EOL> number = str ( number ) , ** qkw ) <EOL> number += <NUM_LIT:1> <EOL> q . full_clean ( ) <EOL> q . save ( ) <EOL> qkw . update ( seqno = q . seqno + <NUM_LIT:1> ) <EOL> self . questions_to_add = '<STR_LIT>' <EOL> self . save ( ) <EOL> super ( Poll , self ) . after_ui_save ( ar , cw ) <EOL> @ dd . virtualfield ( dd . HtmlBox ( _ ( "<STR_LIT>" ) ) ) <EOL> def result ( self , ar ) : <EOL> return E . div ( * tuple ( get_poll_result ( self ) ) ) <EOL> def get_poll_result ( self ) : <EOL> for cs in ChoiceSet . objects . all ( ) : <EOL> questions = self . questions . filter ( choiceset = cs ) <EOL> if questions . count ( ) > <NUM_LIT:0> : <EOL> yield E . h2 ( str ( cs ) ) <EOL> for question in questions : <EOL> yield E . p ( question . text ) <EOL> class PollDetail ( dd . DetailLayout ) : <EOL> main = "<STR_LIT>" <EOL> general = dd . Panel ( """<STR_LIT>""" , label = _ ( "<STR_LIT>" ) ) <EOL> results = dd . Panel ( """<STR_LIT>""" , label = _ ( "<STR_LIT>" ) ) <EOL> class Polls ( dd . Table ) : <EOL> required_roles = dd . required ( PollsUser ) <EOL> model = '<STR_LIT>' <EOL> column_names = '<STR_LIT>' <EOL> detail_layout = PollDetail ( ) <EOL> insert_layout = dd . InsertLayout ( """<STR_LIT>""" , window_size = ( <NUM_LIT> , <NUM_LIT:15> ) ) <EOL> class AllPolls ( Polls ) : <EOL> required_roles = dd . required ( PollsStaff ) <EOL> column_names = '<STR_LIT>' <EOL> class MyPolls ( ByUser , Polls ) : <EOL> column_names = '<STR_LIT>' <EOL> @ dd . python_2_unicode_compatible <EOL> class Question ( mixins . Sequenced ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> ordering = [ '<STR_LIT>' ] <EOL> allow_cascaded_delete = [ '<STR_LIT>' ] <EOL> poll = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> number = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:20> , blank = True ) <EOL> title = models . CharField ( pgettext ( "<STR_LIT>" , "<STR_LIT>" ) , max_length = <NUM_LIT:200> ) <EOL> details = models . TextField ( _ ( "<STR_LIT>" ) , blank = True ) <EOL> choiceset = models . ForeignKey ( '<STR_LIT>' , blank = True , null = True ) <EOL> multiple_choices = models . BooleanField ( <EOL> _ ( "<STR_LIT>" ) , blank = True , default = False ) <EOL> is_heading = models . BooleanField ( _ ( "<STR_LIT>" ) , default = False ) <EOL> NUMBERED_TITLE_FORMAT = "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> if self . number : <EOL> return self . NUMBERED_TITLE_FORMAT % ( self . number , self . title ) <EOL> return self . title <EOL> def get_siblings ( self ) : <EOL> return self . poll . questions . order_by ( '<STR_LIT>' ) <EOL> def get_choiceset ( self ) : <EOL> if self . is_heading : <EOL> return None <EOL> if self . choiceset is None : <EOL> return self . poll . default_choiceset <EOL> return self . choiceset <EOL> def full_clean ( self , * args , ** kw ) : <EOL> if self . multiple_choices is None : <EOL> self . multiple_choices = self . poll . default_multiple_choices <EOL> super ( Question , self ) . full_clean ( ) <EOL> Question . set_widget_options ( '<STR_LIT>' , width = <NUM_LIT:5> ) <EOL> class Questions ( dd . Table ) : <EOL> required_roles = dd . required ( PollsStaff ) <EOL> model = '<STR_LIT>' <EOL> column_names = "<STR_LIT>" <EOL> detail_layout = """<STR_LIT>""" <EOL> order_by = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class QuestionsByPoll ( Questions ) : <EOL> required_roles = dd . required ( PollsUser ) <EOL> master_key = '<STR_LIT>' <EOL> column_names = '<STR_LIT>' <EOL> auto_fit_column_widths = True <EOL> stay_in_grid = True <EOL> class ToggleChoice ( dd . Action ) : <EOL> """<STR_LIT>""" <EOL> readonly = False <EOL> show_in_bbar = False <EOL> parameters = dict ( <EOL> question = dd . ForeignKey ( "<STR_LIT>" ) , <EOL> choice = dd . ForeignKey ( "<STR_LIT>" ) , <EOL> ) <EOL> no_params_window = True <EOL> def run_from_ui ( self , ar , ** kw ) : <EOL> response = ar . selected_rows [ <NUM_LIT:0> ] <EOL> if response is None : <EOL> return <EOL> pv = ar . action_param_values <EOL> qs = AnswerChoice . objects . filter ( response = response , ** pv ) <EOL> if qs . count ( ) == <NUM_LIT:1> : <EOL> qs [ <NUM_LIT:0> ] . delete ( ) <EOL> elif qs . count ( ) == <NUM_LIT:0> : <EOL> if not pv . question . multiple_choices : <EOL> qs = AnswerChoice . objects . filter ( <EOL> response = response , question = pv . question ) <EOL> qs . delete ( ) <EOL> obj = AnswerChoice ( response = response , ** pv ) <EOL> obj . full_clean ( ) <EOL> obj . save ( ) <EOL> else : <EOL> raise Exception ( <EOL> "<STR_LIT>" % ( qs . query , qs . count ( ) ) ) <EOL> ar . success ( refresh = True ) <EOL> @ dd . python_2_unicode_compatible <EOL> class Response ( UserAuthored , mixins . Registrable ) : <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> ordering = [ '<STR_LIT:date>' ] <EOL> poll = dd . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> date = models . DateField ( _ ( "<STR_LIT>" ) , default = dd . today ) <EOL> state = ResponseStates . field ( default = ResponseStates . draft . as_callable ) <EOL> remark = models . TextField ( verbose_name = _ ( "<STR_LIT>" ) , blank = True ) <EOL> partner = dd . ForeignKey ( '<STR_LIT>' , blank = True , null = True ) <EOL> toggle_choice = ToggleChoice ( ) <EOL> @ dd . chooser ( ) <EOL> def poll_choices ( cls ) : <EOL> return Poll . objects . filter ( state = PollStates . published ) <EOL> def __str__ ( self ) : <EOL> if self . partner is None : <EOL> return _ ( "<STR_LIT>" ) % dict ( <EOL> user = self . user , poll = self . poll ) <EOL> return _ ( "<STR_LIT>" ) . format ( <EOL> user = self . user . initials , <EOL> date = dd . fds ( self . date ) , <EOL> partner = self . partner . get_full_name ( salutation = False ) , <EOL> poll = self . poll ) <EOL> @ classmethod <EOL> def get_registrable_fields ( model , site ) : <EOL> for f in super ( Response , model ) . get_registrable_fields ( site ) : <EOL> yield f <EOL> yield '<STR_LIT:user>' <EOL> yield '<STR_LIT>' <EOL> yield '<STR_LIT:date>' <EOL> yield '<STR_LIT>' <EOL> class ResponseDetail ( dd . DetailLayout ) : <EOL> main = "<STR_LIT>" <EOL> answers = dd . Panel ( """<STR_LIT>""" , label = _ ( "<STR_LIT>" ) ) <EOL> more = dd . Panel ( """<STR_LIT>""" , label = _ ( "<STR_LIT>" ) ) <EOL> class Responses ( dd . Table ) : <EOL> required_roles = dd . required ( PollsUser ) <EOL> model = '<STR_LIT>' <EOL> detail_layout = ResponseDetail ( ) <EOL> insert_layout = """<STR_LIT>""" <EOL> class AllResponses ( Responses ) : <EOL> required_roles = dd . required ( PollsStaff ) <EOL> class MyResponses ( ByUser , Responses ) : <EOL> column_names = '<STR_LIT>' <EOL> class ResponsesByPoll ( Responses ) : <EOL> master_key = '<STR_LIT>' <EOL> column_names = '<STR_LIT>' <EOL> class ResponsesByPartner ( Responses ) : <EOL> """<STR_LIT>""" <EOL> master_key = '<STR_LIT>' <EOL> column_names = '<STR_LIT>' <EOL> slave_grid_format = '<STR_LIT>' <EOL> @ classmethod <EOL> def get_slave_summary ( self , obj , ar ) : <EOL> """<STR_LIT>""" <EOL> if obj is None : <EOL> return <EOL> visible_polls = Poll . objects . filter ( state__in = ( <EOL> PollStates . published , PollStates . closed ) ) . order_by ( '<STR_LIT>' ) <EOL> qs = Response . objects . filter ( partner = obj ) . order_by ( '<STR_LIT:date>' ) <EOL> polls_responses = { } <EOL> for resp in qs : <EOL> polls_responses . setdefault ( resp . poll . pk , [ ] ) . append ( resp ) <EOL> items = [ ] <EOL> for poll in visible_polls : <EOL> iar = self . insert_action . request_from ( <EOL> ar , obj , known_values = dict ( poll = poll ) ) <EOL> elems = [ str ( poll ) , '<STR_LIT>' ] <EOL> responses = polls_responses . get ( poll . pk , [ ] ) <EOL> elems += join_elems ( <EOL> [ ar . obj2html ( r , dd . fds ( r . date ) ) <EOL> for r in responses ] , sep = '<STR_LIT:U+002CU+0020>' ) <EOL> if poll . state == PollStates . published : <EOL> elems += [ '<STR_LIT:U+0020>' , iar . ar2button ( ) ] <EOL> items . append ( E . li ( * elems ) ) <EOL> return E . div ( E . ul ( * items ) ) <EOL> class AnswerChoice ( dd . Model ) : <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> ordering = [ '<STR_LIT>' ] <EOL> response = models . ForeignKey ( '<STR_LIT>' ) <EOL> question = models . ForeignKey ( '<STR_LIT>' ) <EOL> choice = models . ForeignKey ( <EOL> '<STR_LIT>' , <EOL> related_name = '<STR_LIT>' , verbose_name = _ ( "<STR_LIT>" ) , <EOL> blank = True , null = True ) <EOL> @ dd . chooser ( ) <EOL> def choice_choices ( cls , question ) : <EOL> return question . get_choiceset ( ) . choices . all ( ) <EOL> class AnswerChoices ( dd . Table ) : <EOL> required_roles = dd . required ( PollsStaff ) <EOL> model = '<STR_LIT>' <EOL> @ dd . python_2_unicode_compatible <EOL> class AnswerRemark ( dd . Model ) : <EOL> class Meta ( object ) : <EOL> app_label = '<STR_LIT>' <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> ordering = [ '<STR_LIT>' ] <EOL> response = models . ForeignKey ( '<STR_LIT>' ) <EOL> question = models . ForeignKey ( '<STR_LIT>' ) <EOL> remark = models . TextField ( _ ( "<STR_LIT>" ) , blank = True ) <EOL> def __str__ ( self ) : <EOL> return str ( self . question ) <EOL> class AnswerRemarks ( dd . Table ) : <EOL> required_roles = dd . required ( PollsUser ) <EOL> model = '<STR_LIT>' <EOL> detail_layout = dd . DetailLayout ( """<STR_LIT>""" , window_size = ( <NUM_LIT> , <NUM_LIT:10> ) ) <EOL> hidden_elements = dd . fields_list ( AnswerRemark , '<STR_LIT>' ) <EOL> stay_in_grid = True <EOL> class AnswerRemarksByAnswer ( AnswerRemarks ) : <EOL> use_as_default_table = False <EOL> hide_top_toolbar = True <EOL> class AllAnswerRemarks ( AnswerRemarks ) : <EOL> required_roles = dd . required ( PollsStaff ) <EOL> @ dd . python_2_unicode_compatible <EOL> class AnswersByResponseRow ( object ) : <EOL> """<STR_LIT>""" <EOL> FORWARD_TO_QUESTION = tuple ( <EOL> "<STR_LIT>" . split ( ) ) <EOL> def __init__ ( self , response , question ) : <EOL> self . response = response <EOL> self . question = question <EOL> self . pk = self . id = question . pk <EOL> try : <EOL> self . remark = AnswerRemark . objects . get ( <EOL> question = question , response = response ) <EOL> except AnswerRemark . DoesNotExist : <EOL> self . remark = AnswerRemark ( <EOL> question = question , response = response ) <EOL> self . choices = AnswerChoice . objects . filter ( <EOL> question = question , response = response ) <EOL> for k in self . FORWARD_TO_QUESTION : <EOL> setattr ( self , k , getattr ( question , k ) ) <EOL> def __str__ ( self ) : <EOL> if self . choices . count ( ) == <NUM_LIT:0> : <EOL> return str ( _ ( "<STR_LIT>" ) ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( [ str ( ac . choice ) for ac in self . choices ] ) <EOL> class AnswerRemarkField ( dd . VirtualField ) : <EOL> """<STR_LIT>""" <EOL> editable = True <EOL> def __init__ ( self ) : <EOL> t = models . TextField ( _ ( "<STR_LIT>" ) , blank = True ) <EOL> dd . VirtualField . __init__ ( self , t , None ) <EOL> def set_value_in_object ( self , ar , obj , value ) : <EOL> obj . remark . remark = value <EOL> obj . remark . save ( ) <EOL> def value_from_object ( self , obj , ar ) : <EOL> return obj . remark . remark <EOL> class AnswersByResponse ( dd . VirtualTable ) : <EOL> """<STR_LIT>""" <EOL> label = _ ( "<STR_LIT>" ) <EOL> editable = True <EOL> master = '<STR_LIT>' <EOL> column_names = '<STR_LIT>' <EOL> variable_row_height = True <EOL> auto_fit_column_widths = True <EOL> slave_grid_format = '<STR_LIT>' <EOL> remark = AnswerRemarkField ( ) <EOL> @ classmethod <EOL> def get_data_rows ( self , ar ) : <EOL> response = ar . master_instance <EOL> if response is None : <EOL> return <EOL> for q in rt . modules . polls . Question . objects . filter ( poll = response . poll ) : <EOL> yield AnswersByResponseRow ( response , q ) <EOL> @ classmethod <EOL> def get_slave_summary ( self , response , ar ) : <EOL> """<STR_LIT>""" <EOL> if response is None : <EOL> return <EOL> if response . poll_id is None : <EOL> return <EOL> AnswerRemarks = rt . modules . polls . AnswerRemarksByAnswer <EOL> all_responses = rt . modules . polls . Response . objects . filter ( <EOL> poll = response . poll ) . order_by ( '<STR_LIT:date>' ) <EOL> if response . partner : <EOL> all_responses = all_responses . filter ( partner = response . partner ) <EOL> ht = xghtml . Table ( ) <EOL> ht . attrib . update ( cellspacing = "<STR_LIT>" , bgcolor = "<STR_LIT>" , width = "<STR_LIT>" ) <EOL> cellattrs = dict ( align = "<STR_LIT:left>" , valign = "<STR_LIT>" , bgcolor = "<STR_LIT>" ) <EOL> headers = [ _ ( "<STR_LIT>" ) ] <EOL> for r in all_responses : <EOL> if r == response : <EOL> headers . append ( dd . fds ( r . date ) ) <EOL> else : <EOL> headers . append ( ar . obj2html ( r , dd . fds ( r . date ) ) ) <EOL> ht . add_header_row ( * headers , ** cellattrs ) <EOL> ar . master_instance = response <EOL> sar = Responses . update_action . request_from ( ar ) <EOL> sar . selected_rows = [ response ] <EOL> editable = sar . get_permission ( ) <EOL> kv = dict ( response = response ) <EOL> insert = AnswerRemarks . insert_action . request_from ( <EOL> ar , known_values = kv ) <EOL> detail = AnswerRemarks . detail_action . request_from ( ar ) <EOL> for answer in self . get_data_rows ( ar ) : <EOL> cells = [ self . question . value_from_object ( answer , ar ) ] <EOL> for r in all_responses : <EOL> if editable and r == response : <EOL> insert . known_values . update ( question = answer . question ) <EOL> detail . known_values . update ( question = answer . question ) <EOL> items = [ <EOL> self . answer_buttons . value_from_object ( answer , ar ) ] <EOL> if answer . remark . remark : <EOL> items += [ E . br ( ) , answer . remark . remark ] <EOL> if answer . remark . pk : <EOL> items += [ <EOL> '<STR_LIT:U+0020>' , <EOL> detail . ar2button ( <EOL> answer . remark , _ ( "<STR_LIT>" ) , <EOL> icon_name = None ) ] <EOL> else : <EOL> btn = insert . ar2button ( <EOL> answer . remark , _ ( "<STR_LIT>" ) , icon_name = None ) <EOL> items += [ "<STR_LIT>" , btn , "<STR_LIT:)>" ] <EOL> else : <EOL> other_answer = AnswersByResponseRow ( r , answer . question ) <EOL> items = [ str ( other_answer ) ] <EOL> if other_answer . remark . remark : <EOL> items += [ E . br ( ) , answer . remark . remark ] <EOL> cells . append ( E . p ( * items ) ) <EOL> ht . add_body_row ( * cells , ** cellattrs ) <EOL> return E . div ( ht . as_element ( ) , class_ = "<STR_LIT>" ) <EOL> @ dd . displayfield ( _ ( "<STR_LIT>" ) ) <EOL> def answer_buttons ( self , obj , ar ) : <EOL> cs = obj . question . get_choiceset ( ) <EOL> if cs is None : <EOL> return '<STR_LIT>' <EOL> elems = [ ] <EOL> pv = dict ( question = obj . question ) <EOL> ba = Responses . actions . toggle_choice <EOL> if ba is None : <EOL> raise Exception ( "<STR_LIT>" . format ( ar . actor ) ) <EOL> sar = ba . request_from ( ar ) <EOL> sar . selected_rows = [ obj . response ] <EOL> if not sar . get_permission ( ) : <EOL> return str ( obj ) <EOL> AnswerChoice = rt . modules . polls . AnswerChoice <EOL> for c in cs . choices . all ( ) : <EOL> pv . update ( choice = c ) <EOL> text = str ( c ) <EOL> qs = AnswerChoice . objects . filter ( <EOL> response = obj . response , ** pv ) <EOL> if qs . count ( ) == <NUM_LIT:1> : <EOL> text = [ E . b ( '<STR_LIT:[>' , text , '<STR_LIT:]>' ) ] <EOL> elif qs . count ( ) == <NUM_LIT:0> : <EOL> pass <EOL> else : <EOL> raise Exception ( <EOL> "<STR_LIT>" % ( qs . query , qs . count ( ) ) ) <EOL> sar . set_action_param_values ( ** pv ) <EOL> e = sar . ar2button ( obj . response , text , style = "<STR_LIT>" ) <EOL> elems . append ( e ) <EOL> return E . span ( * join_elems ( elems ) , class_ = "<STR_LIT>" ) <EOL> @ classmethod <EOL> def get_pk_field ( self ) : <EOL> return Question . _meta . pk <EOL> @ classmethod <EOL> def get_row_by_pk ( self , ar , pk ) : <EOL> response = ar . master_instance <EOL> q = rt . modules . polls . Question . objects . get ( pk = pk ) <EOL> return AnswersByResponseRow ( response , q ) <EOL> @ classmethod <EOL> def disable_delete ( self , obj , ar ) : <EOL> return "<STR_LIT>" <EOL> @ dd . displayfield ( _ ( "<STR_LIT>" ) ) <EOL> def question ( self , obj , ar ) : <EOL> if obj . question . number : <EOL> txt = obj . question . NUMBERED_TITLE_FORMAT % ( <EOL> obj . question . number , obj . question . title ) <EOL> else : <EOL> txt = obj . question . title <EOL> attrs = dict ( class_ = "<STR_LIT>" ) <EOL> if obj . question . details : <EOL> attrs . update ( title = obj . question . details ) <EOL> if obj . question . is_heading : <EOL> txt = E . b ( txt , ** attrs ) <EOL> return E . span ( txt , ** attrs ) <EOL> @ dd . python_2_unicode_compatible <EOL> class AnswersByQuestionRow ( object ) : <EOL> """<STR_LIT>""" <EOL> FORWARD_TO_RESPONSE = tuple ( <EOL> "<STR_LIT>" . split ( ) ) <EOL> def __init__ ( self , response , question ) : <EOL> self . response = response <EOL> self . question = question <EOL> self . pk = self . id = response . pk <EOL> try : <EOL> self . remark = AnswerRemark . objects . get ( <EOL> question = question , response = response ) . remark <EOL> except AnswerRemark . DoesNotExist : <EOL> self . remark = '<STR_LIT>' <EOL> self . choices = AnswerChoice . objects . filter ( <EOL> question = question , response = response ) <EOL> for k in self . FORWARD_TO_RESPONSE : <EOL> setattr ( self , k , getattr ( question , k ) ) <EOL> def __str__ ( self ) : <EOL> if self . choices . count ( ) == <NUM_LIT:0> : <EOL> return str ( _ ( "<STR_LIT>" ) ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( [ str ( ac . choice ) for ac in self . choices ] ) <EOL> class AnswersByQuestion ( dd . VirtualTable ) : <EOL> """<STR_LIT>""" <EOL> label = _ ( "<STR_LIT>" ) <EOL> master = '<STR_LIT>' <EOL> column_names = '<STR_LIT>' <EOL> variable_row_height = True <EOL> auto_fit_column_widths = True <EOL> @ classmethod <EOL> def get_data_rows ( self , ar ) : <EOL> question = ar . master_instance <EOL> if question is None : <EOL> return <EOL> for r in rt . modules . polls . Response . objects . filter ( poll = question . poll ) : <EOL> yield AnswersByQuestionRow ( r , question ) <EOL> @ dd . displayfield ( _ ( "<STR_LIT>" ) ) <EOL> def response ( self , obj , ar ) : <EOL> return ar . obj2html ( obj . response ) <EOL> @ dd . displayfield ( _ ( "<STR_LIT>" ) ) <EOL> def remark ( self , obj , ar ) : <EOL> return obj . remark <EOL> @ dd . displayfield ( _ ( "<STR_LIT>" ) ) <EOL> def answer ( self , obj , ar ) : <EOL> return str ( obj ) <EOL> class PollResult ( Questions ) : <EOL> "<STR_LIT>" <EOL> master_key = '<STR_LIT>' <EOL> column_names = "<STR_LIT>" <EOL> @ dd . virtualfield ( dd . ForeignKey ( '<STR_LIT>' ) ) <EOL> def question ( self , obj , ar ) : <EOL> return obj <EOL> @ dd . requestfield ( _ ( "<STR_LIT>" ) ) <EOL> def answers ( self , obj , ar ) : <EOL> return AnswerChoices . request ( known_values = dict ( question = obj ) ) <EOL> @ dd . requestfield ( _ ( "<STR_LIT>" ) ) <EOL> def a1 ( self , obj , ar ) : <EOL> cs = obj . get_choiceset ( ) <EOL> if cs is not None : <EOL> c = next ( iter ( cs . choices . all ( ) ) ) <EOL> return AnswerChoices . request ( <EOL> known_values = dict ( question = obj , choice = c ) ) </s>
