Beispiel #1
0
def get_history_of(db_user: User, translator: Translator):
    """
    Returns the reputation history of an user

    :param db_user: User
    :param translator: Translator
    :return: dict()
    """

    db_reputation = DBDiscussionSession.query(ReputationHistory) \
        .filter_by(reputator_uid=db_user.uid) \
        .join(ReputationReason, ReputationReason.uid == ReputationHistory.reputation_uid) \
        .order_by(ReputationHistory.uid.asc()) \
        .all()

    rep_list = []
    for rep in db_reputation:
        date = sql_timestamp_pretty_print(rep.timestamp,
                                          translator.get_lang(),
                                          humanize=False)
        points_data = ('+' if rep.reputations.points > 0 else '') + str(
            rep.reputations.points)
        rep_list.append({
            'date': date,
            'points_data': points_data,
            'action': translator.get(rep.reputations.reason),
            'points': rep.reputations.points
        })

    count, all_rights = get_reputation_of(db_user)
    return {
        'count': count,
        'all_rights': all_rights,
        'history': list(reversed(rep_list))
    }
Beispiel #2
0
def get_issue_dict_for(db_issue: Issue, uid: int, lang: str) -> dict:
    """
    Creates an dictionary for the issue

    :param db_issue: Issue
    :param uid: current selected Issue.uid
    :param lang: ui_locales
    :return: dict()
    """
    _um = UrlManager(db_issue.slug)
    issue_dict = {
        'uid': str(db_issue.uid),
        'slug': db_issue.slug,
        'title': db_issue.title,
        'url': '/' + db_issue.slug,
        'review_url':
        _um.get_review_url() if str(uid) != str(db_issue.uid) else '',
        'info': db_issue.info,
        'stat_count': get_number_of_statements(db_issue.uid),
        'date': sql_timestamp_pretty_print(db_issue.date, lang),
        'author': db_issue.author.public_nickname,
        'error': '',
        'author_url': '/user/{}'.format(db_issue.author.uid),
        'enabled': 'disabled' if str(uid) == str(db_issue.uid) else 'enabled'
    }
    return issue_dict
Beispiel #3
0
def get_latest_news(ui_locales):
    """
    Returns the latest news for the carousel

    :param ui_locales:
    :return: dict()
    :return:
    """
    logger('NewsHelper', 'main')
    db_news = DBDiscussionSession.query(News).order_by(News.date.desc()).all()
    ret_news = []
    for index, news in enumerate(db_news[:5]):
        news_dict = {
            'indicatorclass': '',
            'blockclass': 'carousel-item',
            'title': news.title,
            'author': news.author,
            'date': sql_timestamp_pretty_print(news.date, ui_locales, False),
            'news': news.news,
            'id': index
        }
        ret_news.append(news_dict)
    ret_news[0]['indicatorclass'] = 'active'
    ret_news[0]['blockclass'] += ' active'
    return ret_news
Beispiel #4
0
def set_news(title: str, text: str, db_user: User, lang: str,
             main_page: str) -> dict():
    """
    Sets a new news into the news table

    :param title: of the news
    :param text: of the news
    :param db_user: author of the news
    :param lang: ui_locales
    :param main_page: url
    :return:
    """
    logger('NewsHelper', 'def')

    author = db_user.firstname
    if db_user.firstname != 'admin':
        author += ' {}'.format(db_user.surname)

    date = arrow.now()
    DBDiscussionSession.add(
        News(title=title, author=author, date=arrow.now(), news=text))
    DBDiscussionSession.flush()
    transaction.commit()

    return_dict = {
        'status': 'success',
        'title': title,
        'date': sql_timestamp_pretty_print(date, lang, False),
        'author': author,
        'news': text
    }

    create_news_rss(main_page, lang)

    return return_dict
