Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 803565d

Browse files
committedSep 14, 2015
basic pluggable like model
1 parent fb8082c commit 803565d

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
 

‎firefly/models/like.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# coding: utf-8
2+
3+
from __future__ import absolute_import
4+
from datetime import datetime
5+
6+
from firefly.ext import db
7+
from .user import User
8+
9+
10+
class Likes(object):
11+
def __init__(self, product_type):
12+
self.product_type = product_type
13+
self._instance = None
14+
15+
def __get__(self, instance, owner):
16+
self._instance = instance
17+
return self
18+
19+
def productidgetter(self, func):
20+
return func
21+
22+
@property
23+
def product_id(self):
24+
return self.productidgetter(self._instance)
25+
26+
def add(self, user_id):
27+
user = User.objects.filter(id=user_id).first()
28+
return Like.objects.create(
29+
product_id=self.product_id,
30+
product_type=self.product_type,
31+
user=user
32+
)
33+
34+
35+
class Like(db.Document):
36+
id = db.SequenceField(primary_key=True)
37+
created_at = db.DateTimeField(default=datetime.utcnow, required=True)
38+
product_type = db.IntField()
39+
product_id = db.IntField()
40+
user = db.ReferenceField(User)

‎firefly/models/topic.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from firefly.views.utils import timesince
1111
from firefly.models.consts import CATEGORY_COLORS
1212
from .user import User
13+
from .like import Likes
1314

1415
__all__ = ["Category", "Post", "Video", "Image", "Comment"]
1516

@@ -58,6 +59,12 @@ class Post(db.Document):
5859
comments = db.ListField(db.ReferenceField('Comment'))
5960
category = db.ReferenceField(Category)
6061

62+
likes = Likes('Post')
63+
64+
@likes.productidgetter
65+
def product_id(self):
66+
return str(self.id)
67+
6168
def url(self):
6269
return url_for('post.detail', id=self.id)
6370

@@ -84,14 +91,17 @@ def recent_activity_time(self):
8491

8592

8693
class Video(Post):
94+
likes = Likes('Video')
8795
embed_code = db.StringField(required=True)
8896

8997

9098
class Image(Post):
99+
likes = Likes('Image')
91100
image_url = db.StringField(required=True, max_length=255)
92101

93102

94103
class Quote(Post):
104+
likes = Likes('Quote')
95105
content = db.StringField(required=True)
96106
author = db.ReferenceField(User)
97107

@@ -103,6 +113,12 @@ class Comment(db.Document):
103113
author = db.ReferenceField(User)
104114
ref_id = db.IntField(default=0)
105115

116+
likes = Likes('Comment')
117+
118+
@likes.productidgetter
119+
def product_id(self):
120+
return str(self.id)
121+
106122
@property
107123
def post_type(self):
108124
return self.__class__.__name__

0 commit comments

Comments
 (0)
Please sign in to comment.