Ejemplo n.º 1
0
def new_topic(request):
    topics_storage = Topics(request)
    user_storage = Users(request)
    # process new post submition
    post_data = dict()
    save_result = False
    if 'title' in request.POST and 'author' in request.POST and 'message' in request.POST:
        for key, value in request.POST.items():
            post_data[key] = value
            post_data['date'] = datetime.datetime.utcnow()

        # get or create user by checking the username
        post_data['author_id'] = user_storage.get_or_create_user(username=post_data['author'])

        del post_data['author']
        # SAVE it on mongo!
        save_result = topics_storage.save_topic(**post_data)
        # end process new topic submition
    return {
        'cur_page': 'topic',
        'save_result': save_result
    }
Ejemplo n.º 2
0
def home(request):

    topics_storage = Topics(request)
    user_storage = Users(request)
    post_storage = Posts(request)

    topics = topics_storage.get_recent_topics(num_of_entries=5, page=1)
    recent_topics = []
    for topic in topics:
        author = user_storage.get_user(id=topic[u'author_id'])
        posts_qty = post_storage.get_posts_count_by_topic_id(topic[u'_id'])
        entry = {
            'title': topic[u'title'],
            'author': author[u'username'],
            'id': topic[u'_id'],
            'posts_qty': posts_qty,
        }
        recent_topics.append(entry)
    users_storage = Users(request)
    users = users_storage.get_most_active_users(num_of_entries=5, page=1)
    active_users = []
    for user in users:
        user_posts_count = post_storage.get_posts_count_by_author_id(user[u'_id'])
        entry = {
            'username': user[u'username'],
            'user_posts_count': user_posts_count,
            'id': user[u'_id'],
        }
        active_users.append(entry)
    context = {
        'cur_page': 'home',
        'page_title': 'Welcome to Anchor Forums',
        'recent_topics': recent_topics,
        'active_users': active_users,
    }
    return context
Ejemplo n.º 3
0
def topic(context, request):
    topics_storage = Topics(request)
    user_storage = Users(request)
    post_storage = Posts(request)

    #context.__name__ is our {topic_id} and this comes from resources.py
    if context.__name__:
        topic = topics_storage.get_topic_by_id(context.__name__)
        
        #throw a NotFound Exception if the {topic_id} doesnt exist, this will kick off our 404 view
        if topic is None:
            raise HTTPNotFound('notfound').exception

        # process new post submition
        post_data = dict()
        save_result = False
        if 'author' in request.POST and 'message' in request.POST:
            for key, value in request.POST.items():
                post_data[key] = value
            post_data['date'] = datetime.datetime.utcnow()
            post_data['topic_id'] = topic['_id']

            # get or create user by checking the username
            post_data['author_id'] = user_storage.get_or_create_user(username=post_data['author'])

            del post_data['author']
            # SAVE it on mongo!
            save_result = post_storage.save_post(**post_data)
            # end process new post submition

        author = user_storage.get_user(id=topic[u'author_id'])
        posts = post_storage.get_posts_by_topic_id(topic[u'_id'])
        posts_of_topic = []
        for post in posts:
            author = user_storage.get_user(id=post[u'author_id'])
            post_data = {
                'id': post['_id'],
                'date': post['date'],
                'author': author['username'],
                'message': post['message'],
            }
            posts_of_topic.append(post_data)

        context =  {
            'cur_page': 'topic',
            'page_title': topic[u'title'],
            'message': topic[u'message'],
            'author': author[u'username'],
            'id': topic[u'_id'],
            'posts': posts_of_topic,
            'save_result' : save_result,
        }
        return context
    else:
        # no topic id provided, so return a list of all topics
        topics = topics_storage.get_topics()
        all_topics = []
        for topic in topics:
            author = user_storage.get_user(id=topic[u'author_id'])
            posts_qty = post_storage.get_posts_count_by_topic_id(topic[u'_id'])
            entry = {
                'title': topic[u'title'],
                'author': author[u'username'],
                'id': topic[u'_id'],
                'date': topic[u'date'],
                'posts_qty': posts_qty
            }
            all_topics.append(entry)
        return {'cur_page': 'topic', 'all_topics':all_topics, 'list_topics': True}