Beispiel #5
0
def get_clicked_elements_of(db_user: User, is_argument: bool, lang: str) -> list:
    """
    Returns array with all clicked elements by the user. Each element is a dict with information like the uid,
    timestamp, up_Vote, validity, the clicked uid and content of the clicked element.

    :param db_user: User
    :param is_argument: Boolean
    :param lang: ui_locales
    :return: [{},...]
    """
    return_array = []
    db_type = ClickedArgument if is_argument else ClickedStatement
    db_clicks = DBDiscussionSession.query(db_type).filter_by(author_uid=db_user.uid).all()

    for click in db_clicks:
        click_dict = dict()
        click_dict['uid'] = click.uid
        click_dict['timestamp'] = sql_timestamp_pretty_print(click.timestamp, lang)
        click_dict['is_up_vote'] = click.is_up_vote
        click_dict['is_valid'] = click.is_valid
        if is_argument:
            click_dict['argument_uid'] = click.argument_uid
            click_dict['content'] = get_text_for_argument_uid(click.argument_uid, lang)
        else:
            click_dict['statement_uid'] = click.statement_uid
            click_dict['content'] = get_text_for_statement_uid(click.statement_uid)
        return_array.append(click_dict)

    return return_array
Beispiel #6
0
def get_marked_elements_of(db_user: User, is_argument: bool, lang: str):
    """
    Get all marked arguments/statements of the user

    :param db_user: User
    :param is_argument: Boolean
    :param lang: uid_locales
    :return: [{},...]
    """
    return_array = []

    if is_argument:
        db_votes = DBDiscussionSession.query(MarkedArgument).filter_by(author_uid=db_user.uid).all()
    else:
        db_votes = DBDiscussionSession.query(MarkedStatement).filter_by(author_uid=db_user.uid).all()

    for vote in db_votes:
        vote_dict = dict()
        vote_dict['uid'] = str(vote.uid)
        vote_dict['timestamp'] = sql_timestamp_pretty_print(vote.timestamp, lang)
        if is_argument:
            vote_dict['argument_uid'] = str(vote.argument_uid)
            vote_dict['content'] = get_text_for_argument_uid(vote.argument_uid, lang)
        else:
            vote_dict['statement_uid'] = str(vote.statement_uid)
            vote_dict['content'] = get_text_for_statement_uid(vote.statement_uid)
        return_array.append(vote_dict)

    return return_array
Beispiel #7
0
def get_information_of(db_user: User, lang):
    """
    Returns public information of the given user

    :param db_user: User
    :param lang: ui_locales
    :return: dict()
    """
    if db_user.nickname == nick_of_anonymous_user:
        return __get_special_infos(lang)
    db_group = DBDiscussionSession.query(Group).get(db_user.group_uid)
    ret_dict = dict()
    ret_dict['public_nick'] = db_user.global_nickname
    ret_dict['last_action'] = sql_timestamp_pretty_print(
        db_user.last_action, lang)
    ret_dict['last_login'] = sql_timestamp_pretty_print(
        db_user.last_login, lang)
    ret_dict['registered'] = sql_timestamp_pretty_print(
        db_user.registered, lang)
    ret_dict['group'] = start_with_capital(db_group.name)

    ret_dict['is_male'] = db_user.gender == 'm'
    ret_dict['is_female'] = db_user.gender == 'f'
    ret_dict['is_neutral'] = db_user.gender != 'm' and db_user.gender != 'f'

    arg_votes, stat_votes = get_mark_count_of(db_user, False)
    db_reviews_duplicate = DBDiscussionSession.query(
        ReviewDuplicate).filter_by(detector_uid=db_user.uid).all()
    db_reviews_edit = DBDiscussionSession.query(ReviewEdit).filter_by(
        detector_uid=db_user.uid).all()
    db_reviews_delete = DBDiscussionSession.query(ReviewDelete).filter_by(
        detector_uid=db_user.uid).all()
    db_reviews_optimization = DBDiscussionSession.query(
        ReviewOptimization).filter_by(detector_uid=db_user.uid).all()
    db_reviews = db_reviews_duplicate + db_reviews_edit + db_reviews_delete + db_reviews_optimization

    get_tv_dict = get_textversions(db_user, lang)
    ret_dict['statements_posted'] = len(get_tv_dict.get('statements', []))
    ret_dict['edits_done'] = len(get_tv_dict.get('edits', []))
    ret_dict['reviews_proposed'] = len(db_reviews)
    ret_dict['discussion_arg_votes'] = arg_votes
    ret_dict['discussion_stat_votes'] = stat_votes
    ret_dict['avatar_url'] = get_profile_picture(db_user, 120)
    ret_dict['discussion_stat_rep'], trash = get_reputation_of(db_user)

    return ret_dict
