def get_topic(id, user=None): """Gets a single topic (formatted by json_data) from redis or postgresql And performs some pre-filters for the current user Args: id (int): id user (User): the current user Returns: None if the topic is not existed or a dict of topic data """ id = int(id) key = Topic.cache_key(id) redis_client = get_client(key) topic = None if not redis_client.exists(key): topic = Topic.find_by_pk(id) if topic: data = topic.json_data() redis_client.set(key, json.dumps(data)) topic = data else: if redis_client.type(key) == 'hash': print redis_client topic = json.loads(redis_client.get(key)) topic['user_vote'] = Topic.check_vote(user, topic['id']) return topic
def view(slug, id): topic = g.topic user = g.user if topic.update_view_count(ip=request.remote_addr, user=user, commit=True): cache.update_topic(topic.id, topic) cache.update_sorted_topics(topic, 'view_count') topic_data = topic.json_data() topic_data['comments'] = topic.get_comments(json=True) topic_data['user_vote'] = Topic.check_vote(user, topic.id) topic_data['user_comment'] = Topic.check_comment(user, topic.id) context = { 'js_module': 'view_topic', 'style': 'view_topic', 'topic': topic_data, 'sub_title': topic.title } return render_template('topics/view.html', **context)