<s> """<STR_LIT>""" <EOL> import logging <EOL> from collections import Counter <EOL> from datetime import datetime , date <EOL> from random import randint , seed , shuffle <EOL> from time import mktime <EOL> from django . contrib . admin . views . decorators import staff_member_required <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . http import HttpResponse <EOL> from django . shortcuts import get_object_or_404 , redirect , render <EOL> from django . template import Context <EOL> from django . template . defaultfilters import slugify <EOL> from django . template . loader import get_template <EOL> from appraise . evaluation . models import APPRAISE_TASK_TYPE_CHOICES , EvaluationTask , EvaluationItem , EvaluationResult <EOL> from appraise . settings import LOG_LEVEL , LOG_HANDLER , COMMIT_TAG <EOL> logging . basicConfig ( level = LOG_LEVEL ) <EOL> LOGGER = logging . getLogger ( '<STR_LIT>' ) <EOL> LOGGER . addHandler ( LOG_HANDLER ) <EOL> ERROR_CLASSES = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> APPRAISE_TASK_CACHE = { } <EOL> def _update_task_cache ( task , user ) : <EOL> """<STR_LIT>""" <EOL> if not APPRAISE_TASK_CACHE . has_key ( task . task_id ) : <EOL> APPRAISE_TASK_CACHE [ task . task_id ] = { } <EOL> _cache = APPRAISE_TASK_CACHE [ task . task_id ] <EOL> _task_data = { <EOL> '<STR_LIT>' : task . is_finished_for_user ( user ) , <EOL> '<STR_LIT>' : task . get_status_header , <EOL> '<STR_LIT:status>' : task . get_status_for_user ( user ) , <EOL> '<STR_LIT>' : task . get_status_for_users ( ) , <EOL> '<STR_LIT>' : task . task_name , <EOL> '<STR_LIT:url>' : task . get_absolute_url ( ) , <EOL> '<STR_LIT>' : task . get_status_url ( ) , <EOL> } <EOL> _cache . update ( { user . username : _task_data } ) <EOL> def _save_results ( item , user , duration , raw_result ) : <EOL> """<STR_LIT>""" <EOL> LOGGER . debug ( '<STR_LIT>' . format ( <EOL> item , user , duration , raw_result . encode ( '<STR_LIT:utf-8>' ) ) ) <EOL> _existing_result = EvaluationResult . objects . filter ( item = item , user = user ) <EOL> if _existing_result : <EOL> _result = _existing_result [ <NUM_LIT:0> ] <EOL> else : <EOL> _result = EvaluationResult ( item = item , user = user ) <EOL> try : <EOL> from django . utils . dateparse import parse_time <EOL> _result . duration = str ( duration ) <EOL> except TypeError : <EOL> _result . duration = duration <EOL> _result . raw_result = raw_result <EOL> _result . save ( ) <EOL> def _find_next_item_to_process ( items , user , random_order = False ) : <EOL> """<STR_LIT>""" <EOL> user_results = EvaluationResult . objects . filter ( user = user ) <EOL> processed_items = user_results . values_list ( '<STR_LIT>' , flat = True ) <EOL> unprocessed_items = list ( items . exclude ( pk__in = processed_items ) ) <EOL> if random_order : <EOL> shuffle ( unprocessed_items ) <EOL> if unprocessed_items : <EOL> return unprocessed_items [ <NUM_LIT:0> ] <EOL> return None <EOL> def _compute_context_for_item ( item ) : <EOL> """<STR_LIT>""" <EOL> source_text = [ None , None , None ] <EOL> reference_text = [ None , None , None ] <EOL> left_context = EvaluationItem . objects . filter ( task = item . task , pk = item . id - <NUM_LIT:1> ) <EOL> right_context = EvaluationItem . objects . filter ( task = item . task , pk = item . id + <NUM_LIT:1> ) <EOL> _item_doc_id = getattr ( item . attributes , '<STR_LIT>' , None ) <EOL> source_text [ <NUM_LIT:1> ] = item . source [ <NUM_LIT:0> ] <EOL> if item . reference : <EOL> reference_text [ <NUM_LIT:1> ] = item . reference [ <NUM_LIT:0> ] <EOL> if left_context : <EOL> _left = left_context [ <NUM_LIT:0> ] <EOL> _left_doc_id = getattr ( _left . attributes , '<STR_LIT>' , None ) <EOL> if _left_doc_id == _item_doc_id : <EOL> source_text [ <NUM_LIT:0> ] = _left . source [ <NUM_LIT:0> ] <EOL> if _left . reference : <EOL> reference_text [ <NUM_LIT:0> ] = _left . reference [ <NUM_LIT:0> ] <EOL> if right_context : <EOL> _right = right_context [ <NUM_LIT:0> ] <EOL> _right_doc_id = getattr ( _right . attributes , '<STR_LIT>' , None ) <EOL> if _right_doc_id == _item_doc_id : <EOL> source_text [ <NUM_LIT:2> ] = _right . source [ <NUM_LIT:0> ] <EOL> if _right . reference : <EOL> reference_text [ <NUM_LIT:2> ] = _right . reference [ <NUM_LIT:0> ] <EOL> return ( source_text , reference_text ) <EOL> @ login_required <EOL> def _handle_quality_checking ( request , task , items ) : <EOL> """<STR_LIT>""" <EOL> start_datetime = datetime . now ( ) <EOL> form_valid = False <EOL> if request . method == "<STR_LIT:POST>" : <EOL> item_id = request . POST . get ( '<STR_LIT>' , None ) <EOL> now_timestamp = request . POST . get ( '<STR_LIT>' , None ) <EOL> submit_button = request . POST . get ( '<STR_LIT>' , None ) <EOL> form_valid = all ( ( item_id , now_timestamp , submit_button ) ) <EOL> if form_valid : <EOL> current_item = get_object_or_404 ( EvaluationItem , pk = int ( item_id ) ) <EOL> now_datetime = datetime . fromtimestamp ( float ( now_timestamp ) ) <EOL> duration = start_datetime - now_datetime <EOL> if submit_button == '<STR_LIT>' : <EOL> _raw_result = '<STR_LIT>' <EOL> else : <EOL> _raw_result = submit_button <EOL> _save_results ( current_item , request . user , duration , _raw_result ) <EOL> item = _find_next_item_to_process ( items , request . user , task . random_order ) <EOL> if not item : <EOL> return redirect ( '<STR_LIT>' ) <EOL> source_text , reference_text = _compute_context_for_item ( item ) <EOL> finished_items , total_items = task . get_finished_for_user ( request . user ) <EOL> finished_items += <NUM_LIT:1> <EOL> dictionary = { <EOL> '<STR_LIT>' : request . path , <EOL> '<STR_LIT>' : COMMIT_TAG , <EOL> '<STR_LIT:description>' : task . description , <EOL> '<STR_LIT>' : item . id , <EOL> '<STR_LIT>' : mktime ( datetime . now ( ) . timetuple ( ) ) , <EOL> '<STR_LIT>' : reference_text , <EOL> '<STR_LIT>' : source_text , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( finished_items , total_items ) , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : item . translations [ <NUM_LIT:0> ] , <EOL> } <EOL> return render ( request , '<STR_LIT>' , dictionary ) <EOL> @ login_required <EOL> def _handle_ranking ( request , task , items ) : <EOL> """<STR_LIT>""" <EOL> form_valid = False <EOL> if request . method == "<STR_LIT:POST>" : <EOL> item_id = request . POST . get ( '<STR_LIT>' , None ) <EOL> end_timestamp = request . POST . get ( '<STR_LIT>' , None ) <EOL> order_random = request . POST . get ( '<STR_LIT>' , None ) <EOL> start_timestamp = request . POST . get ( '<STR_LIT>' , None ) <EOL> submit_button = request . POST . get ( '<STR_LIT>' , None ) <EOL> form_valid = all ( ( item_id , end_timestamp , order_random , <EOL> start_timestamp , submit_button ) ) <EOL> if form_valid : <EOL> current_item = get_object_or_404 ( EvaluationItem , pk = int ( item_id ) ) <EOL> start_datetime = datetime . fromtimestamp ( float ( start_timestamp ) ) <EOL> end_datetime = datetime . fromtimestamp ( float ( end_timestamp ) ) <EOL> duration = end_datetime - start_datetime <EOL> order = [ int ( x ) for x in order_random . split ( '<STR_LIT:U+002C>' ) ] <EOL> ranks = { } <EOL> for index in range ( len ( current_item . translations ) ) : <EOL> rank = request . POST . get ( '<STR_LIT>' . format ( index ) , - <NUM_LIT:1> ) <EOL> ranks [ order [ index ] ] = int ( rank ) <EOL> if submit_button == '<STR_LIT>' : <EOL> _raw_result = '<STR_LIT>' <EOL> elif submit_button == '<STR_LIT>' : <EOL> _raw_result = range ( len ( current_item . translations ) ) <EOL> _raw_result = '<STR_LIT:U+002C>' . join ( [ str ( ranks [ x ] ) for x in _raw_result ] ) <EOL> _save_results ( current_item , request . user , duration , _raw_result ) <EOL> item = _find_next_item_to_process ( items , request . user , task . random_order ) <EOL> if not item : <EOL> return redirect ( '<STR_LIT>' ) <EOL> source_text , reference_text = _compute_context_for_item ( item ) <EOL> finished_items , total_items = task . get_finished_for_user ( request . user ) <EOL> finished_items += <NUM_LIT:1> <EOL> translations = [ ] <EOL> order = range ( len ( item . translations ) ) <EOL> shuffle ( order ) <EOL> for index in order : <EOL> translations . append ( item . translations [ index ] ) <EOL> dictionary = { <EOL> '<STR_LIT>' : request . path , <EOL> '<STR_LIT>' : COMMIT_TAG , <EOL> '<STR_LIT:description>' : task . description , <EOL> '<STR_LIT>' : item . id , <EOL> '<STR_LIT>' : '<STR_LIT:U+002C>' . join ( [ str ( x ) for x in order ] ) , <EOL> '<STR_LIT>' : reference_text , <EOL> '<STR_LIT>' : source_text , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( finished_items , total_items ) , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : translations , <EOL> } <EOL> return render ( request , '<STR_LIT>' , dictionary ) <EOL> @ login_required <EOL> def _handle_postediting ( request , task , items ) : <EOL> """<STR_LIT>""" <EOL> if request . method == "<STR_LIT:POST>" : <EOL> item_id = request . POST . get ( '<STR_LIT>' ) <EOL> edit_id = request . POST . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> end_timestamp = request . POST . get ( '<STR_LIT>' , None ) <EOL> order_random = request . POST . get ( '<STR_LIT>' , None ) <EOL> submit_button = request . POST . get ( '<STR_LIT>' ) <EOL> from_scratch = request . POST . get ( '<STR_LIT>' ) <EOL> postedited = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> start_timestamp = request . POST . get ( '<STR_LIT>' , None ) <EOL> current_item = get_object_or_404 ( EvaluationItem , pk = int ( item_id ) ) <EOL> duration = None <EOL> if end_timestamp and start_timestamp : <EOL> start_datetime = datetime . fromtimestamp ( float ( start_timestamp ) ) <EOL> end_datetime = datetime . fromtimestamp ( float ( end_timestamp ) ) <EOL> duration = end_datetime - start_datetime <EOL> order = [ int ( x ) for x in order_random . split ( '<STR_LIT:U+002C>' ) ] <EOL> real_id = str ( order [ int ( edit_id ) ] ) <EOL> print <EOL> print "<STR_LIT>" . format ( item_id ) <EOL> print "<STR_LIT>" . format ( edit_id ) <EOL> print "<STR_LIT>" . format ( real_id ) <EOL> print "<STR_LIT>" . format ( submit_button ) <EOL> print "<STR_LIT>" . format ( from_scratch ) <EOL> print "<STR_LIT>" . format ( postedited . encode ( '<STR_LIT:utf-8>' ) ) <EOL> print <EOL> print request . POST <EOL> print <EOL> if submit_button == '<STR_LIT>' : <EOL> _results = [ ] <EOL> if from_scratch : <EOL> _results . append ( '<STR_LIT>' ) <EOL> _results . append ( real_id ) <EOL> _results . append ( postedited ) <EOL> _raw_result = '<STR_LIT:\n>' . join ( _results ) <EOL> elif submit_button == '<STR_LIT>' : <EOL> _raw_result = '<STR_LIT>' <EOL> _save_results ( current_item , request . user , duration , _raw_result ) <EOL> item = _find_next_item_to_process ( items , request . user , task . random_order ) <EOL> if not item : <EOL> return redirect ( '<STR_LIT>' ) <EOL> source_text , reference_text = _compute_context_for_item ( item ) <EOL> _finished , _total = task . get_finished_for_user ( request . user ) <EOL> translations = [ ] <EOL> order = range ( len ( item . translations ) ) <EOL> shuffle ( order ) <EOL> for index in order : <EOL> translations . append ( item . translations [ index ] ) <EOL> dictionary = { '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : item . id , <EOL> '<STR_LIT>' : source_text , '<STR_LIT>' : reference_text , <EOL> '<STR_LIT>' : translations , <EOL> '<STR_LIT:description>' : task . description , <EOL> '<STR_LIT>' : '<STR_LIT:U+002C>' . join ( [ str ( x ) for x in order ] ) , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( _finished + <NUM_LIT:1> , _total ) , <EOL> '<STR_LIT>' : request . path , '<STR_LIT>' : COMMIT_TAG } <EOL> return render ( request , '<STR_LIT>' , dictionary ) <EOL> @ login_required <EOL> def _handle_error_classification ( request , task , items ) : <EOL> """<STR_LIT>""" <EOL> if request . method == "<STR_LIT:POST>" : <EOL> end_timestamp = request . POST . get ( '<STR_LIT>' , None ) <EOL> item_id = request . POST . get ( '<STR_LIT>' ) <EOL> words = request . POST . get ( '<STR_LIT>' ) <EOL> missing_words = request . POST . get ( '<STR_LIT>' ) <EOL> too_many_errors = request . POST . get ( '<STR_LIT>' ) <EOL> start_timestamp = request . POST . get ( '<STR_LIT>' , None ) <EOL> submit_button = request . POST . get ( '<STR_LIT>' ) <EOL> current_item = get_object_or_404 ( EvaluationItem , pk = int ( item_id ) ) <EOL> duration = None <EOL> if end_timestamp and start_timestamp : <EOL> start_datetime = datetime . fromtimestamp ( float ( start_timestamp ) ) <EOL> end_datetime = datetime . fromtimestamp ( float ( end_timestamp ) ) <EOL> duration = end_datetime - start_datetime <EOL> errors = { } <EOL> if words : <EOL> for index in range ( int ( words ) ) : <EOL> _errors = { } <EOL> for error in ERROR_CLASSES : <EOL> severity = request . POST . get ( '<STR_LIT>' . format ( error , index ) ) <EOL> if severity and severity != "<STR_LIT>" : <EOL> _errors [ error ] = severity <EOL> if _errors : <EOL> errors [ index ] = _errors <EOL> print <EOL> print "<STR_LIT>" . format ( item_id ) <EOL> print "<STR_LIT>" . format ( missing_words ) <EOL> print "<STR_LIT>" . format ( too_many_errors ) <EOL> print "<STR_LIT>" . format ( submit_button ) <EOL> print "<STR_LIT>" . format ( errors ) <EOL> print <EOL> if submit_button == '<STR_LIT>' : <EOL> if too_many_errors : <EOL> _raw_result = '<STR_LIT>' <EOL> else : <EOL> _errors = [ ] <EOL> if missing_words : <EOL> _errors . append ( '<STR_LIT>' ) <EOL> for index , data in errors . items ( ) : <EOL> _word_i = [ '<STR_LIT>' . format ( k , v ) for k , v in data . items ( ) ] <EOL> _errors . append ( '<STR_LIT>' . format ( index , '<STR_LIT:U+002C>' . join ( _word_i ) ) ) <EOL> _raw_result = '<STR_LIT:\n>' . join ( _errors ) <EOL> elif submit_button == '<STR_LIT>' : <EOL> _raw_result = '<STR_LIT>' <EOL> _save_results ( current_item , request . user , duration , _raw_result ) <EOL> item = _find_next_item_to_process ( items , request . user , task . random_order ) <EOL> if not item : <EOL> return redirect ( '<STR_LIT>' ) <EOL> source_text , reference_text = _compute_context_for_item ( item ) <EOL> _finished , _total = task . get_finished_for_user ( request . user ) <EOL> translation = item . translations [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> words = item . translations [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+0020>' ) <EOL> dictionary = { '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : item . id , <EOL> '<STR_LIT>' : source_text , '<STR_LIT>' : reference_text , <EOL> '<STR_LIT>' : translation , <EOL> '<STR_LIT>' : words , <EOL> '<STR_LIT:description>' : task . description , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( _finished + <NUM_LIT:1> , _total ) , <EOL> '<STR_LIT>' : request . path , '<STR_LIT>' : COMMIT_TAG } <EOL> return render ( request , '<STR_LIT>' , dictionary ) <EOL> def _handle_three_way_ranking ( request , task , items ) : <EOL> """<STR_LIT>""" <EOL> start_datetime = datetime . now ( ) <EOL> form_valid = False <EOL> if request . method == "<STR_LIT:POST>" : <EOL> item_id = request . POST . get ( '<STR_LIT>' , None ) <EOL> now_timestamp = request . POST . get ( '<STR_LIT>' , None ) <EOL> order_reversed = request . POST . get ( '<STR_LIT>' , None ) <EOL> submit_button = request . POST . get ( '<STR_LIT>' , None ) <EOL> form_valid = all ( ( item_id , now_timestamp , order_reversed , <EOL> submit_button ) ) <EOL> if form_valid : <EOL> current_item = get_object_or_404 ( EvaluationItem , pk = int ( item_id ) ) <EOL> now_datetime = datetime . fromtimestamp ( float ( now_timestamp ) ) <EOL> duration = start_datetime - now_datetime <EOL> if submit_button == '<STR_LIT>' : <EOL> _raw_result = '<STR_LIT>' <EOL> else : <EOL> _raw_result = submit_button <EOL> if order_reversed == '<STR_LIT:yes>' : <EOL> if submit_button == '<STR_LIT>' : <EOL> _raw_result = '<STR_LIT>' <EOL> elif submit_button == '<STR_LIT>' : <EOL> _raw_result = '<STR_LIT>' <EOL> _save_results ( current_item , request . user , duration , _raw_result ) <EOL> item = _find_next_item_to_process ( items , request . user , task . random_order ) <EOL> if not item : <EOL> return redirect ( '<STR_LIT>' ) <EOL> source_text , reference_text = _compute_context_for_item ( item ) <EOL> if source_text [ <NUM_LIT:0> ] : <EOL> source_text [ <NUM_LIT:0> ] = source_text [ <NUM_LIT:0> ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> source_text [ <NUM_LIT:1> ] = source_text [ <NUM_LIT:1> ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> source_text [ <NUM_LIT:1> ] = source_text [ <NUM_LIT:1> ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if source_text [ <NUM_LIT:2> ] : <EOL> source_text [ <NUM_LIT:2> ] = source_text [ <NUM_LIT:2> ] . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> finished_items , total_items = task . get_finished_for_user ( request . user ) <EOL> finished_items += <NUM_LIT:1> <EOL> translations = item . translations [ : <NUM_LIT:2> ] <EOL> order_reversed = False <EOL> if randint ( <NUM_LIT:0> , <NUM_LIT:1> ) : <EOL> translations . reverse ( ) <EOL> order_reversed = True <EOL> dictionary = { <EOL> '<STR_LIT>' : request . path , <EOL> '<STR_LIT>' : COMMIT_TAG , <EOL> '<STR_LIT:description>' : task . description , <EOL> '<STR_LIT>' : item . id , <EOL> '<STR_LIT>' : mktime ( datetime . now ( ) . timetuple ( ) ) , <EOL> '<STR_LIT>' : order_reversed , <EOL> '<STR_LIT>' : reference_text , <EOL> '<STR_LIT>' : source_text , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( finished_items , total_items ) , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : translations , <EOL> } <EOL> return render ( request , '<STR_LIT>' , dictionary ) <EOL> @ login_required <EOL> def task_handler ( request , task_id ) : <EOL> """<STR_LIT>""" <EOL> LOGGER . info ( '<STR_LIT>' . format ( <EOL> request . user . username or "<STR_LIT>" ) ) <EOL> task = get_object_or_404 ( EvaluationTask , task_id = task_id ) <EOL> items = EvaluationItem . objects . filter ( task = task ) <EOL> if not items : <EOL> return redirect ( '<STR_LIT>' ) <EOL> _task_type = task . get_task_type_display ( ) <EOL> if _task_type == '<STR_LIT>' : <EOL> return _handle_quality_checking ( request , task , items ) <EOL> elif _task_type == '<STR_LIT>' : <EOL> return _handle_ranking ( request , task , items ) <EOL> elif _task_type == '<STR_LIT>' : <EOL> return _handle_postediting ( request , task , items ) <EOL> elif _task_type == '<STR_LIT>' : <EOL> return _handle_error_classification ( request , task , items ) <EOL> elif _task_type == '<STR_LIT>' : <EOL> return _handle_three_way_ranking ( request , task , items ) <EOL> _msg = '<STR_LIT>' . format ( _task_type ) <EOL> raise NotImplementedError , _msg <EOL> @ login_required <EOL> def overview ( request ) : <EOL> """<STR_LIT>""" <EOL> LOGGER . info ( '<STR_LIT>' . format ( <EOL> request . user . username or "<STR_LIT>" ) ) <EOL> if request . user . groups . filter ( name = "<STR_LIT>" ) . exists ( ) : <EOL> LOGGER . info ( '<STR_LIT>' . format ( <EOL> request . user . username ) ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> seed ( None ) <EOL> evaluation_tasks = { } <EOL> for task_type_id , task_type in APPRAISE_TASK_TYPE_CHOICES : <EOL> evaluation_tasks [ task_type ] = [ ] <EOL> if request . user . is_superuser : <EOL> _tasks = EvaluationTask . objects . filter ( task_type = task_type_id ) <EOL> else : <EOL> _tasks = EvaluationTask . objects . filter ( task_type = task_type_id , <EOL> users = request . user , active = True ) <EOL> for _task in _tasks : <EOL> if not APPRAISE_TASK_CACHE . has_key ( _task . task_id ) : <EOL> APPRAISE_TASK_CACHE [ _task . task_id ] = { } <EOL> _cache = APPRAISE_TASK_CACHE [ _task . task_id ] <EOL> if not _cache . has_key ( request . user . username ) : <EOL> _update_task_cache ( _task , request . user ) <EOL> _task_data = _cache [ request . user . username ] <EOL> evaluation_tasks [ task_type ] . append ( _task_data ) <EOL> if len ( evaluation_tasks [ task_type ] ) == <NUM_LIT:0> : <EOL> evaluation_tasks . pop ( task_type ) <EOL> dictionary = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : COMMIT_TAG , <EOL> '<STR_LIT>' : evaluation_tasks , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> } <EOL> return render ( request , '<STR_LIT>' , dictionary ) <EOL> @ staff_member_required <EOL> def status_view ( request , task_id = None ) : <EOL> """<STR_LIT>""" <EOL> LOGGER . info ( '<STR_LIT>' . format ( <EOL> request . user . username ) ) <EOL> if request . user . groups . filter ( name = "<STR_LIT>" ) . exists ( ) : <EOL> LOGGER . info ( '<STR_LIT>' . format ( <EOL> request . user . username ) ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> if task_id : <EOL> task = get_object_or_404 ( EvaluationTask , task_id = task_id ) <EOL> headers = task . get_status_header ( ) <EOL> status = [ ] <EOL> for user in task . users . all ( ) : <EOL> status . append ( ( user . username , task . get_status_for_user ( user ) ) ) <EOL> scores = None <EOL> result_data = [ ] <EOL> raw_result_data = Counter ( ) <EOL> users = list ( task . users . all ( ) ) <EOL> for item in EvaluationItem . objects . filter ( task = task ) : <EOL> results = [ ] <EOL> for user in users : <EOL> qset = EvaluationResult . objects . filter ( user = user , item = item ) <EOL> if qset . exists ( ) : <EOL> category = str ( qset [ <NUM_LIT:0> ] . results ) <EOL> results . append ( ( user . id , item . id , category ) ) <EOL> raw_result_data [ qset [ <NUM_LIT:0> ] . raw_result ] += <NUM_LIT:1> <EOL> if len ( results ) == len ( users ) : <EOL> result_data . extend ( results ) <EOL> _raw_results = [ ] <EOL> _keys = raw_result_data . keys ( ) <EOL> _total_results = float ( sum ( raw_result_data . values ( ) ) ) <EOL> for key in sorted ( _keys ) : <EOL> value = raw_result_data [ key ] <EOL> _raw_results . append ( ( key , value , <NUM_LIT:100> * value / _total_results ) ) <EOL> try : <EOL> if len ( users ) > <NUM_LIT:1> : <EOL> try : <EOL> from nltk . metrics . agreement import AnnotationTask <EOL> chk = AnnotationTask ( data = [ ( '<STR_LIT:b>' , '<STR_LIT:1>' , '<STR_LIT:k>' ) , <EOL> ( '<STR_LIT:a>' , '<STR_LIT:1>' , '<STR_LIT:k>' ) ] ) <EOL> assert ( chk == <NUM_LIT:1.0> ) <EOL> except AssertionError : <EOL> LOGGER . debug ( '<STR_LIT>' ) <EOL> from appraise . utils import AnnotationTask <EOL> result_data . sort ( ) <EOL> annotation_task = AnnotationTask ( result_data ) <EOL> scores = ( <EOL> annotation_task . alpha ( ) , <EOL> annotation_task . kappa ( ) , <EOL> annotation_task . S ( ) , <EOL> annotation_task . pi ( ) <EOL> ) <EOL> except ZeroDivisionError : <EOL> scores = None <EOL> except ImportError : <EOL> scores = None <EOL> dictionary = { <EOL> '<STR_LIT>' : task . get_status_for_users ( ) , <EOL> '<STR_LIT>' : COMMIT_TAG , <EOL> '<STR_LIT>' : headers , <EOL> '<STR_LIT>' : scores , <EOL> '<STR_LIT>' : _raw_results , <EOL> '<STR_LIT:status>' : status , <EOL> '<STR_LIT>' : task . task_id , <EOL> '<STR_LIT>' : task . task_name , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> } <EOL> return render ( request , '<STR_LIT>' , dictionary ) <EOL> else : <EOL> evaluation_tasks = { } <EOL> for task_type_id , task_type in APPRAISE_TASK_TYPE_CHOICES : <EOL> evaluation_tasks [ task_type ] = [ ] <EOL> if request . user . is_superuser : <EOL> _tasks = EvaluationTask . objects . filter ( task_type = task_type_id ) <EOL> else : <EOL> _tasks = EvaluationTask . objects . filter ( task_type = task_type_id , <EOL> active = True ) <EOL> for _task in _tasks : <EOL> if not APPRAISE_TASK_CACHE . has_key ( _task . task_id ) : <EOL> APPRAISE_TASK_CACHE [ _task . task_id ] = { } <EOL> _cache = APPRAISE_TASK_CACHE [ _task . task_id ] <EOL> if not _cache . has_key ( request . user . username ) : <EOL> _update_task_cache ( _task , request . user ) <EOL> _task_data = _cache [ request . user . username ] <EOL> evaluation_tasks [ task_type ] . append ( _task_data ) <EOL> if len ( evaluation_tasks [ task_type ] ) == <NUM_LIT:0> : <EOL> evaluation_tasks . pop ( task_type ) <EOL> dictionary = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : COMMIT_TAG , <EOL> '<STR_LIT>' : evaluation_tasks , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> } <EOL> return render ( request , '<STR_LIT>' , dictionary ) <EOL> @ staff_member_required <EOL> def export_task_results ( request , task_id = None ) : <EOL> """<STR_LIT>""" <EOL> task = get_object_or_404 ( EvaluationTask , task_id = task_id ) <EOL> template = get_template ( '<STR_LIT>' ) <EOL> export_xml = template . render ( Context ( { '<STR_LIT>' : [ task . export_to_xml ( ) ] } ) ) <EOL> export_filename = '<STR_LIT>' . format ( slugify ( task . task_name ) , <EOL> date . today ( ) ) <EOL> response = HttpResponse ( export_xml , mimetype = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> export_filename ) <EOL> return response <EOL> @ staff_member_required <EOL> def export_agreement_data ( request , task_id = None ) : <EOL> """<STR_LIT>""" <EOL> task = get_object_or_404 ( EvaluationTask , task_id = task_id ) <EOL> result_data = [ ] <EOL> users = list ( task . users . all ( ) ) <EOL> for item in EvaluationItem . objects . filter ( task = task ) : <EOL> results = [ ] <EOL> for user in users : <EOL> qset = EvaluationResult . objects . filter ( user = user , item = item ) <EOL> if qset . exists ( ) : <EOL> category = str ( qset [ <NUM_LIT:0> ] . results ) <EOL> data = '<STR_LIT>' . format ( user . username , item . id , category ) <EOL> results . append ( data ) <EOL> if len ( results ) == len ( users ) : <EOL> result_data . extend ( results ) <EOL> export_txt = '<STR_LIT:\n>' . join ( result_data ) <EOL> export_filename = '<STR_LIT>' . format ( slugify ( task . task_name ) , <EOL> date . today ( ) ) <EOL> response = HttpResponse ( export_txt , mimetype = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> export_filename ) <EOL> return response </s>