Beispiel #8
0
def _get_special_infos(lang: str) -> Dict[str, Any]:
    return {
        'public_nick': 'Son Goku',
        'last_action': sql_timestamp_pretty_print(get_now(), lang),
        'last_login': sql_timestamp_pretty_print(get_now(), lang),
        'registered': sql_timestamp_pretty_print(get_now(), lang),
        'group': 'Saiyajin',
        'is_male': True,
        'is_female': False,
        'is_neutral': False,
        'statements_posted': '>9000',
        'edits_done': '>9000',
        'reviews_proposed': '>9000',
        'discussion_arg_votes': '>9000',
        'discussion_stat_votes': '>9000',
        'avatar_url': '/static/images/goku.jpg',
        'discussion_stat_rep': '>9000',
    }
Beispiel #9
0
def get_information_of(user: User, lang: str) -> Dict[str, Any]:
    """
    Returns public information of the given user.

    :param user: User whose information is queried.
    :param lang: Language-string representing language of the answer.
    :return: A dictionary containing the public information.
    """
    if user.nickname == nick_of_anonymous_user:
        return _get_special_infos(lang)
    ret_dict = dict()
    ret_dict['public_nick'] = user.global_nickname
    ret_dict['last_action'] = sql_timestamp_pretty_print(
        user.last_action, lang)
    ret_dict['last_login'] = sql_timestamp_pretty_print(user.last_login, lang)
    ret_dict['registered'] = sql_timestamp_pretty_print(user.registered, lang)
    ret_dict['group'] = start_with_capital(user.group.name)

    ret_dict['is_male'] = user.gender == 'm'
    ret_dict['is_female'] = user.gender == 'f'
    ret_dict['is_neutral'] = user.gender != 'm' and user.gender != 'f'

    arg_votes, stat_votes = get_mark_count_of(user, False)
    db_reviews_duplicate = DBDiscussionSession.query(
        ReviewDuplicate).filter_by(detector_uid=user.uid).all()
    db_reviews_edit = DBDiscussionSession.query(ReviewEdit).filter_by(
        detector_uid=user.uid).all()
    db_reviews_delete = DBDiscussionSession.query(ReviewDelete).filter_by(
        detector_uid=user.uid).all()
    db_reviews_optimization = DBDiscussionSession.query(
        ReviewOptimization).filter_by(detector_uid=user.uid).all()
    db_reviews = db_reviews_duplicate + db_reviews_edit + db_reviews_delete + db_reviews_optimization

    get_tv_dict = get_textversions(user, lang)
    ret_dict['statements_posted'] = len(get_tv_dict.get('statements', []))
    ret_dict['edits_done'] = len(get_tv_dict.get('edits', []))
    ret_dict['reviews_proposed'] = len(db_reviews)
    ret_dict['discussion_arg_votes'] = arg_votes
    ret_dict['discussion_stat_votes'] = stat_votes
    ret_dict['avatar_url'] = get_profile_picture(user, 120)
    ret_dict['discussion_stat_rep'], _ = get_reputation_of(user)

    return ret_dict
