示例#1
0
def tagged(tag=''):
    """Return all tagged nodes of public projects as JSON."""
    from pillar.auth import current_user

    # We explicitly register the tagless endpoint to raise a 404, otherwise the PATCH
    # handler on /api/nodes/<node_id> will return a 405 Method Not Allowed.
    if not tag:
        raise wz_exceptions.NotFound()

    # Build the (cached) list of tagged nodes
    agg_list = _tagged(tag)

    for node in agg_list:
        if node['properties'].get('duration_seconds'):
            node['properties']['duration'] = datetime.timedelta(seconds=node['properties']['duration_seconds'])

        if node.get('_created') is not None:
            node['pretty_created'] = pretty_date(node['_created'])

    # If the user is anonymous, no more information is needed and we return
    if current_user.is_anonymous:
        return jsonify(agg_list)

    # If the user is authenticated, attach view_progress for video assets
    view_progress = current_user.nodes['view_progress']
    for node in agg_list:
        node_id = str(node['_id'])
        # View progress should be added only for nodes of type 'asset' and
        # with content_type 'video', only if the video was already in the watched
        # list for the current user.
        if node_id in view_progress:
            node['view_progress'] = view_progress[node_id]

    return jsonify(agg_list)
示例#2
0
def format_comment(comment, is_reply=False, is_team=False, replies=None):
    """Format a comment node into a simpler dictionary.

    :param comment: the comment object
    :param is_reply: True if the comment is a reply to another comment
    :param is_team: True if the author belongs to the group that owns the node
    :param replies: list of replies (formatted with this function)
    """
    try:
        is_own = (current_user.objectid == comment.user._id) \
            if current_user.is_authenticated else False
    except AttributeError:
        current_app.bugsnag.notify(
            Exception('Missing user for embedded user ObjectId'),
            meta_data={'nodes_info': {
                'node_id': comment['_id']
            }})
        return
    is_rated = False
    is_rated_positive = None
    if comment.properties.ratings:
        for rating in comment.properties.ratings:
            if current_user.is_authenticated and rating.user == current_user.objectid:
                is_rated = True
                is_rated_positive = rating.is_positive
                break

    return dict(_id=comment._id,
                gravatar=gravatar(comment.user.email, size=32),
                time_published=pretty_date(comment._created or datetime_now(),
                                           detail=True),
                rating=comment.properties.rating_positive -
                comment.properties.rating_negative,
                author=comment.user.full_name,
                author_username=comment.user.username,
                content=comment.properties.content,
                is_reply=is_reply,
                is_own=is_own,
                is_rated=is_rated,
                is_rated_positive=is_rated_positive,
                is_team=is_team,
                replies=replies)
示例#3
0
def notification_parse(notification):
    if notification.actor:
        username = notification.actor['username']
        avatar = notification.actor['avatar']
    else:
        return None
    return dict(_id=notification['_id'],
                username=username,
                username_avatar=avatar,
                action=notification.action,
                object_type=notification.object_type,
                object_name=notification.object_name,
                object_url=url_for('nodes.redirect_to_context',
                                   node_id=notification.object_id),
                context_object_type=notification.context_object_type,
                context_object_name=notification.context_object_name,
                context_object_url=url_for(
                    'nodes.redirect_to_context',
                    node_id=notification.context_object_id),
                date=pretty_date(notification['_created'], detail=True),
                is_read=notification.is_read,
                is_subscribed=notification.is_subscribed,
                subscription=notification.subscription)
示例#4
0
def format_pretty_date_time(d):
    return pretty_date(d, detail=True)
示例#5
0
def format_pretty_date(d):
    return pretty_date(d)
示例#6
0
 def pd(**diff):
     return pretty_date(now - datetime.timedelta(**diff),
                        detail=True,
                        now=now)
示例#7
0
 def pd(**diff):
     return pretty_date(now + datetime.timedelta(**diff), now=now)
示例#8
0
    def test_none(self):
        from pillar.web.utils import pretty_date

        self.assertIsNone(pretty_date(None))