Exemplo n.º 1
0
 def decr_viewcount(self):
     cache_key = self._remaining_viewcount_by_student_cache_key % self.id
     if mc.get(cache_key):
         viewcount = int(mc.get(cache_key))
         if viewcount <= 0:
             raise CannotViewPostContactError()
         mc.decr(cache_key)
Exemplo n.º 2
0
 def get(cls, id_):
     cache_key = cls._course_post_by_id_cache_key % id_
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     post = CoursePost.query.get(id_)
     if post:
         mc.set(cache_key, pickle.dumps(post).hex())
         mc.expire(cache_key, ONE_HOUR)
     return post
Exemplo n.º 3
0
 def get_by_open_id(cls, open_id):
     cache_key = cls._wechat_user_by_open_id_cache_key % open_id
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     wechat_user = cls.query.filter_by(open_id=open_id).first()
     if wechat_user:
         mc.set(cache_key, pickle.dumps(wechat_user).hex())
         mc.expire(cache_key, ONE_DAY)
     return wechat_user
Exemplo n.º 4
0
 def get_all(cls):
     cache_key = cls._all_course_cache_key
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     else:
         courses = Course.query.all()
         mc.set(cache_key, pickle.dumps(courses).hex())
         mc.expire(cache_key, ONE_DAY)
         return courses
Exemplo n.º 5
0
 def get(cls, id_):
     cache_key = cls._student_cache_key % id_
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     student = cls.query.filter_by(id=id_).first()
     if student:
         mc.set(cache_key, pickle.dumps(student).hex())
         mc.expire(cache_key, ONE_HOUR)
     return student
Exemplo n.º 6
0
 def remaining_viewcount(self):
     cache_key = self._remaining_viewcount_by_student_cache_key % self.id
     if mc.get(cache_key):
         viewcount = int(mc.get(cache_key))
         return viewcount
     else:
         mc.set(cache_key, self.MAX_VIEWCOUNT)
         mc.expire(cache_key, ONE_DAY)
         return self.MAX_VIEWCOUNT
Exemplo n.º 7
0
 def get(cls, id_):
     cache_key = cls._course_post_demand_by_id_cache_key % id_
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     course_demand = CourseDemand.query.get(id_)
     if course_demand:
         mc.set(cache_key, pickle.dumps(course_demand).hex())
         mc.expire(cache_key, ONE_DAY)
     return course_demand
Exemplo n.º 8
0
 def demand(self):
     cache_key = self._course_post_demand_by_post_id_cache_key % self.id
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     course_demand = CourseDemand.get_by_post(self.id)
     if course_demand and course_demand.course_id:
         mc.set(cache_key, pickle.dumps(course_demand).hex())
         mc.expire(cache_key, ONE_DAY)
         return course_demand
     return None
Exemplo n.º 9
0
 def supply(self):
     cache_key = self._course_post_supply_by_post_id_cache_key % self.id
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     course_supply = CourseSupply.get_by_post(self.id)
     if course_supply and course_supply.course_id:
         mc.set(cache_key, pickle.dumps(course_supply).hex())
         mc.expire(cache_key, ONE_DAY)
         return course_supply
     return None
Exemplo n.º 10
0
 def get(cls, id_):
     cache_key = cls._course_cache_key % id_
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     else:
         course = Course.query.get(id_)
         if course:
             mc.set(cache_key, pickle.dumps(course).hex())
             mc.expire(cache_key, ONE_DAY)
         return course
Exemplo n.º 11
0
 def get(cls, id_):
     cache_key = cls._wechat_session_by_id_cache_key % id_
     if mc.get(cache_key):
         wechat_session = pickle.loads(bytes.fromhex(mc.get(cache_key)))
         mc.expire(cache_key, HALF_DAY)
         return wechat_session
     wechat_session = cls.query.get(id_)
     if wechat_session:
         mc.set(cache_key, pickle.dumps(wechat_session).hex())
         mc.expire(cache_key, HALF_DAY)
     return wechat_session
Exemplo n.º 12
0
 def gets(cls, student_id, post_id, post_type=PostType.course_post):
     cache_key = cls._records_by_student_and_post_and_type_cache_key % (
         student_id, post_id, post_type.value)
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     records = cls.query.filter_by(student_id=student_id,
                                   post_id=post_id,
                                   post_type_=post_type.value).all()
     if records:
         mc.set(cache_key, pickle.dumps(records).hex())
         mc.expire(cache_key, ONE_DAY)
     return records
Exemplo n.º 13
0
    def get_by_third_session_key(cls, third_session_key):
        id_ = mc.get(cls._id_by_open_id_cache_key % third_session_key)

        wechat_session = cls.get(id_) if id_ else cls.query.filter_by(
            third_session_key=third_session_key).first()

        if wechat_session and not wechat_session.expired:
            mc.set(cls._id_by_open_id_cache_key % wechat_session.open_id, wechat_session.id)
            mc.expire(cls._id_by_open_id_cache_key % wechat_session.open_id, HALF_DAY)
            return wechat_session
        return None
Exemplo n.º 14
0
 def avatar(self):
     cache_key = self._avatar_cache_key % self.id
     if mc.get(cache_key):
         return pickle.loads(bytes.fromhex(mc.get(cache_key)))
     return self.cache_avatar(self.id, self.avatar_url)