def get_box_for(db_user, lang, main_page, is_inbox):
    """
    Returns all notifications for the user

    :param db_user: User
    :param lang: ui_locales
    :param main_page: URL
    :param is_inbox: Boolean
    :return: [Notification]
    """
    if is_inbox:
        db_messages = DBDiscussionSession.query(Message).filter(
            Message.to_author_uid == db_user.uid,
            Message.is_inbox == is_inbox).order_by(Message.uid.desc()).all()
    else:
        db_messages = DBDiscussionSession.query(Message).filter(
            Message.from_author_uid == db_user.uid,
            Message.is_inbox == is_inbox).order_by(Message.uid.desc()).all()

    message_array = []
    for message in db_messages:
        tmp_dict = dict()
        if is_inbox:
            db_from_user = DBDiscussionSession.query(User).get(
                message.from_author_uid)
            tmp_dict[
                'show_from_author'] = db_from_user.global_nickname != 'admin'
            tmp_dict['from_author'] = db_from_user.global_nickname
            tmp_dict['from_author_avatar'] = get_profile_picture(db_from_user,
                                                                 size=30)
            tmp_dict['from_author_url'] = main_page + '/user/' + str(
                db_from_user.uid)
        else:
            db_to_user = DBDiscussionSession.query(User).get(
                message.to_author_uid)
            tmp_dict['to_author'] = db_to_user.global_nickname
            tmp_dict['to_author_avatar'] = get_profile_picture(db_to_user,
                                                               size=30)
            tmp_dict['to_author_url'] = main_page + '/user/' + str(
                db_to_user.uid)

        tmp_dict['id'] = str(message.uid)
        tmp_dict['timestamp'] = sql_timestamp_pretty_print(
            message.timestamp, lang)
        tmp_dict['read'] = message.read
        tmp_dict['topic'] = message.topic
        tmp_dict['content'] = message.content
        tmp_dict['collapse_link'] = '#collapse' + str(message.uid)
        tmp_dict['collapse_id'] = 'collapse' + str(message.uid)
        message_array.append(tmp_dict)

    return message_array[::-1]
Beispiel #11
0
def prepare_json_of_issue(db_issue: Issue, db_user: User) -> dict():
    """
    Prepares slug, info, argument count and the date of the issue as dict

    :param db_issue: Issue
    :param db_user: User
    :return: Issue-dict()
    """
    slug = slugify(db_issue.title)
    title = db_issue.title
    info = db_issue.info
    long_info = db_issue.long_info
    stat_count = get_number_of_statements(db_issue.uid)
    lang = db_issue.lang
    date_pretty = sql_timestamp_pretty_print(db_issue.date, lang)
    duration = (arrow.utcnow() - db_issue.date)
    days, seconds = duration.days, duration.seconds
    duration = ceil(days * 24 + seconds / 3600)
    date_ms = int(db_issue.date.format('X')) * 1000
    date = db_issue.date.format('DD.MM.YY')
    time = db_issue.date.format('HH:mm')

    db_issues = get_visible_issues_for_user_as_query(
        db_user.uid).filter(Issue.uid != db_issue.uid).all()
    all_array = [
        get_issue_dict_for(issue, db_issue.uid, lang) for issue in db_issues
    ]

    _t = Translator(lang)
    tooltip = _t.get(_.discussionInfoTooltipSg) if stat_count == 1 else _t.get(
        _.discussionInfoTooltipPl)
    tooltip = tooltip.format(date, time, stat_count)

    return {
        'slug': slug,
        'lang': lang,
        'info': info,
        'long_info': long_info,
        'title': title,
        'uid': db_issue.uid,
        'stat_count': stat_count,
        'date': date,
        'date_ms': date_ms,
        'date_pretty': date_pretty,
        'all': all_array,
        'tooltip': tooltip,
        'intro': _t.get(_.currentDiscussion),
        'duration': duration,
        'read_only': db_issue.is_read_only
    }
