<commit_before>"""Export users and their proposal rates.

This is registered as console script 'export_mercator_users' in setup.py.

"""

import csv
import inspect
import optparse
import os
import sys
import textwrap
import time
from pyramid.paster import bootstrap
from substanced.util import find_catalog

from adhocracy_core.catalog.adhocracy import index_rates
from adhocracy_core.resources.principal import IUser
from adhocracy_core.sheets.metadata import IMetadata
from adhocracy_core.sheets.principal import IUserBasic
from adhocracy_core.sheets.principal import IUserExtended
from adhocracy_core.sheets.rate import IRateable
from adhocracy_core.sheets.rate import IRate
from adhocracy_core.utils import get_sheet_field
from adhocracy_mercator.resources.mercator import IMercatorProposalVersion
from adhocracy_mercator.sheets.mercator import ITitle
from adhocracy_core.sheets.tags import filter_by_tag


def _get_most_rated_proposals(root, min_rate=100):
    catalog = find_catalog(root, 'system')
    adhocracy_catalog = find_catalog(root, 'adhocracy')
    path = catalog['path']
    interfaces = catalog['interfaces']
    tag = adhocracy_catalog['tag']

    query = path.eq('/mercator') \
        & interfaces.eq(IMercatorProposalVersion) \
        & tag.eq('LAST')

    proposals = query.execute()
    proposals = [p for p in proposals if index_rates(p, None) > min_rate]
    proposals = list(reversed(sorted(proposals,
                                     key=lambda p: index_rates(p, None))))
    return proposals


def _get_proposals_titles(proposals):
    return [get_sheet_field(p, ITitle, 'title') for p in proposals]


def _get_rate_subject_name(rate):
    rater = get_sheet_field(rate, IRate, 'subject')
    return get_sheet_field(rater, IUserBasic, 'name')


def _get_user_rate(user_name, proposal):
    rates = get_sheet_field(proposal, IRateable, 'rates')
    last_rates = filter_by_tag(rates, 'LAST')
    rated = [rate for rate in last_rates
             if _get_rate_subject_name(rate) == user_name]

    if len(rated) == 1:
        rate = rated[0]
        creation_date = get_sheet_field(rate, IMetadata, 'item_creation_date')
        return (get_sheet_field(rate, IRate, 'rate'),
                creation_date.strftime('%Y-%m-%d_%H:%M:%S'))
    return (0, '')


def export_users():
    """Export all users and their proposal rates to csv file.

    usage::

        bin/export_mercator_users etc/development.ini  10
    """
    usage = 'usage: %prog config_file'
    parser = optparse.OptionParser(
        usage=usage,
        description=textwrap.dedent(inspect.getdoc(export_users))
    )
    options, args = parser.parse_args(sys.argv[1:])
    if not len(args) >= 1:
        print('You must provide at least one argument')
        return 2

    env = bootstrap(args[0])

    root = (env['root'])
    catalog = find_catalog(root, 'system')
    path = catalog['path']
    interfaces = catalog['interfaces']

    query = path.eq('/principals/users/') \
        & interfaces.eq(IUser)

    users = query.execute()

    # FIXME: set 100 instead of 0 on prod
    proposals = _get_most_rated_proposals(root, 100)
    proposals_titles = _get_proposals_titles(proposals)
<commit_msg>add argument min_rate to export users script; use argpars (optparse is depricated)<commit_after>"""Export users and their proposal rates.

This is registered as console script 'export_mercator_users' in setup.py.

"""

import argparse
import csv
import inspect
import os
import time
from pyramid.paster import bootstrap
from substanced.util import find_catalog

from adhocracy_core.catalog.adhocracy import index_rates
from adhocracy_core.resources.principal import IUser
from adhocracy_core.sheets.metadata import IMetadata
from adhocracy_core.sheets.principal import IUserBasic
from adhocracy_core.sheets.principal import IUserExtended
from adhocracy_core.sheets.rate import IRateable
from adhocracy_core.sheets.rate import IRate
from adhocracy_core.utils import get_sheet_field
from adhocracy_mercator.resources.mercator import IMercatorProposalVersion
from adhocracy_mercator.sheets.mercator import ITitle
from adhocracy_core.sheets.tags import filter_by_tag


def _get_most_rated_proposals(root, min_rate=0):
    catalog = find_catalog(root, 'system')
    adhocracy_catalog = find_catalog(root, 'adhocracy')
    path = catalog['path']
    interfaces = catalog['interfaces']
    tag = adhocracy_catalog['tag']

    query = path.eq('/mercator') \
        & interfaces.eq(IMercatorProposalVersion) \
        & tag.eq('LAST')

    proposals = query.execute()
    proposals = [p for p in proposals if index_rates(p, None) > min_rate]
    proposals = list(reversed(sorted(proposals,
                                     key=lambda p: index_rates(p, None))))
    return proposals


def _get_proposals_titles(proposals):
    return [get_sheet_field(p, ITitle, 'title') for p in proposals]


def _get_rate_subject_name(rate):
    rater = get_sheet_field(rate, IRate, 'subject')
    return get_sheet_field(rater, IUserBasic, 'name')


def _get_user_rate(user_name, proposal):
    rates = get_sheet_field(proposal, IRateable, 'rates')
    last_rates = filter_by_tag(rates, 'LAST')
    rated = [rate for rate in last_rates
             if _get_rate_subject_name(rate) == user_name]

    if len(rated) == 1:
        rate = rated[0]
        creation_date = get_sheet_field(rate, IMetadata, 'item_creation_date')
        return (get_sheet_field(rate, IRate, 'rate'),
                creation_date.strftime('%Y-%m-%d_%H:%M:%S'))
    return (0, '')


def export_users():
    """Export all users and their proposal rates to csv file.

    usage::

        bin/export_mercator_users etc/development.ini  10
    """
    docstring = inspect.getdoc(export_users)
    parser = argparse.ArgumentParser(description=docstring)
    parser.add_argument('ini_file',
                        help='path to the adhocracy backendini file')
    parser.add_argument('min_rate',
                        type=int,
                        help='minimal rate to restrict listed proposals')
    args = parser.parse_args()
    env = bootstrap(args.ini_file)
    root = env['root']
    catalog = find_catalog(root, 'system')
    path = catalog['path']
    interfaces = catalog['interfaces']

    query = path.eq('/principals/users/') \
        & interfaces.eq(IUser)

    users = query.execute()

    proposals = _get_most_rated_proposals(root, min_rate=args.min_rate)
    proposals_titles = _get_proposals_titles(proposals)

{'fraction_non_alphanumeric': 0.09597904791249422, 'fraction_numerical': 0.0038514866738561084, 'mean_word_length': 3.801775147928994, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1463123', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2038, 'n_words': 484}