Exemplo n.º 1
0
def accept_review():
    """A user is accepting to do a review (notice that the GET is signed).
    Picks a task and adds it to the set of tasks for the user."""
    # Checks the permissions.
    c = db.venue(request.args(0)) or redirect('default', 'index')
    props = db(db.user_properties.user == get_user_email()).select(db.user_properties.venues_can_rate).first()
    if props == None:
        c_can_rate = []
    else:
        c_can_rate = util.get_list(props.venues_can_rate)
    if not (c.rate_constraint == None or c.id in c_can_rate):
        session.flash = T('You cannot rate this venue.')
        redirect(URL('venues', 'rateopen_index'))
    t = datetime.utcnow()
    if not (c.is_active and c.is_approved and c.rate_open_date <= t and c.rate_close_date >= t):
        session.flash = T('This venue is not open for rating.')
        redirect(URL('venues', 'rateopen_index'))
    # The user can rate the venue.
    # Does the user have any pending reviewing tasks for the venue?
    # TODO(luca): rewrite this in terms of completed flag.
    num_open_tasks = db((db.task.user == get_user_email()) &
                        (db.task.venue_id == c.id) &
                        (db.task.is_completed == False)).count()
    if num_open_tasks >= c.max_number_outstanding_reviews:
        session.flash = T('You have too many reviews outstanding for this venue. '
                          'Complete some of them before accepting additional reviewing tasks.')
        redirect(URL('rating', 'task_index'))
        
    while True:
        new_item = ranker.get_item(c.id, get_user_email(),
                                   can_rank_own_submissions=c.can_rank_own_submissions)
        if new_item == None:
            session.flash = T('There are no additional items that can be reviewed.')
            redirect(URL('venues', 'rateopen_index'))
        # Checks that there are no other review tasks for the same user and submission id.
        # This is a safety measure, to prevent bugs caused by get_item.
        already_assigned = db((db.task.user == get_user_email()) & (db.task.submission_id == new_item)).count()
        if already_assigned == 0:
            break
        logger.error("get_item tried to assign twice submission %r to user %r" % (new_item, get_user_email()))
        
    # Creates a reviewing task.
    # To name it, counts how many tasks the user has already for this venue.
    num_tasks = db((db.task.venue_id == c.id) & (db.task.user == get_user_email())).count()
    task_name = (c.name + ' ' + T('Submission') + ' ' + str(num_tasks + 1))[:STRING_FIELD_LENGTH]
    task_name_id = keystore_write(task_name)
    task_id = db.task.insert(submission_id = new_item, venue_id = c.id, submission_name = task_name_id)
    # Increments the number of reviews for the item.
    subm = db.submission(new_item)
    if subm is not None:
        if subm.n_assigned_reviews is None:
            subm.n_assigned_reviews = 1
        else:
            subm.n_assigned_reviews = subm.n_assigned_reviews + 1
        subm.update_record()
    db.commit()
    session.flash = T('A review has been added to your review assignments.')
    redirect(URL('task_index', args=[task_id]))
Exemplo n.º 2
0
def accept_review():
    """Asks a user whether the user is willing to accept a rating task for a venue,
    and if so, picks a task and adds it to the set of tasks for the user."""
    # Checks the permissions.
    c = db.venue(request.args(0)) or redirect('default', 'index')
    props = db(db.user_properties.email == auth.user.email).select(db.user_properties.venues_can_rate).first()
    if props == None:
	c_can_rate = []
    else:
	c_can_rate = util.get_list(props.venues_can_rate)
    if not (c.rate_constraint == None or c.id in c_can_rate):
	session.flash = T('You cannot rate this venue.')
        redirect(URL('venues', 'rateopen_index'))
    t = datetime.utcnow()
    if not (c.is_active and c.is_approved and c.rate_open_date <= t and c.rate_close_date >= t):
        session.flash = T('This venue is not open for rating.')
        redirect(URL('venues', 'rateopen_index'))
    # The user can rate the venue.
    # Does the user have any pending reviewing tasks for the venue?
    num_open_tasks = db((db.task.user_id == auth.user_id) &
			(db.task.venue_id == c.id) &
			(db.task.rejected == False) &
			(db.task.completed_date > datetime.utcnow())).count()
    if num_open_tasks > c.max_number_outstanding_reviews:
	session.flash = T('You have too many reviews outstanding for this venue. '
			  'Complete some of them before accepting additional reviewing tasks.')
	redirect(URL('rating', 'task_index'))
    # This venue_form is used to display the venue.
    venue_form = SQLFORM(db.venue, record=c, readonly=True)
    confirmation_form = FORM.confirm(T('Accept'),
        {T('Decline'): URL('default', 'index')})
    if confirmation_form.accepted:
        # Reads the most recent ratings given by the user.
        # TODO(luca): we should really poll the rating system for this; that's what
        # should keep track of these things.
        previous_ratings = db((db.comparison.author == auth.user_id) 
            & (db.comparison.venue_id == c.id)).select(orderby=~db.comparison.date).first()
        # To get list of old items we need to check previous ratings
        # and current open tasks.
        if previous_ratings == None:
            old_items = []
        else:
            old_items = util.get_list(previous_ratings.ordering)
        # Now checking open tasks for the user.
        active_items_rows = db((db.task.venue_id == c.id) &
                               (db.task.user_id == auth.user_id) &
                               (db.task.completed_date == datetime(dates.MAXYEAR, 12, 1))
                               ).select(db.task.submission_id)
        active_items = [x.submission_id for x in active_items_rows]
        old_items.extend(active_items)
        new_item = ranker.get_item(db, c.id, auth.user_id, old_items,
				   can_rank_own_submissions=c.can_rank_own_submissions)
        if new_item == None:
            session.flash = T('There are no items to review so far.')
            redirect(URL('venues', 'rateopen_index'))
        # Creates a reviewing task.
        # To name it, counts how many tasks the user has already for this venue.
        num_tasks = db((db.task.venue_id == c.id) & (db.task.user_id == auth.user_id)).count()
        task_name = (c.name + ' ' + T('Submission') + ' ' + str(num_tasks + 1))[:STRING_FIELD_LENGTH]
        task_id = db.task.insert(submission_id = new_item, venue_id = c.id, submission_name = task_name)
	# Increments the number of reviews for the item.
	subm = db.submission(new_item)
	if subm is not None:
	    if subm.n_assigned_reviews is None:
		subm.n_assigned_reviews = 1
	    else:
		subm.n_assigned_reviews = subm.n_assigned_reviews + 1
	    subm.update_record()
        db.commit()
        session.flash = T('A review has been added to your review assignments.')
        redirect(URL('task_index', args=[task_id]))
    return dict(venue_form=venue_form, confirmation_form=confirmation_form)