Beispiel #12
0
def __get_executed_review_element_of(table_key, main_page, db_review,
                                     translator,
                                     is_executed) -> Optional[dict]:
    """

    :param table_key: Shortcut for the table
    :param main_page: Main page of D-BAS
    :param db_review: Element
    :param translator: current ui_locales
    :param is_executed
    :return: Element
    """
    queue = get_queue_by_key(table_key)
    adapter = QueueAdapter(queue=queue(),
                           application_url=main_page,
                           translator=translator)
    full_text = adapter.get_text_of_element(db_review)
    if not full_text:
        return None

    # pretty print
    intro = translator.get(_.otherUsersSaidThat) + ' '
    if full_text.startswith(intro):
        short_text = full_text[len(intro):len(intro) + 1].upper()
        short_text += full_text[len(intro) + 1:len(intro) +
                                txt_len_history_page]
    else:
        short_text = full_text[0:txt_len_history_page]

    short_text += '...' if len(full_text) > txt_len_history_page else '.'
    short_text = f'<span class="text-primary">{short_text}</span>'

    pro_list, con_list = adapter.get_all_votes_for(db_review)

    # and build up some dict
    pdict = __handle_table_of_review_element(table_key, db_review, short_text,
                                             full_text, is_executed)
    if not pdict:
        return None

    pdict['entry_id'] = db_review.uid
    pdict['timestamp'] = sql_timestamp_pretty_print(db_review.timestamp,
                                                    translator.get_lang())
    pdict['votes_pro'] = pro_list
    pdict['votes_con'] = con_list
    pdict['reporter'] = __get_user_dict_for_review(db_review.detector_uid,
                                                   main_page)

    return pdict
Beispiel #13
0
def create_users_dict(db_user, timestamp, main_page, lang):
    """
    Creates dictionary with nickname, url and timestamp

    :param db_user: User
    :param timestamp: SQL Timestamp
    :param main_page: url
    :param lang: language
    :return: dict()
    """
    tmp = db_user.global_nickname
    return {'nickname': tmp,
            'public_profile_url': main_page + '/user/' + str(db_user.uid),
            'avatar_url': get_profile_picture(db_user),
            'vote_timestamp': sql_timestamp_pretty_print(timestamp, lang)}
Beispiel #14
0
def get_from_database(db_user: User, lang: str) -> List[dict]:
    """
    Returns history from database

    :param db_user: User
    :param lang: ui_locales
    :return: [String]
    """
    db_history = DBDiscussionSession.query(History).filter_by(author_uid=db_user.uid).all()
    return_array = []
    for history in db_history:
        return_array.append({'path': history.path,
                             'timestamp': sql_timestamp_pretty_print(history.timestamp, lang, False, True) + ' GMT'})

    return return_array
Beispiel #15
0
def get_all_infos_about_argument(db_argument: Argument, main_page, db_user,
                                 lang) -> dict:
    """
    Returns bunch of information about the given argument

    :param db_argument: The argument
    :param main_page: url of the application
    :param db_user: User
    :param lang: Language
    :rtype: dict
    :return: dictionary with many information or an error
    """
    _t = Translator(lang.ui_locales)

    return_dict = dict()
    db_votes = DBDiscussionSession.query(ClickedArgument).filter(
        ClickedArgument.argument_uid == db_argument.uid,
        ClickedArgument.is_valid == True,
        ClickedStatement.is_up_vote == True).all()

    db_author = DBDiscussionSession.query(User).get(db_argument.author_uid)
    return_dict['vote_count'] = str(len(db_votes))
    return_dict['author'] = db_author.global_nickname
    return_dict['author_url'] = main_page + '/user/' + str(db_author.uid)
    return_dict['gravatar'] = get_profile_picture(db_author)
    return_dict['timestamp'] = sql_timestamp_pretty_print(
        db_argument.timestamp, db_argument.lang)
    text = get_text_for_argument_uid(db_argument.uid)
    return_dict['text'] = start_with_capital(text)

    supporters = []
    gravatars = dict()
    public_page = dict()
    for vote in db_votes:
        db_author = DBDiscussionSession.query(User).get(vote.author_uid)
        name = db_author.global_nickname
        if db_user.nickname == db_author.nickname:
            name += ' (' + _t.get(_.itsYou) + ')'
        supporters.append(name)
        gravatars[name] = get_profile_picture(db_author)
        public_page[name] = main_page + '/user/' + str(db_author.uid)

    return_dict['supporter'] = supporters
    return_dict['gravatars'] = gravatars
    return_dict['public_page'] = public_page

    return return_dict
