Skip to content
This repository
Browse code

Remove cup_info system and reimplement cake days more simply.

This removes a hardcache multi-lookup on every builder'd page and
simplifies the system for cake days.

Cups for the nightly "best *" awards are casualties of this, but they're
little known and probably won't be missed.
  • Loading branch information...
commit 24729503f9b212c30064b237bb292b21ef7d1bd5 1 parent 531c1bd
Neil Williams authored September 12, 2013
23  r2/r2/controllers/api.py
@@ -3101,11 +3101,9 @@ def POST_selectflair(self, form, jquery, user, link, flair_template_id,
3101 3101
                    award=VByName("fullname"),
3102 3102
                    description=VLength("description", max_length=1000),
3103 3103
                    url=VLength("url", max_length=1000),
3104  
-                   cup_hours=VFloat("cup_hours",
3105  
-                                      coerce=False, min=0, max=24 * 365),
3106 3104
                    recipient=VExistingUname("recipient"))
3107 3105
     def POST_givetrophy(self, form, jquery, secret_used, award, description,
3108  
-                        url, cup_hours, recipient):
  3106
+                        url, recipient):
3109 3107
         if form.has_errors("recipient", errors.USER_DOESNT_EXIST,
3110 3108
                                         errors.NO_USER):
3111 3109
             pass
