Ejemplo n.º 1
0
 def deserialize(self, node, cstruct):
     if cstruct is c.null:
         return cstruct
     user = Login.query.filter_by(login_name=login_name(cstruct)).first()
     if user is None:
         raise c.Invalid(
             node,
             _('"${val}" is not a valid username',
               mapping=dict(val=cstruct)))
     return user
Ejemplo n.º 2
0
def select_list(userid, form):
    # Find the unique violation types and the number of reporters. This will be
    # joined against the Report model to get the violations/reporters for each
    # selected report.
    subq = (ReportComment.dbsession.query(
        ReportComment.reportid, sa.func.count(),
        sa.type_coerce(sa.func.array_agg(ReportComment.violation.distinct()),
                       ARRAY(sa.Integer,
                             as_tuple=True)).label('violations')).filter(
                                 ReportComment.violation != 0).group_by(
                                     ReportComment.reportid).subquery())

    # Find reports, joining against the aforementioned subquery, and eager-load
    # the reports' owners.
    q = (Report.dbsession.query(
        Report, subq).options(joinedload(Report.owner)).join(
            subq, Report.reportid == subq.c.reportid).reset_joinpoint())

    # For each type of report, eagerly load the content reported and the
    # content's owner. Also, keep track of the Login model aliases used for each
    # report type so they can be filtered against later.
    login_aliases = []
    for column_name in _report_types:
        login_alias = aliased(Login)
        login_aliases.append(login_alias)
        q = (q.outerjoin(getattr(
            Report, column_name)).outerjoin(login_alias).options(
                contains_eager(column_name + '.owner',
                               alias=login_alias)).reset_joinpoint())

    # Filter by report status. form.status can also be 'all', in which case no
    # filter is applied.
    if form.status == 'closed':
        q = q.filter_by(is_closed=True)
    elif form.status == 'open':
        q = q.filter_by(is_closed=False)

    # If filtering by the report's content's owner, iterate over the previously
    # collected Login model aliases to compare against Login.login_name.
    if form.submitter:
        submitter = legacy.login_name(form.submitter)
        q = q.filter(sa.or_(l.login_name == submitter for l in login_aliases))

    # If filtering by violation type, see if the violation is in the array
    # aggregate of unique violations for this report.
    if form.violation and form.violation != '-1':
        q = q.filter(
            sa.literal(int(form.violation)) == sa.func.any(subq.c.violations))

    q = q.order_by(Report.opened_at.desc())
    return [(report, report_count, map(_convert_violation, violations))
            for report, _, report_count, violations in q.all()]
Ejemplo n.º 3
0
def create_user(full_name="",
                birthday=arrow.get(586162800),
                config=None,
                username=None,
                password=None,
                email_addr=None,
                user_id=None):
    """ Creates a new user and profile, and returns the user ID. """
    if username is None:
        username = "******" + str(next(_user_index))

    while True:
        user = add_entity(
            users.Login(login_name=legacy.login_name(username),
                        last_login=arrow.get(0)))

        if user.userid not in staff.MODS and user.userid not in staff.DEVELOPERS:
            break

        db = d.connect()
        db.delete(user)
        db.flush()

    add_entity(
        users.Profile(userid=user.userid,
                      username=username,
                      full_name=full_name,
                      unixtime=arrow.get(0),
                      config=config))
    add_entity(users.UserInfo(userid=user.userid, birthday=birthday))
    # Set a password for this user
    if password is not None:
        d.engine.execute(
            "INSERT INTO authbcrypt VALUES (%(id)s, %(bcrypthash)s)",
            id=user.userid,
            bcrypthash=login.passhash(password))
    # Set an email address for this user
    if email_addr is not None:
        d.engine.execute(
            "UPDATE login SET email = %(email)s WHERE userid = %(id)s",
            email=email_addr,
            id=user.userid)
    # Force the userID to a user-defined value and return it
    if user_id is not None:
        d.engine.execute(
            "UPDATE login SET userid = %(newid)s WHERE userid = %(oldid)s",
            newid=user_id,
            oldid=user.userid)
        return user_id
    return user.userid
Ejemplo n.º 4
0
def create_user(full_name="",
                birthday=arrow.get(586162800),
                config=None,
                username=None):
    """ Creates a new user and profile, and returns the user ID. """
    if username is None:
        username = "******" + str(next(_user_index))
    user = add_entity(
        users.Login(login_name=legacy.login_name(username),
                    last_login=arrow.get(0)))
    add_entity(
        users.Profile(userid=user.userid,
                      username=username,
                      full_name=full_name,
                      unixtime=arrow.get(0),
                      config=config))
    add_entity(users.UserInfo(userid=user.userid, birthday=birthday))
    return user.userid