Beispiel #16
0
def __get_logfile_dict(textversion: TextVersion, main_page: str, lang: str) -> Dict:
    """
    Returns dictionary with information about the given textversion

    :param textversion: TextVersion
    :param main_page: String
    :param lang: Language.ui_locales
    :return: dict()
    """
    db_author = DBDiscussionSession.query(User).get(textversion.author_uid)
    corr_dict = dict()
    corr_dict['uid'] = str(textversion.uid)
    corr_dict['author'] = str(db_author.global_nickname)
    corr_dict['author_url'] = main_page + '/user/' + str(db_author.uid)
    corr_dict['author_gravatar'] = get_profile_picture(db_author, 20)
    corr_dict['date'] = sql_timestamp_pretty_print(textversion.timestamp, lang)
    corr_dict['text'] = str(textversion.content)
    return corr_dict
Beispiel #17
0
def get_reporter_stats_for_review(db_review, ui_locales, main_page):
    """
    Get statistics for the current review

    :param db_review: Review-Row
    :param ui_locales: Language.ui_locales
    :param main_page: Host URL
    :return: dict()
    """
    LOG.debug("Return statistics for review %s", db_review.uid)

    db_reporter = DBDiscussionSession.query(User).get(db_review.detector_uid)

    return {
        'reported': sql_timestamp_pretty_print(db_review.timestamp, ui_locales),
        'reporter': db_reporter.global_nickname,
        'reporter_gravatar': get_profile_picture(db_reporter, 20),
        'reporter_url': main_page + '/user/' + str(db_reporter.uid),
        'id': str(db_review.uid)
    }
Beispiel #18
0
def _clicked_argument_to_dict(clicked_argument: ClickedArgument,
                              lang: str) -> Dict[str, Any]:
    """
    Transforms a single ClickedArgument object into a dictionary representation of itself.

    :param clicked_argument: The object that shall be transformed.
    :param lang: Language that the answer shall be delivered in.
    :return: A dictionary representing `clicked_argument`.
    """

    return {
        'uid': clicked_argument.uid,
        'timestamp': sql_timestamp_pretty_print(clicked_argument.timestamp,
                                                lang),
        'is_up_vote': clicked_argument.is_up_vote,
        'is_valid': clicked_argument.is_valid,
        'argument_uid': clicked_argument.argument_uid,
        'content': get_text_for_argument_uid(clicked_argument.argument_uid,
                                             lang)
    }
Beispiel #19
0
def get_textversions(db_user: User,
                     lang: str,
                     timestamp_after=None,
                     timestamp_before=None):
    """
    Returns all textversions, were the user was author

    :param db_user: User
    :param lang: ui_locales
    :param timestamp_after: Arrow or None
    :param timestamp_before: Arrow or None
    :return: [{},...], [{},...]
    """
    statement_array = []
    edit_array = []

    if not timestamp_after:
        timestamp_after = arrow.get('1970-01-01').format('YYYY-MM-DD')
    if not timestamp_before:
        timestamp_before = arrow.utcnow().replace(days=+1).format('YYYY-MM-DD')

    db_edits = DBDiscussionSession.query(TextVersion).filter(
        TextVersion.author_uid == db_user.uid,
        TextVersion.timestamp >= timestamp_after,
        TextVersion.timestamp < timestamp_before).all()

    for edit in db_edits:
        db_root_version = DBDiscussionSession.query(TextVersion).filter_by(
            statement_uid=edit.statement_uid).first()
        edit_dict = dict()
        edit_dict['uid'] = str(edit.uid)
        edit_dict['statement_uid'] = str(edit.statement_uid)
        edit_dict['content'] = str(edit.content)
        edit_dict['timestamp'] = sql_timestamp_pretty_print(
            edit.timestamp, lang)
        if db_root_version.uid == edit.uid:
            statement_array.append(edit_dict)
        else:
            edit_array.append(edit_dict)

    return {'statements': statement_array, 'edits': edit_array}
