changeset 6517:a7271dbefd96

gists: simplify expiration checks
author Søren Løvborg <sorenl@unity3d.com>
date Wed, 22 Feb 2017 21:13:37 +0100
parents eea19c23b741
children a083ed3a5032
files kallithea/controllers/admin/gists.py kallithea/controllers/api/api.py kallithea/model/db.py
diffstat 3 files changed, 14 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/admin/gists.py	Thu Feb 23 18:00:56 2017 +0100
+++ b/kallithea/controllers/admin/gists.py	Wed Feb 22 21:13:37 2017 +0100
@@ -72,7 +72,7 @@
         c.show_public = request.GET.get('public') and not_default_user
 
         gists = Gist().query() \
-            .filter(or_(Gist.gist_expires == -1, Gist.gist_expires >= time.time())) \
+            .filter_by(is_expired=False) \
             .order_by(Gist.created_on.desc())
 
         # MY private
@@ -166,12 +166,10 @@
     def show(self, gist_id, revision='tip', format='html', f_path=None):
         c.gist = Gist.get_or_404(gist_id)
 
-        #check if this gist is not expired
-        if c.gist.gist_expires != -1:
-            if time.time() > c.gist.gist_expires:
-                log.error('Gist expired at %s',
-                          time_to_datetime(c.gist.gist_expires))
-                raise HTTPNotFound()
+        if c.gist.is_expired:
+            log.error('Gist expired at %s',
+                      time_to_datetime(c.gist.gist_expires))
+            raise HTTPNotFound()
         try:
             c.file_changeset, c.files = GistModel().get_gist_files(gist_id,
                                                             revision=revision)
@@ -189,12 +187,10 @@
     def edit(self, gist_id, format='html'):
         c.gist = Gist.get_or_404(gist_id)
 
-        #check if this gist is not expired
-        if c.gist.gist_expires != -1:
-            if time.time() > c.gist.gist_expires:
-                log.error('Gist expired at %s',
-                          time_to_datetime(c.gist.gist_expires))
-                raise HTTPNotFound()
+        if c.gist.is_expired:
+            log.error('Gist expired at %s',
+                      time_to_datetime(c.gist.gist_expires))
+            raise HTTPNotFound()
         try:
             c.file_changeset, c.files = GistModel().get_gist_files(gist_id)
         except VCSError:
--- a/kallithea/controllers/api/api.py	Thu Feb 23 18:00:56 2017 +0100
+++ b/kallithea/controllers/api/api.py	Wed Feb 22 21:13:37 2017 +0100
@@ -2355,7 +2355,7 @@
         return [
             gist.get_api_data()
             for gist in Gist().query()
-                .filter(or_(Gist.gist_expires == -1, Gist.gist_expires >= time.time()))
+                .filter_by(is_expired=False)
                 .filter(Gist.owner_id == user_id)
                 .order_by(Gist.created_on.desc())
         ]
--- a/kallithea/model/db.py	Thu Feb 23 18:00:56 2017 +0100
+++ b/kallithea/model/db.py	Wed Feb 22 21:13:37 2017 +0100
@@ -2533,6 +2533,10 @@
 
     owner = relationship('User')
 
+    @hybrid_property
+    def is_expired(self):
+        return (self.gist_expires != -1) & (time.time() > self.gist_expires)
+
     def __repr__(self):
         return '<Gist:[%s]%s>' % (self.gist_type, self.gist_access_id)