def test_python_datetime_pretty_print(self): # datetime corresponding to Gregorian ordinal d = date.fromordinal(736132) # Verify, that if 'lang' is 'de' format of date is 'day. month.' self.assertEqual(lib.pretty_print_timestamp(ts=d, lang='de'), '17. Jun.') # Verify, that if 'lang' is not 'de' format of date is 'month. day. ' self.assertEqual(lib.pretty_print_timestamp(ts=d, lang='en'), 'Jun. 17.') self.assertEqual(lib.pretty_print_timestamp(ts='2016-01-01', lang=''), 'Jan. 01.')
def __get_dict_for_charts(db_issue: Issue, arg_stat_mapper: dict) -> dict: """ :param db_issue: :return: """ days_since_start = min((arrow.utcnow() - db_issue.date).days, 14) label, data = [], [] today = date.today() db_arguments = DBDiscussionSession.query(Argument).filter_by( issue_uid=db_issue.uid).all() arguments_uids = [db_arg.uid for db_arg in db_arguments] statement_uids = [] for arg_uid in arguments_uids: statement_uids += arg_stat_mapper[arg_uid] db_clicked_arguments = DBDiscussionSession.query(ClickedArgument).filter( ClickedArgument.uid.in_(arguments_uids)) db_clicked_statements = DBDiscussionSession.query(ClickedStatement).filter( ClickedStatement.uid.in_(statement_uids)) for days_diff in range(days_since_start, -1, -1): date_begin = today - timedelta(days=days_diff) date_end = today - timedelta(days=days_diff - 1) label.append(pretty_print_timestamp(date_begin, db_issue.lang)) clicked_arguments = db_clicked_arguments.filter( ClickedArgument.timestamp >= arrow.get(date_begin), ClickedArgument.timestamp < arrow.get(date_end)).count() clicked_statements = db_clicked_statements.filter( ClickedStatement.timestamp >= arrow.get(date_begin), ClickedStatement.timestamp < arrow.get(date_end)).count() data.append(clicked_arguments + clicked_statements) return {'data': data, 'label': label}
def __special_public_data(rdict, lang): rdict['labels1'] = [ pretty_print_timestamp(date.today() - timedelta(days=dd), lang) for dd in range(7, -1, -1) ] rdict['labels2'] = [ pretty_print_timestamp(date.today() - timedelta(days=dd), lang) for dd in range(30, -1, -1) ] rdict['labels3'] = rdict['labels2'] rdict['labels4'] = rdict['labels2'] rdict['data1'] = [9000.1] * 7 rdict['data2'] = [9000.1] * 30 rdict['data3'] = rdict['data2'] rdict['data4'] = rdict['data2'] return rdict
def _special_public_data(rdict: Dict, lang: str) -> Dict[str, List]: """ Return mock public data for anonymous User. :param rdict: The dictionary which shall be filled. :param lang: The language in which the dictionary is provided. :return: Mock public statistics in a dictionary. """ rdict['labels1'] = [ pretty_print_timestamp(date.today() - timedelta(days=dd), lang) for dd in range(7, -1, -1) ] rdict['labels2'] = [ pretty_print_timestamp(date.today() - timedelta(days=dd), lang) for dd in range(30, -1, -1) ] rdict['labels3'] = rdict['labels2'] rdict['labels4'] = rdict['labels2'] rdict['data1'] = [9000.1] * 7 rdict['data2'] = [9000.1] * 30 rdict['data3'] = rdict['data2'] rdict['data4'] = rdict['data2'] return rdict
def _labels_for_historical_data(days: int, lang: str) -> List[str]: """ Return a list of dates that can be used as labels for public data points regarding a user. :param days: Labels for last x days. :param lang: The language of the labels. :return: A list containing the labels for every of the last `days`. """ labels = [] for days_diff in range(days, -1, -1): date_begin = date.today() - timedelta(days=days_diff) timestamp = pretty_print_timestamp(date_begin, lang) labels.append(timestamp) return labels
def get_public_data(user_id: int, lang: str): """ Fetch some public information about the user with given nickname :param user_id: User.uid :param lang: :return: dict() """ LOG.debug("User: %s", user_id) return_dict = dict() db_user = DBDiscussionSession.query(User).get(user_id) if db_user is None and user_id != 1: return return_dict _tn = Translator(lang) # data for last 7 and 30 days labels_decision_7 = [] labels_decision_30 = [] labels_edit_30 = [] labels_statement_30 = [] data_decision_7 = [] data_decision_30 = [] data_edit_30 = [] data_statement_30 = [] return_dict['label1'] = _tn.get(_.decisionIndex7) return_dict['label2'] = _tn.get(_.decisionIndex30) return_dict['label3'] = _tn.get(_.statementIndex) return_dict['label4'] = _tn.get(_.editIndex) if user_id == 1: return __special_public_data(return_dict, lang) for days_diff in range(30, -1, -1): date_begin = date.today() - timedelta(days=days_diff) date_end = date.today() - timedelta(days=days_diff - 1) begin = arrow.get(date_begin.strftime('%Y-%m-%d'), 'YYYY-MM-DD') end = arrow.get(date_end.strftime('%Y-%m-%d'), 'YYYY-MM-DD') ts = pretty_print_timestamp(date_begin, lang) labels_decision_30.append(ts) labels_statement_30.append(ts) labels_edit_30.append(ts) db_clicks_statements = DBDiscussionSession.query( ClickedStatement).filter( ClickedStatement.author_uid == db_user.uid, ClickedStatement.timestamp >= begin, ClickedStatement.timestamp < end).all() db_clicks_arguments = DBDiscussionSession.query( ClickedArgument).filter(ClickedArgument.author_uid == db_user.uid, ClickedArgument.timestamp >= begin, ClickedArgument.timestamp < end).all() clicks = len(db_clicks_statements) + len(db_clicks_arguments) data_decision_30.append(clicks) if days_diff < 6: labels_decision_7.append(ts) data_decision_7.append(clicks) get_tv_dict = get_textversions(db_user, lang, begin, end) data_statement_30.append(len(get_tv_dict.get('statements', []))) data_edit_30.append(len(get_tv_dict.get('edits', []))) return_dict['labels1'] = labels_decision_7 return_dict['labels2'] = labels_decision_30 return_dict['labels3'] = labels_statement_30 return_dict['labels4'] = labels_edit_30 return_dict['data1'] = data_decision_7 return_dict['data2'] = data_decision_30 return_dict['data3'] = data_statement_30 return_dict['data4'] = data_edit_30 return return_dict