Beispiel #20
0
def get_textversions(user: User, lang: str, timestamp_after: Arrow = None, timestamp_before: Arrow = None) -> \
        Dict[str, List[Dict[str, str]]]:
    """
    Returns all textversions, were the user is the author.

    :param user: The user who is author of desired textversion objects.
    :param lang: The language in which the results shall be delivered.
    :param timestamp_after: Set a date if results should only include objects created after the date.
    :param timestamp_before: Set a date if results should only include objects created before the date.
    :return: Return a dict with all statements and edits that use a textversion where the user is author.
    """
    statements = []
    edits = []

    if not timestamp_after:
        timestamp_after = arrow.get('1970-01-01').format('YYYY-MM-DD')
    if not timestamp_before:
        timestamp_before = arrow.utcnow().replace(days=+1).format('YYYY-MM-DD')

    textversions = DBDiscussionSession.query(TextVersion).filter(
        TextVersion.author_uid == user.uid,
        TextVersion.timestamp >= timestamp_after,
        TextVersion.timestamp < timestamp_before).all()

    for textversion in textversions:
        db_root_version = DBDiscussionSession.query(TextVersion).filter_by(
            statement_uid=textversion.statement_uid).first()
        edit_dict = dict()
        edit_dict['uid'] = str(textversion.uid)
        edit_dict['statement_uid'] = str(textversion.statement_uid)
        edit_dict['content'] = str(textversion.content)
        edit_dict['timestamp'] = sql_timestamp_pretty_print(
            textversion.timestamp, lang)
        if db_root_version.uid == textversion.uid:
            statements.append(edit_dict)
        else:
            edits.append(edit_dict)

    return {'statements': statements, 'edits': edits}
Beispiel #21
0
def __get_stats_for_review(review, ui_locales, main_page):
    """
    Get statistics for the current review

    :param review: Review-Row
    :param ui_locales: Language.ui_locales
    :param main_page: Host URL
    :return: dict()
    """
    logger('ReviewSubpagerHelper', 'main')

    db_reporter = DBDiscussionSession.query(User).get(review.detector_uid)

    stats = dict()
    stats['reported'] = sql_timestamp_pretty_print(review.timestamp,
                                                   ui_locales)
    stats['reporter'] = db_reporter.global_nickname
    stats['reporter_gravatar'] = get_profile_picture(db_reporter, 20)
    stats['reporter_url'] = main_page + '/user/' + str(db_reporter.uid)
    stats['id'] = str(review.uid)

    return stats
Beispiel #22
0
def get_reputation_history_of(nickname, translator):
    """
    Returns the reputation history of an user

    :param nickname: User.nickname
    :param translator: Translator
    :return: dict()
    """
    db_user = DBDiscussionSession.query(User).filter_by(nickname=nickname).first()
    if not db_user:
        return dict()

    ret_dict = dict()
    count, all_rights = get_reputation_of(nickname)
    ret_dict['count'] = count
    ret_dict['all_rights'] = all_rights

    db_reputation = DBDiscussionSession.query(ReputationHistory) \
        .filter_by(reputator_uid=db_user.uid) \
        .join(ReputationReason, ReputationReason.uid == ReputationHistory.reputation_uid) \
        .order_by(ReputationHistory.uid.asc())\
        .all()

    rep_list = list()
    for rep in db_reputation:
        date = sql_timestamp_pretty_print(rep.timestamp, translator.get_lang(), humanize=False)
        points_data = ('+' if rep.reputations.points > 0 else '') + str(rep.reputations.points)
        points = rep.reputations.points
        action = translator.get(rep.reputations.reason)
        rep_list.append({'date': date,
                         'points_data': points_data,
                         'action': action,
                         'points': points})

    ret_dict['history'] = list(reversed(rep_list))

    return ret_dict
