def test_get_post_votes(app): user = (factories.UserFactory(first_name='u%d' % i) for i in itertools.count()) event = factories.EventFactory() post = factories.PostFactory(content_object=event) assert services.get_post_votes(post) == (0, 0) assert services.update_user_position(next(user), post, 1) == (1, 0) assert services.update_user_position(next(user), post, 1) == (2, 0) assert services.update_user_position(next(user), post, -1) == (2, 1) assert services.get_post_votes(post) == (2, 1) # Try to vote several times with the same user user = factories.UserFactory() assert services.update_user_position(user, post, 1) == (3, 1) assert services.update_user_position(user, post, -1) == (2, 2) assert services.update_user_position(user, post, -1) == (2, 2) assert services.update_user_position(user, post, 1) == (3, 1) assert services.get_post_votes(post) == (3, 1) assert services.get_user_topic_votes(user, post.topic) == {post.pk: 1}
def get_posts(user, topic, posts): result = [] user_votes = services.get_user_topic_votes(user, topic) if user.is_authenticated else {} curator_votes = services.get_curator_topic_votes(user, topic) if user.is_authenticated else {} for post in posts: if post['type'] == 'event': event = post['event'] result.append({ 'type': 'event', 'post': _get_post_context(post['post'], user_votes, curator_votes), 'event': { 'position_image': _get_position_image( event.position, 'img/event-positive.png', 'img/event-negative.png', 'img/event-neutral.png', ), 'name': event.title, 'timestamp': event.timestamp.strftime('%Y-%m-%d'), 'source': { 'link': event.source_link, 'name': event.source_title, }, }, }) elif post['type'] == 'curator': curator = post['curator'] position = services.get_user_topic_position(curator.user, topic) result.append({ 'type': 'curator', 'post': _get_post_context(post['post'], user_votes, curator_votes), 'curator': { 'position_image': _get_position_image( position, 'img/actor-positive.png', 'img/actor-negative.png', 'img/actor-neutral.png', ), 'name': str(curator.user), 'title': curator.title, 'photo': curator.photo, }, }) else: # post['type'] == 'quote' source = post['source'] actor = source.actor result.append({ 'type': 'quotes', 'source': { 'link': source.source_link, 'name': source.source_title, 'actor': { 'name': str(actor), 'title': source.actor_title or actor.title, 'photo': actor.photo, 'position_image': _get_position_image( source.position, 'img/actor-positive.png', 'img/actor-negative.png', 'img/actor-neutral.png', ), }, }, 'quotes': [{ 'text': quote.text, 'post': _get_post_context(post, user_votes, curator_votes), 'vote': { 'img': { 'top': 'img/thumb-up.png', 'bottom': 'img/thumb-down.png', }, }, 'arguments': [{ 'name': argument.title, 'classes': 'text-%s' % ('danger' if argument.position < 0 else 'success'), 'counterargument': { 'classes': 'glyphicon glyphicon-%s' % ('remove' if argument.counterargument else 'tag'), } } for argument in quote.postargument_set.order_by('pk')], } for post, quote in post['quotes']], }) return result
def get_posts(user, topic, posts): result = [] user_votes = services.get_user_topic_votes(user, topic) if user.is_authenticated() else {} curator_votes = services.get_curator_topic_votes(user, topic) if user.is_authenticated() else {} for post in posts: if post['type'] == 'event': event = post['event'] result.append({ 'type': 'event', 'post': _get_post_context(post['post'], user_votes, curator_votes), 'event': { 'position_image': _get_position_image( event.position, 'img/event-positive.png', 'img/event-negative.png', 'img/event-neutral.png', ), 'name': event.title, 'timestamp': event.timestamp.strftime('%Y-%m-%d'), 'source': { 'link': event.source_link, 'name': event.source_title, }, }, }) elif post['type'] == 'curator': curator = post['curator'] position = services.get_user_topic_position(curator.user, topic) result.append({ 'type': 'curator', 'post': _get_post_context(post['post'], user_votes, curator_votes), 'curator': { 'position_image': _get_position_image( position, 'img/actor-positive.png', 'img/actor-negative.png', 'img/actor-neutral.png', ), 'name': str(curator.user), 'title': curator.title, 'photo': curator.photo, }, }) else: # post['type'] == 'quote' source = post['source'] actor = source.actor result.append({ 'type': 'quotes', 'source': { 'link': source.source_link, 'name': source.source_title, 'actor': { 'name': str(actor), 'title': source.actor_title or actor.title, 'photo': actor.photo, 'position_image': _get_position_image( source.position, 'img/actor-positive.png', 'img/actor-negative.png', 'img/actor-neutral.png', ), }, }, 'quotes': [{ 'text': quote.text, 'post': _get_post_context(post, user_votes, curator_votes), 'vote': { 'img': { 'top': 'img/thumb-up.png', 'bottom': 'img/thumb-down.png', }, }, 'arguments': [{ 'name': argument.title, 'classes': 'text-%s' % ('danger' if argument.position < 0 else 'success'), 'counterargument': { 'classes': 'glyphicon glyphicon-%s' % ('remove' if argument.counterargument else 'tag'), } } for argument in quote.postargument_set.order_by('pk')], } for post, quote in post['quotes']], }) return result