diff --git a/r2/r2/controllers/api.py b/r2/r2/controllers/api.py index b9462d85c..0dda7b810 100644 --- a/r2/r2/controllers/api.py +++ b/r2/r2/controllers/api.py @@ -61,7 +61,6 @@ from datetime import datetime, timedelta from md5 import md5 from r2.lib.promote import promote, unpromote, get_promoted -from r2.lib.promote import promote_builder_wrapper def link_listing_by_url(url, count = None): try: @@ -1286,8 +1285,8 @@ class ApiController(RedditController): mid_margin = VCssMeasure('mid_margin'), links = VFullNames('links')) def POST_fetch_links(self, res, num_margin, mid_margin, links): - b = IDBuilder([l._fullname for l in links], - wrap = promote_builder_wrapper(ListingController.builder_wrapper)) + b = IDBuilder([l._fullname for l in links], + wrap = ListingController.builder_wrapper) l = OrganicListing(b) l.num_margin = num_margin l.mid_margin = mid_margin @@ -1348,12 +1347,16 @@ class ApiController(RedditController): res._focus('title') elif res._chk_errors((errors.NO_URL,errors.BAD_URL)): res._focus('url') + elif res._chk_error(errors.ALREADY_SUB) and (not l or url != l.url): + #if url == l.url, we're just editting something else + res._focus('url') elif res._chk_error(errors.SUBREDDIT_NOEXIST): res._focus('sr') elif expire == 'expirein' and res._chk_error(errors.BAD_NUMBER): res._focus('timelimitlength') elif l: l.title = title + old_url = l.url l.url = url l.promoted_subscribersonly = subscribers_only @@ -1363,25 +1366,19 @@ class ApiController(RedditController): l.promote_until = None elif expire == 'expirein' and timelimitlength and timelimittype: l.promote_until = timefromnow("%d %s" % (timelimitlength, timelimittype)) - + l._commit() + l.update_url_cache(old_url) res._redirect('/promote/edit_promo/%s' % to36(l._id)) else: - l = Link(title = title, - url = url, - author_id = c.user._id, - sr_id = sr._id, - lang = sr.lang, - ip = ip) + l = Link._submit(title, url, c.user, sr, ip, False) if expire == 'expirein' and timelimitlength and timelimittype: promote_until = timefromnow("%d %s" % (timelimitlength, timelimittype)) else: promote_until = None - l._commit() - promote(l, subscribers_only = subscribers_only, promote_until = promote_until, disable_comments = disable_comments) diff --git a/r2/r2/controllers/listingcontroller.py b/r2/r2/controllers/listingcontroller.py index 0e21d2fa7..b2675f587 100644 --- a/r2/r2/controllers/listingcontroller.py +++ b/r2/r2/controllers/listingcontroller.py @@ -35,7 +35,6 @@ from r2.lib.strings import Score from r2.lib import organic from r2.lib.solrsearch import SearchQuery from r2.lib.utils import iters, check_cheating -from r2.lib.promote import promote_builder_wrapper from admin import admin_profile_query @@ -148,9 +147,15 @@ class ListingController(RedditController): def builder_wrapper(thing): w = Wrapped(thing) - if c.user.pref_compress and isinstance(thing, Link): - w.render_class = LinkCompressed - w.score_fmt = Score.points + if isinstance(thing, Link): + if thing.promoted: + w = Wrapped(thing) + w.render_class = PromotedLink + w.rowstyle = 'promoted link' + + elif c.user.pref_compress: + w.render_class = LinkCompressed + w.score_fmt = Score.points return w @@ -193,8 +198,7 @@ class HotController(FixListing, ListingController): l = min(len(o_links) - 3, 8) disp_links = [o_links[(i + pos) % len(o_links)] for i in xrange(-2, l)] - b = IDBuilder(disp_links, - wrap = promote_builder_wrapper(self.builder_wrapper)) + b = IDBuilder(disp_links, wrap = self.builder_wrapper) o = OrganicListing(b, org_links = o_links, visible_link = o_links[pos], diff --git a/r2/r2/controllers/promotecontroller.py b/r2/r2/controllers/promotecontroller.py index bb096987d..1c97cef8f 100644 --- a/r2/r2/controllers/promotecontroller.py +++ b/r2/r2/controllers/promotecontroller.py @@ -28,7 +28,7 @@ from r2.controllers import ListingController from r2.controllers.reddit_base import RedditController -from r2.lib.promote import get_promoted, promote_builder_wrapper +from r2.lib.promote import get_promoted from r2.lib.utils import timetext from datetime import datetime @@ -67,8 +67,7 @@ class PromoteController(RedditController): sr = Subreddit._byID(link.sr_id) names = [link._fullname] - builder = IDBuilder(names, - wrap = promote_builder_wrapper(ListingController.builder_wrapper)) + builder = IDBuilder(names, wrap = ListingController.builder_wrapper) listing = LinkListing(builder, show_nums = False, nextprev = False) rendered = listing.listing().render() diff --git a/r2/r2/lib/pages/pages.py b/r2/r2/lib/pages/pages.py index 372a7b401..6328b1308 100644 --- a/r2/r2/lib/pages/pages.py +++ b/r2/r2/lib/pages/pages.py @@ -35,7 +35,6 @@ from r2.lib.menus import SubredditButton, SubredditMenu, menu from r2.lib.strings import plurals, rand_strings, strings from r2.lib.utils import title_to_url, query_string, UrlParser from r2.lib.template_helpers import add_sr, get_domain -from r2.lib.promote import promote_builder_wrapper import sys datefmt = _force_utf8(_('%d %b %Y')) @@ -124,7 +123,7 @@ class Reddit(Wrapped): ps.append(SideBox(_('Submit a link'), '/submit', 'submit', sr_path = True, - subtitles = [_('to anything interesting: news article, blog entry, video, picture...')], + subtitles = [strings.submit_box_text], show_cover = True)) if self.create_reddit_box: @@ -363,7 +362,8 @@ class BoringPage(Reddit): class FormPage(BoringPage): """intended for rendering forms with no rightbox needed or wanted""" def __init__(self, pagename, show_sidebar = False, *a, **kw): - BoringPage.__init__(self, pagename, show_sidebar = show_sidebar, *a, **kw) + BoringPage.__init__(self, pagename, show_sidebar = show_sidebar, + *a, **kw) class LoginPage(BoringPage): @@ -383,7 +383,8 @@ class LoginPage(BoringPage): class Login(Wrapped): """The two-unit login and register form.""" def __init__(self, user_reg = '', user_login = '', dest=''): - Wrapped.__init__(self, user_reg = user_reg, user_login = user_login, dest = dest) + Wrapped.__init__(self, user_reg = user_reg, user_login = user_login, + dest = dest) class SearchPage(BoringPage): @@ -418,7 +419,7 @@ class LinkInfoPage(Reddit): # TODO: temp hack until we find place for builder_wrapper from r2.controllers.listingcontroller import ListingController link_builder = IDBuilder(link._fullname, - wrap = promote_builder_wrapper(ListingController.builder_wrapper)) + wrap = ListingController.builder_wrapper) # link_listing will be the one-element listing at the top self.link_listing = LinkListing(link_builder, nextprev=False).listing() @@ -429,15 +430,16 @@ class LinkInfoPage(Reddit): link_title = ((self.link.title) if hasattr(self.link, 'title') else '') if comment: author = Account._byID(comment.author_id, data=True).name - title = _("%(author)s comments on %(title)s") % dict(author=author, title=_force_unicode(link_title)) + params = {'author' : author, 'title' : _force_unicode(link_title)} + title = strings.permalink_title % params else: - title = _("%(title)s : %(site)s") % dict(title=_force_unicode(link_title), site = c.site.name) + params = {'title':_force_unicode(link_title), 'site' : c.site.name} + title = strings.link_info_title % params Reddit.__init__(self, title = title, *a, **kw) def build_toolbars(self): base_path = "/%s/%s/" % (self.link._id36, title_to_url(self.link.title)) - if isinstance(base_path, unicode): - base_path = base_path.encode('utf-8') + base_path = _force_utf8(base_path) def info_button(name): return NamedButton(name, dest = '/%s%s' % (name, base_path), aliases = ['/%s/%s' % (name, self.link._id36)]) @@ -1206,4 +1208,3 @@ class PromoteLinkForm(Wrapped): timedeltatext = timedeltatext, listing = listing, *a, **kw) - diff --git a/r2/r2/lib/promote.py b/r2/r2/lib/promote.py index 5614fa56e..7e72eefe6 100644 --- a/r2/r2/lib/promote.py +++ b/r2/r2/lib/promote.py @@ -118,6 +118,7 @@ def get_promoted_slow(): return link_names +#deprecated def promote_builder_wrapper(alternative_wrapper): def wrapper(thing): if isinstance(thing, Link) and thing.promoted: diff --git a/r2/r2/lib/strings.py b/r2/r2/lib/strings.py index 3e80d7268..78cb8ddb1 100644 --- a/r2/r2/lib/strings.py +++ b/r2/r2/lib/strings.py @@ -103,7 +103,11 @@ string_dict = dict( no_imports = _('@imports are not allowed'), invalid_property_list = _('invalid CSS property list "%(proplist)s"'), unknown_rule_type = _('unknown CSS rule type "%(ruletype)s"') - ) + ), + + submit_box_text = _('to anything interesting: news article, blog entry, video, picture...'), + permalink_title = _("%(author)s comments on %(title)s"), + link_info_title = _("%(title)s : %(site)s"), ) diff --git a/r2/r2/lib/wrapped.py b/r2/r2/lib/wrapped.py index c213c608a..6f1013c7f 100644 --- a/r2/r2/lib/wrapped.py +++ b/r2/r2/lib/wrapped.py @@ -22,6 +22,7 @@ from filters import unsafe from utils import storage +from itertools import chain import sys sys.setrecursionlimit(500) @@ -69,7 +70,7 @@ class Wrapped(object): debug = g.template_debug template = None if self.__class__ == Wrapped: - for lookup in self.lookups + (self.render_class,): + for lookup in chain(self.lookups, (self.render_class,)): try: template = tpm.get(lookup, style, cache = not debug) except AttributeError: diff --git a/r2/r2/models/link.py b/r2/r2/models/link.py index 1cebb9021..38aa05417 100644 --- a/r2/r2/models/link.py +++ b/r2/r2/models/link.py @@ -90,6 +90,17 @@ class Link(Thing, Printable): link_ids.append(self._id) g.permacache.set(key, link_ids) + def update_url_cache(self, old_url): + """Remove the old url from the by_url cache then update the + cache with the new url.""" + if old_url != 'self': + key = self.by_url_key(old_url) + link_ids = g.permacache.get(key) or [] + while self._id in link_ids: + link_ids.remove(self._id) + g.permacache.set(key, link_ids) + self.set_url_cache() + @property def already_submitted_link(self): return self.make_permalink_slow() + '?already_submitted=true' @@ -247,11 +258,16 @@ class Link(Thing, Printable): clicked = {} for item in wrapped: - show_media = (c.user.pref_media == 'on' or - (item.promoted and item.has_thumbnail - and c.user.pref_media != 'off') or - (c.user.pref_media == 'subreddit' and - item.subreddit.show_media)) + show_media = False + if c.user.pref_media == 'on': + show_media = True + elif c.user.pref_media == 'subreddit' and item.subreddit.show_media: + show_media = True + elif (item.promoted + and item.has_thumbnail + and c.user.pref_media != 'off' + and not c.user.pref_compress): + show_media = True if not show_media: item.thumbnail = "" @@ -293,6 +309,7 @@ class Link(Thing, Printable): item.nofollow = True else: item.nofollow = False + if c.user_is_loggedin: incr_counts(wrapped) @@ -334,7 +351,6 @@ class PromotedLink(Link): for item in wrapped: # these are potentially paid for placement item.nofollow = True - if item.promoted_by in promoted_by_accounts: item.promoted_by_name = promoted_by_accounts[item.promoted_by].name else: diff --git a/r2/r2/public/static/reddit.css b/r2/r2/public/static/reddit.css index 55f0cdefd..28e6fcc66 100644 --- a/r2/r2/public/static/reddit.css +++ b/r2/r2/public/static/reddit.css @@ -241,7 +241,7 @@ input[type=checkbox], input[type=radio] { margin-top: .4em; } .content { z-index: 1; - margin: 7px 0px 0px 5px; + margin: 7px 5px 0px 5px; } .content .spacer { margin-bottom: 5px } @@ -700,7 +700,7 @@ a.star { text-decoration: none; color: #ff8b60 } /* links */ -.link { margin: 0; margin-bottom: 8px; } +.link { margin: 0; margin-bottom: 8px; padding-left: 3px; } .link .score {text-align: center; color: #c6c6c6;} .link .title {font-size:medium; font-weight: normal; margin-bottom: 1px;} diff --git a/r2/r2/templates/promotelinkform.html b/r2/r2/templates/promotelinkform.html index c12858b75..cbf35f99a 100644 --- a/r2/r2/templates/promotelinkform.html +++ b/r2/r2/templates/promotelinkform.html @@ -54,6 +54,7 @@ ${error_field("NO_URL", "span")} ${error_field("BAD_URL", "span")} + ${error_field("ALREADY_SUB", "span")} @@ -151,4 +152,4 @@ ${error_field("RATELIMIT", "span")} - \ No newline at end of file +