Get rid of editable_add_props.

It was doing too much, and also the last place where the campaigns dict
attribute was still put onto promoted links.
This commit is contained in:
bsimpson63
2012-10-17 16:16:33 -04:00
parent 152c79c62c
commit 89a65e9eb1
4 changed files with 28 additions and 21 deletions

View File

@@ -377,12 +377,14 @@ class PromoteController(ListingController):
if indx is not None:
promote.edit_campaign(l, indx, dates, bid, sr)
l = promote.editable_add_props(l)
jquery.update_campaign(*l.campaigns[indx])
renderable_campaigns = promote.get_renderable_campaigns(l)
campaign = renderable_campaigns[indx]
jquery.update_campaign(*campaign)
else:
indx = promote.new_campaign(l, dates, bid, sr)
l = promote.editable_add_props(l)
jquery.new_campaign(*l.campaigns[indx])
renderable_campaigns = promote.get_renderable_campaigns(l)
campaign = renderable_campaigns[indx]
jquery.new_campaign(*campaign)
@validatedForm(VSponsor('link_id'),
VModhash(),

View File

@@ -3146,8 +3146,8 @@ class PromoteLinkForm(Templated):
Subreddit.submit_sr_names(None))
self.default_sr = (self.subreddits[0] if self.subreddits
else g.default_sr)
# have the promo code wrap the campaigns for rendering
self.link = promote.editable_add_props(link)
self.link = promote.wrap_promoted(link)
self.campaigns = promote.get_renderable_campaigns(link)
self.promotion_log = promote.PromotionLog.get(link)
if not c.user_is_sponsor:
@@ -3351,8 +3351,9 @@ class PaymentForm(Templated):
def __init__(self, link, indx, **kw):
self.countries = [pycountry.countries.get(name=n)
for n in g.allowed_pay_countries]
self.link = promote.editable_add_props(link)
self.campaign = self.link.campaigns[indx]
self.link = promote.wrap_promoted(link)
campaigns = promote.get_renderable_campaigns(link)
self.campaign = campaigns[indx]
self.indx = indx
Templated.__init__(self, **kw)

View File

@@ -310,18 +310,22 @@ class RenderableCampaign():
for s in self.__slots__:
yield getattr(self, s)
def editable_add_props(l):
if not isinstance(l, Wrapped):
l = Wrapped(l)
l.bids = get_transactions(l)
def get_renderable_campaigns(link):
campaigns = PromoCampaign._by_link(link._id)
bids = get_transactions(link)
renderable = {}
for campaign in campaigns:
rc = RenderableCampaign(link, campaign, bids.get(campaign._id))
renderable[campaign._id] = rc
return renderable
campaigns = {}
for campaign in PromoCampaign._by_link(l._id):
campaigns[campaign._id] = RenderableCampaign(l, campaign, l.bids.get(campaign._id))
l.campaigns = campaigns
return l
def wrap_promoted(link):
if not isinstance(link, Wrapped):
link = Wrapped(link)
return link
# These could be done with relationships, but that seeks overkill as
# we never query based on user and only check per-thing

View File

@@ -84,7 +84,7 @@ ${unsafe(js.use('sponsored'))}
</p></div>
<div class="clear"></div>
</div>
<div id="campaign" style="${'display:none' if thing.link.campaigns else ''}"
<div id="campaign" style="${'display:none' if thing.campaigns else ''}"
class="campaign infotext rounded" method="post" action="/api/add_campaign">
@@ -217,7 +217,7 @@ ${unsafe(js.use('sponsored'))}
<div class="clear"></div>
</div>
<div class="existing-campaigns infotext rounded">
<table style="${'display:none' if not thing.link.campaigns else ''}">
<table style="${'display:none' if not thing.campaigns else ''}">
<tr>
<th title="${start_title}">start</th>
<th title="${end_title}">end</th>
@@ -233,7 +233,7 @@ ${unsafe(js.use('sponsored'))}
</table>
<script type="text/javascript">
$(function() {
%for campaign in sorted(thing.link.campaigns.values(), \
%for campaign in sorted(thing.campaigns.values(), \
key = lambda x: x.start_date):
$.new_campaign(${unsafe(','.join(simplejson.dumps(x) for x in campaign))});
%endfor
@@ -241,7 +241,7 @@ ${unsafe(js.use('sponsored'))}
});
</script>
<p class="error"
style="${'display:none' if thing.link.campaigns else ''}">
style="${'display:none' if thing.campaigns else ''}">
You don't have any campaigns for this link yet. You should add one.
</p>
</div>