Exemplo n.º 3
0
def accept_review():
    """Asks a user whether the user is willing to accept a rating task for a venue,
    and if so, picks a task and adds it to the set of tasks for the user."""
    # Checks the permissions.
    c = db.venue(request.args(0)) or redirect('default', 'index')
    props = db(db.user_properties.user == auth.user.email).select(
        db.user_properties.venues_can_rate).first()
    if props == None:
        c_can_rate = []
    else:
        c_can_rate = util.get_list(props.venues_can_rate)
    if not (c.rate_constraint == None or c.id in c_can_rate):
        session.flash = T('You cannot rate this venue.')
        redirect(URL('venues', 'rateopen_index'))
    t = datetime.utcnow()
    if not (c.is_active and c.is_approved and c.rate_open_date <= t
            and c.rate_close_date >= t):
        session.flash = T('This venue is not open for rating.')
        redirect(URL('venues', 'rateopen_index'))
    # The user can rate the venue.
    # Does the user have any pending reviewing tasks for the venue?
    num_open_tasks = db((db.task.user == auth.user.email)
                        & (db.task.venue_id == c.id)
                        & (db.task.rejected == False) &
                        (db.task.completed_date > datetime.utcnow())).count()
    if num_open_tasks > c.max_number_outstanding_reviews:
        session.flash = T(
            'You have too many reviews outstanding for this venue. '
            'Complete some of them before accepting additional reviewing tasks.'
        )
        redirect(URL('rating', 'task_index'))
    # This venue_form is used to display the venue.
    venue_form = SQLFORM(
        db.venue,
        record=c,
        readonly=True,
        fields=['name', 'description', 'rate_open_date', 'rate_close_date'])
    confirmation_form = FORM.confirm(T('Accept'),
                                     {T('Decline'): URL('default', 'index')})
    if confirmation_form.accepted:
        # Reads the most recent ratings given by the user.
        # TODO(luca): we should really poll the rating system for this; that's what
        # should keep track of these things.
        previous_ratings = db((db.comparison.user == auth.user.email)
                              & (db.comparison.venue_id == c.id)).select(
                                  orderby=~db.comparison.date).first()
        # To get list of old items we need to check previous ratings
        # and current open tasks.
        if previous_ratings == None:
            old_items = []
        else:
            old_items = util.get_list(previous_ratings.ordering)
        # Now checking open tasks for the user.
        active_items_rows = db((db.task.venue_id == c.id)
                               & (db.task.user == auth.user.email)
                               & (db.task.completed_date == datetime(
                                   dates.MAXYEAR, 12, 1))).select(
                                       db.task.submission_id)
        active_items = [x.submission_id for x in active_items_rows]
        old_items.extend(active_items)
        new_item = ranker.get_item(
            db,
            c.id,
            auth.user.email,
            old_items,
            can_rank_own_submissions=c.can_rank_own_submissions)
        if new_item == None:
            session.flash = T('There are no items to review so far.')
            redirect(URL('venues', 'rateopen_index'))
        # Creates a reviewing task.
        # To name it, counts how many tasks the user has already for this venue.
        num_tasks = db((db.task.venue_id == c.id)
                       & (db.task.user == auth.user.email)).count()
        task_name = (c.name + ' ' + T('Submission') + ' ' +
                     str(num_tasks + 1))[:STRING_FIELD_LENGTH]
        task_id = db.task.insert(submission_id=new_item,
                                 venue_id=c.id,
                                 submission_name=task_name)
        # Increments the number of reviews for the item.
        subm = db.submission(new_item)
        if subm is not None:
            if subm.n_assigned_reviews is None:
                subm.n_assigned_reviews = 1
            else:
                subm.n_assigned_reviews = subm.n_assigned_reviews + 1
            subm.update_record()
        db.commit()
        session.flash = T(
            'A review has been added to your review assignments.')
        redirect(URL('task_index', args=[task_id]))
    return dict(venue_form=venue_form, confirmation_form=confirmation_form)