@@ -3113,9 +3111,6 @@ def POST_givetrophy(self, form, jquery, secret_used, award, description,
3113 3111
         if form.has_errors("fullname", errors.NO_TEXT, errors.NO_THING_ID):
3114 3112
             pass
3115 3113
 
3116  
-        if form.has_errors("cup_hours", errors.BAD_NUMBER):
3117  
-            pass
3118  
-        
3119 3114
         if secret_used and not award.api_ok:
3120 3115
             c.errors.add(errors.NO_API, field='secret')
3121 3116
             form.has_errors('secret', errors.NO_API)
@@ -3123,24 +3118,10 @@ def POST_givetrophy(self, form, jquery, secret_used, award, description,
3123 3118
         if form.has_error():
3124 3119
             return
3125 3120
 
3126  
-        if cup_hours:
3127  
-            cup_seconds = int(cup_hours * 3600)
3128  
-            cup_expiration = timefromnow("%s seconds" % cup_seconds)
3129  
-        else:
3130  
-            cup_expiration = None
3131  
-        
3132  
-        t = Trophy._new(recipient, award, description=description, url=url,
3133  
-                        cup_info=dict(expiration=cup_expiration))
  3121
+        t = Trophy._new(recipient, award, description=description, url=url)
3134 3122
 
3135 3123
         form.set_html(".status", _('saved'))
3136 3124
         form._send_data(trophy_fn=t._id36)
3137  
-    
3138  
-    @validatedForm(VAdmin(),
3139  
-                   account = VExistingUname("account"))
3140  
-    def POST_removecup(self, form, jquery, account):
3141  
-        if not account:
3142  
-            return self.abort404()
3143  
-        account.remove_cup()
3144 3125
 
3145 3126
     @validatedForm(secret_used=VAdminOrAdminSecret("secret"),
3146 3127
                    trophy = VTrophy("trophy_fn"))
1  r2/r2/lib/pages/pages.py
@@ -1617,7 +1617,6 @@ def __init__(self, user):
1617 1617
                 self.trophies.append(trophy)
1618 1618
                 award_ids_seen.append(trophy._thing2_id)
1619 1619
 
1620  
-        self.cup_info = user.cup_info()
1621 1620
         Templated.__init__(self)
1622 1621
 
1623 1622
 
11  r2/r2/lib/template_helpers.py
@@ -494,11 +494,10 @@ def panel_size(state):
494 494
 
495 495
 # Appends to the list "attrs" a tuple of:
496 496
 # <priority (higher trumps lower), letter,
497  
-#  css class, i18n'ed mouseover label, hyperlink (opt), img (opt)>
  497
+#  css class, i18n'ed mouseover label, hyperlink (opt)>
498 498
 def add_attr(attrs, kind, label=None, link=None, cssclass=None, symbol=None):
499 499
     from r2.lib.template_helpers import static
500 500
 
501  
-    img = None
502 501
     symbol = symbol or kind
503 502
 
504 503
     if kind == 'F':
@@ -546,10 +545,10 @@ def add_attr(attrs, kind, label=None, link=None, cssclass=None, symbol=None):
546 545
             raise ValueError ("Need a label")
547 546
     elif kind == 'special':
548 547
         priority = 98
549  
-    elif kind.startswith ('trophy:'):
550  
-        img = (kind[7:], '!', 11, 8)
  548
+    elif kind == "cake":
551 549
         priority = 99
552  
-        cssclass = 'recent-trophywinner'
  550
+        cssclass = "cakeday"
  551
+        symbol = "&#x1F370;"
553 552
         if not label:
554 553
             raise ValueError ("Need a label")
555 554
         if not link:
@@ -557,7 +556,7 @@ def add_attr(attrs, kind, label=None, link=None, cssclass=None, symbol=None):
557 556
     else:
558 557
         raise ValueError ("Got weird kind [%s]" % kind)
559 558
 
560  
-    attrs.append( (priority, symbol, cssclass, label, link, img) )
  559
+    attrs.append( (priority, symbol, cssclass, label, link) )
561 560
 
562 561
 
563 562
 def search_url(query, subreddit, restrict_sr="off", sort=None, recent=None):
41  r2/r2/models/account.py
@@ -446,43 +446,6 @@ def add_share_emails(self, emails):
446 446
 
447 447
         self.share = share
448 448
 
449  
-    def set_cup(self, cup_info):
450  
-        from r2.lib.template_helpers import static
451  
-
452  
-        if cup_info is None:
453  
-            return
454  
-
455  
-        if cup_info.get("expiration", None) is None:
456  
-            return
457  
-
458  
-        cup_info.setdefault("label_template",
459  
-          "%(user)s recently won a trophy! click here to see it.")
460  
-
461  
-        cup_info.setdefault("img_url", static('award.png'))
462  
-
463  
-        existing_info = self.cup_info()
464  
-
465  
-        if (existing_info and
466  
-            existing_info["expiration"] > cup_info["expiration"]):
467  
-            # The existing award has a later expiration,
468  
-            # so it trumps the new one as far as cups go
469  
-            return
470  
-
471  
-        td = cup_info["expiration"] - timefromnow("0 seconds")
472  
-
473  
-        cache_lifetime = td.seconds
474  
-
475  
-        if cache_lifetime <= 0:
476  
-            g.log.error("Adding a cup that's already expired?")
477  
-        else:
478  
-            g.hardcache.set("cup_info-%d" % self._id, cup_info, cache_lifetime)
479  
-
480  
-    def remove_cup(self):
481  
-        g.hardcache.delete("cup_info-%d" % self._id)
482  
-
483  
-    def cup_info(self):
484  
-        return g.hardcache.get("cup_info-%d" % self._id)
485  
-
486 449
     def special_distinguish(self):
487 450
         if self._t.get("special_distinguish_name"):
488 451
             return dict((k, self._t.get("special_distinguish_"+k, None))
@@ -600,10 +563,6 @@ def quota_full(self, kind):
600 563
         return filled_quota
601 564
 
602 565
     @classmethod
603  
-    def cup_info_multi(cls, ids):
604  
-        return g.hardcache.get_multi(ids, prefix="cup_info-")
605  
-
606  
-    @classmethod
607 566
     def system_user(cls):
608 567
         try:
609 568
             return cls._by_name(g.system_user)
13  r2/r2/models/award.py
@@ -66,7 +66,7 @@ def _by_codename(cls, codename):
66 66
 
67 67
     @classmethod
68 68
     def give_if_needed(cls, codename, user,
69  
-                       description=None, url=None, cup_info=None):
  69
+                       description=None, url=None):
70 70
         """Give an award to a user, unless they already have it.
71 71
            Returns the trophy. Does nothing and prints nothing
72 72
            (except for g.log.debug) if the award doesn't exist."""
@@ -86,7 +86,7 @@ def give_if_needed(cls, codename, user,
86 86
 
87 87
         g.log.debug("Gave %s to %s" % (codename, user))
88 88
         return Trophy._new(user, award, description=description,
89  
-                        url=url, cup_info=cup_info)
  89
+                        url=url)
90 90
 
91 91
     @classmethod
92 92
     def take_away(cls, codename, user):
@@ -118,21 +118,19 @@ def take_away(cls, codename, user):
118 118
             g.log.debug("%s didn't have %s" % (user, codename))
119 119
 
120 120
 class FakeTrophy(object):
121  
-    def __init__(self, recipient, award, description=None, url=None,
122  
-                 cup_info=None):
  121
+    def __init__(self, recipient, award, description=None, url=None):
123 122
         self._thing2 = award
124 123
         self._thing1 = recipient
125 124
         self.description = description
126 125
         self.url = url
127 126
         self.trophy_url = getattr(self, "url",
128 127
                                   getattr(self._thing2, "url", None))
129  
-        self.cup_info = cup_info
130 128
         self._id = self._id36 = None
131 129
 
132 130
 class Trophy(Relation(Account, Award)):
133 131
     @classmethod
134 132
     def _new(cls, recipient, award, description = None,
135  
-             url = None, cup_info = None):
  133
+             url = None):
136 134
 
137 135
         # The "name" column of the relation can't be a constant or else a
138 136
         # given account would not be allowed to win a given award more than
@@ -150,9 +148,6 @@ def _new(cls, recipient, award, description = None,
150 148
         if url:
151 149
             t.url = url
152 150
 
153  
-        if cup_info:
154  
-            recipient.set_cup(cup_info)
155  
-
156 151
         t._commit()
157 152
         t.update_caches()
158 153
         return t
22  r2/r2/models/builder.py
@@ -71,16 +71,17 @@ def wrap_items(self, items):
71 71
         #get authors
72 72
         #TODO pull the author stuff into add_props for links and
73 73
         #comments and messages?
74  
-
75 74
         aids = set(l.author_id for l in items if hasattr(l, 'author_id')
76 75
                    and l.author_id is not None)
77 76
 
78 77
         authors = {}
79  
-        cup_infos = {}
  78
+        cakes = {}
80 79
         friend_rels = None
81 80
         if aids:
82 81
             authors = Account._byID(aids, data=True, stale=self.stale) if aids else {}
83  
-            cup_infos = Account.cup_info_multi(aids)
  82
+            now = datetime.datetime.now(g.tz)
  83
+            cakes = {a._id for a in authors.itervalues()
  84
+                     if a.cake_expiration and a.cake_expiration >= now}
84 85
             if user and user.gold:
85 86
                 friend_rels = user.friend_rels()
86 87
 
@@ -174,13 +175,14 @@ def wrap_items(self, items):
174 175
                     args['kind'] = 'special'
175 176
                 add_attr(w.attribs, **args)
176 177
 
177  
-            if w.author and w.author._id in cup_infos and not c.profilepage:
178  
-                cup_info = cup_infos[w.author._id]
179  
-                label = _(cup_info["label_template"]) % \
180  
-                        {'user':w.author.name}
181  
-                add_attr(w.attribs, 'trophy:' + cup_info["img_url"],
182  
-                         label=label,
183  
-                         link = "/user/%s" % w.author.name)
  178
+            if w.author and w.author._id in cakes and not c.profilepage:
  179
+                add_attr(
  180
+                    w.attribs,
  181
+                    kind="cake",
  182
+                    label=(_("%(user)s just celebrated a reddit birthday!") %
  183
+                           {"user": w.author.name}),
  184
+                    link="/user/%s" % w.author.name,
  185
+                )
184 186
 
185 187
             if hasattr(item, "sr_id") and item.sr_id is not None:
186 188
                 w.subreddit = subreddits[item.sr_id]
9  r2/r2/public/static/css/reddit.less
@@ -766,6 +766,15 @@ ul.flat-vert {text-align: left;}
766 766
 .tagline .edited-timestamp{ cursor: default }
767 767
 .tagline .stickied-tagline { color: @moderator-color }
768 768
 
  769
+.tagline .userattrs .cakeday {
  770
+    display: inline-block;
  771
+    text-indent: -9999px;
  772
+    width: 11px;
  773
+    height: 8px;
  774
+    background-image: url(../cake.png); /* SPRITE */
  775
+    vertical-align: middle;
  776
+}
  777
+
769 778
 a.author { margin-right: 0.5em; }
770 779
 
771 780
 .flair, .linkflairlabel {
9  r2/r2/templates/adminawardgive.html
@@ -62,15 +62,6 @@
62 62
         <input type="text" name="url" value="${thing.url}" />
63 63
       </td>
64 64
     </tr>
65  
-    <tr>
66  
-      <td>
67  
-        hours to show cup
68  
-      </td>
69  
-      <td>
70  
-        <input type="text" name="cup_hours" value="${thing.hours}" />
71  
-        ${error_field("BAD_NUMBER", "cup_hours", "span")}
72  
-      </td>
73  
-    </tr>
74 65
   </table>
75 66
 
76 67
   <button class="btn" type="submit">give</button>
18  r2/r2/templates/trophycase.html
@@ -52,24 +52,6 @@
52 52
   </td>
53 53
 </%def>
54 54
 
55  
-%if c.user_is_admin and thing.cup_info:
56  
-  <div class="cup-info-box">
57  
-    cup:
58  
-    &#32;
59  
-    <img src="${thing.cup_info['img_url']}" alt="[image not found]" />
60  
-    <br/>
61  
-    &laquo;
62  
-    <tt>${thing.cup_info["label_template"] % dict(user=thing.user.name)}</tt>
63  
-    &raquo;
64  
-    <br/>
65  
-    show cup until:
66  
-    ${thing.cup_info["expiration"].astimezone(g.display_tz).strftime("%Y-%m-%d %H:%M:%S %Z")}
67  
-    &#32;
68  
-    ${ynbutton(_("remove"), _("removed"), "removecup", "hide_thing",
69  
-               hidden_data=dict(account=thing.user.name))}
70  
-  </div>
71  
-%endif
72  
-
73 55
 ## for now
74 56
 %if not thing.trophies:
75 57
   <div class="dust">${_("dust")}</div>
7  r2/r2/templates/wrappeduser.html
@@ -61,19 +61,14 @@
61 61
     <span class="userattrs">
62 62
     %if thing.attribs:
63 63
       [
64  
-      %for priority, abbv, css_class, label, attr_link, img in thing.attribs:
  64
+      %for priority, abbv, css_class, label, attr_link in thing.attribs:
65 65
         %if attr_link:
66 66
           <a class="${css_class}" title="${label}"
67 67
           %if target:
68 68
              target="${target}"
69 69
           %endif
70 70
              href="${attr_link}">
71  
-          %if img:
72  
-            <% (src, alt, width, height) = img %>
73  
-            <img src="${src}" alt="${alt}" width="${width}" height="${height}"/>
74  
-          %else:
75 71
             ${unsafe(abbv)}
76  
-          %endif
77 72
           </a>
78 73
         %else:
79 74
           <span class="${css_class}" title="${label}">${abbv}</span>

0 notes on commit 2472950

Please sign in to comment.
Something went wrong with that request. Please try again.