Beispiel #23
0
def get_news(ui_locales):
    """
    Returns all news in an array, sorted by date

    :param ui_locales:
    :return: dict()
    """
    logger('NewsHelper', 'main')
    db_news = DBDiscussionSession.query(News).order_by(News.date.desc()).all()
    ret_news = []
    for news in db_news:
        news_dict = {
            'title': news.title,
            'author': news.author,
            'date': sql_timestamp_pretty_print(news.date, ui_locales, False),
            'news': news.news,
            'title_id': 'news_{}_title'.format(news.uid),
            'date_id': 'news_{}_date'.format(news.uid),
            'author_id': 'news_{}_author'.format(news.uid),
            'uid': 'news_' + str(news.uid)
        }
        ret_news.append(news_dict)

    return ret_news
def send_users_notification(author, recipient, title, text,
                            ui_locales) -> dict:
    """
    Send a notification from user a to user b

    :param recipient: User
    :param title: Title of the notification
    :param text: Text of the notification
    :param author: User
    :param ui_locales: Current used language
    :rtype: dict
    :return: prepared collection with status information
    """
    db_notification = send_notification(author, recipient, title, text,
                                        author.nickname)
    prepared_dict = {
        'timestamp':
        sql_timestamp_pretty_print(db_notification.timestamp, ui_locales),
        'uid':
        db_notification.uid,
        'recipient_avatar':
        get_profile_picture(recipient, 20)
    }
    return prepared_dict
Beispiel #25
0
def __get_executed_review_element_of(table, main_page, review, last_review_type, translator, is_executed):
    """

    :param table: Shortcut for the table
    :param main_page: Main page of D-BAS
    :param review: Element
    :param last_review_type: Type of the last reviewer of the table
    :param translator: current ui_locales
    :param is_executed
    :return: Element
    """

    length = 35
    # getting text
    if table == 'duplicates':
        full_text = get_text_for_statement_uid(review.duplicate_statement_uid)
    elif table in ['splits', 'merges']:
        full_text = get_text_for_premisegroup_uid(review.premisegroup_uid)
    elif review.statement_uid is None:
        full_text = get_text_for_argument_uid(review.argument_uid)
    else:
        full_text = get_text_for_statement_uid(review.statement_uid)

    # pretty print
    intro = translator.get(_.otherUsersSaidThat) + ' '
    if full_text.startswith(intro):
        short_text = full_text[len(intro):len(intro) + 1].upper() + full_text[len(intro) + 1:len(intro) + length]
    else:
        short_text = full_text[0:length]

    short_text += '...' if len(full_text) > length else '.'
    short_text = '<span class="text-primary">' + short_text + '</span>'

    all_votes = DBDiscussionSession.query(last_review_type).filter_by(review_uid=review.uid)
    is_okay = False if table == 'optimizations' else True
    if table is 'merges':
        pro_votes = all_votes.filter_by(should_merge=is_okay).all()
        con_votes = all_votes.filter(last_review_type.should_merge != is_okay).all()
    elif table is 'splits':
        pro_votes = all_votes.filter_by(should_split=is_okay).all()
        con_votes = all_votes.filter(last_review_type.should_split != is_okay).all()
    else:
        pro_votes = all_votes.filter_by(is_okay=is_okay).all()
        con_votes = all_votes.filter(last_review_type.is_okay != is_okay).all()

    # getting the users which have voted
    pro_list = [__get_user_dict_for_review(pro.reviewer_uid, main_page) for pro in pro_votes]
    con_list = [__get_user_dict_for_review(con.reviewer_uid, main_page) for con in con_votes]

    if table == 'duplicates':
        # switch it, because contra is: it should not be there!
        tmp_list = pro_list
        pro_list = con_list
        con_list = tmp_list

    # and build up some dict
    entry = dict()
    entry['entry_id'] = review.uid
    tmp = __handle_table_of_review_element(table, entry, review, short_text, full_text, length, is_executed)
    if not tmp:
        entry = None
    else:
        entry.update(tmp)
        entry['pro'] = pro_list
        entry['con'] = con_list
        entry['timestamp'] = sql_timestamp_pretty_print(review.timestamp, translator.get_lang())
        entry['votes_pro'] = pro_list
        entry['votes_con'] = con_list
        entry['reporter'] = __get_user_dict_for_review(review.detector_uid, main_page)

    return entry