Exemple #1
0
    def GET_report(self, start, end, link_text=None, owner=None):
        now = datetime.now(g.tz).replace(hour=0, minute=0, second=0,
                                         microsecond=0)
        end = end or now - timedelta(days=1)
        start = start or end - timedelta(days=7)

        links = []
        bad_links = []
        owner_name = owner.name if owner else ''

        if owner:
            promo_weights = PromotionWeights.get_campaigns(start, end,
                                                           author_id=owner._id)
            campaign_ids = [pw.promo_idx for pw in promo_weights]
            campaigns = PromoCampaign._byID(campaign_ids, data=True)
            link_ids = {camp.link_id for camp in campaigns.itervalues()}
            links.extend(Link._byID(link_ids, data=True, return_dict=False))

        if link_text is not None:
            id36s = link_text.replace(',', ' ').split()
            try:
                links_from_text = Link._byID36(id36s, data=True)
            except NotFound:
                links_from_text = {}

            bad_links = [id36 for id36 in id36s if id36 not in links_from_text]
            links.extend(links_from_text.values())

        content = PromoteReport(links, link_text, owner_name, bad_links, start,
                                end)
        if c.render_style == 'csv':
            return content.as_csv()
        else:
            return PromotePage(title=_("sponsored link report"),
                               content=content).render()
Exemple #2
0
    def rollback_account_votes(self, account):
        query = LinkVotesByAccount._cf.xget(account._id36)
        for thing_id, vote_state in query:
            link = Link._byID36(thing_id)
            if int(vote_state) == int(
                    Vote.SERIALIZED_DIRECTIONS[Vote.DIRECTIONS.onon]):  # 3
                link._incr("_ups", -1)
                link._incr("_downs", -1)
            elif int(vote_state) == int(
                    Vote.SERIALIZED_DIRECTIONS[Vote.DIRECTIONS.onoff]):  # 4
                link._incr("_ups", -1)
            elif int(vote_state) == int(
                    Vote.SERIALIZED_DIRECTIONS[Vote.DIRECTIONS.offon]):  # 5
                link._incr("_downs", -1)

        query = CommentVotesByAccount._cf.xget(account._id36)
        for thing_id, vote_state in query:
            comment = Comment._byID36(thing_id)
            if int(vote_state) == int(
                    Vote.SERIALIZED_DIRECTIONS[Vote.DIRECTIONS.onon]):  # 3
                comment._incr("_ups", -1)
                comment._incr("_downs", -1)
            elif int(vote_state) == int(
                    Vote.SERIALIZED_DIRECTIONS[Vote.DIRECTIONS.onoff]):  # 4
                comment._incr("_ups", -1)
            elif int(vote_state) == int(
                    Vote.SERIALIZED_DIRECTIONS[Vote.DIRECTIONS.offon]):  # 5
                comment._incr("_downs", -1)
Exemple #3
0
    def GET_report(self, start, end, link_text=None, owner=None):
        now = datetime.now(g.tz).replace(hour=0, minute=0, second=0,
                                         microsecond=0)
        end = end or now - timedelta(days=1)
        start = start or end - timedelta(days=7)

        links = []
        bad_links = []
        owner_name = owner.name if owner else ''

        if owner:
            promo_weights = PromotionWeights.get_campaigns(start, end,
                                                           author_id=owner._id)
            campaign_ids = [pw.promo_idx for pw in promo_weights]
            campaigns = PromoCampaign._byID(campaign_ids, data=True)
            link_ids = {camp.link_id for camp in campaigns.itervalues()}
            links.extend(Link._byID(link_ids, data=True, return_dict=False))

        if link_text is not None:
            id36s = link_text.replace(',', ' ').split()
            try:
                links_from_text = Link._byID36(id36s, data=True)
            except NotFound:
                links_from_text = {}

            bad_links = [id36 for id36 in id36s if id36 not in links_from_text]
            links.extend(links_from_text.values())

        content = PromoteReport(links, link_text, owner_name, bad_links, start,
                                end)
        if c.render_style == 'csv':
            return content.as_csv()
        else:
            return PromotePage(title=_("sponsored link report"),
                               content=content).render()
Exemple #4
0
    def get(cls, dfp_creative_id):
        try:
            columns = cls._byID(cls._row_key(dfp_creative_id))._values()
            id36 = columns.keys()[0]
        except tdb_cassandra.NotFound:
            return None

        return Link._byID36(id36, data=True, return_dict=False)
Exemple #5
0
    def get(cls, dfp_creative_id):
        try:
            columns = cls._byID(cls._row_key(dfp_creative_id))._values()
            id36 = columns.keys()[0]
        except tdb_cassandra.NotFound:
            return None

        return Link._byID36(id36, data=True, return_dict=False)
Exemple #6
0
 def _get_selfserve_links(self, count):
     links = Subreddit._by_name(g.advertising_links_sr).get_links(
         'new', 'all')
     items = Link._by_fullname(links, data=True, return_dict=False)
     id36s = map(
         lambda x: self.advertising_link_id36_re.match(x.url).group(1),
         items)
     ad_links = Link._byID36(id36s, return_dict=False, data=True)
     return wrap_links(ad_links, num=count)
    def __init__(self, *args, **kwargs):
        self.prices = {
            "gold_month_price": g.gold_month_price,
            "gold_year_price": g.gold_year_price,
        }

        self.partners = GoldPartner.get_all()
        self.categories = set()
        self.giveaways = []

        # batch-lookup the Links and Subreddits for discussions
        id36s = [p.discussion_id36 for p in self.partners if p.discussion_id36]
        links = Link._byID36(id36s, data=True)
        subreddits = Subreddit._byID([link.sr_id for link in links.values()],
                                     data=True)

        for partner in self.partners:
            if partner.category:
                self.categories.add(partner.category)

            extra_classes = partner.css_classes
            if partner.is_new:
                extra_classes.append('new')
            partner.extra_classes = ' '.join(extra_classes)

            if partner.giveaway_desc:
                self.giveaways.append('{0}: {1}'
                                      .format(partner.name,
                                              partner.giveaway_desc))

            if partner.discussion_id36:
                link = links[partner.discussion_id36]
                subreddit = subreddits[link.sr_id]
                partner.discussion_url = link.make_permalink(subreddit)
                partner.discussion_num_comments = link.num_comments
            else:
                partner.discussion_url = None
                partner.discussion_num_comments = None

        self.categories = sorted(self.categories)

        if c.user_is_loggedin:
            self.existing_codes = GoldPartnerDealCode.get_codes_for_user(c.user)
        else:
            self.existing_codes = []
        BoringPage.__init__(self, *args, **kwargs)
Exemple #8
0
    def __init__(self, *args, **kwargs):
        self.prices = {
            "gold_month_price": g.gold_month_price,
            "gold_year_price": g.gold_year_price,
        }

        self.partners = GoldPartner.get_all()
        self.categories = set()
        self.giveaways = []

        # batch-lookup the Links and Subreddits for discussions
        id36s = [p.discussion_id36 for p in self.partners if p.discussion_id36]
        links = Link._byID36(id36s, data=True)
        subreddits = Subreddit._byID([link.sr_id for link in links.values()],
                                     data=True)

        for partner in self.partners:
            if partner.category:
                self.categories.add(partner.category)

            extra_classes = partner.css_classes
            if partner.is_new:
                extra_classes.append('new')
            partner.extra_classes = ' '.join(extra_classes)

            if partner.giveaway_desc:
                self.giveaways.append('{0}: {1}'.format(
                    partner.name, partner.giveaway_desc))

            if partner.discussion_id36:
                link = links[partner.discussion_id36]
                subreddit = subreddits[link.sr_id]
                partner.discussion_url = link.make_permalink(subreddit)
                partner.discussion_num_comments = link.num_comments
            else:
                partner.discussion_url = None
                partner.discussion_num_comments = None

        self.categories = sorted(self.categories)

        if c.user_is_loggedin:
            self.existing_codes = GoldPartnerDealCode.get_codes_for_user(
                c.user)
        else:
            self.existing_codes = []
        BoringPage.__init__(self, *args, **kwargs)
Exemple #9
0
def validate_link(url,whitelist=False):
    if url:
        url=sanitize_url(url)
        if url:
	    if whitelist and domain(url) not in DOMAIN_WHITELIST:
	        print "Domain %s not in whitelist." % domain(url)
		return False
            try:
                lbu = LinksByUrl._byID(LinksByUrl._key_from_url(url))
            except tdb_cassandra.NotFound:
                return url
            link_id36s = lbu._values()
	    links = Link._byID36(link_id36s, data=True, return_dict=False)
	    links = [l for l in links if not l._deleted]
	    if len(links)==0:
	        return url
	    print "Link %s exists..." % url
    return False 
 def _get_selfserve_links(self, count):
     links = Subreddit._by_name(g.advertising_links_sr).get_links('new', 'all')
     items = Link._by_fullname(links, data=True, return_dict=False)
     id36s = map(lambda x: self.advertising_link_id36_re.match(x.url).group(1), items)
     ad_links = Link._byID36(id36s, return_dict=False, data=True)
     return wrap_links(ad_links, num=count)
    def _run_realtime_email_queue(msgs, chan):

        if time.time() - run_realtime_email_queue.last_got_accounts > 600:
            #-- Pick up a fresh list of accounts, if we havenn't done so recently, in case settings change
            if g.email_debug:
                g.log.info('Getting accounts')
            run_realtime_email_queue.accounts = Account._query(Account.c.email != None, sort = asc('_date'), data=True)
            run_realtime_email_queue.last_got_accounts = time.time()
        
        for msg in msgs:
            # msg.body contains the unique name of the post, comment or message, e.g. 't1_2n'(comment #95) or 't6_q'(post #26)
            fullname = str(msg.body)
            fullname_type = fullname[0:2]
            id36 = fullname[3:]
            if g.email_debug:
                g.log.info('msg: %r', fullname)
            howold = (datetime.datetime.now() - msg.timestamp).total_seconds() 
            if  howold < 110:
                # Wait until this item is 2 minutes old, to allow time for corrections
                if g.email_debug:
                    g.log.info('waiting for a moment')
                time.sleep(120 - howold)

            is_com = is_post = False
            thing = link = comment = None
            if fullname_type == 't1':
                # a comment
                is_com = True
                comment = Comment._byID36(id36, data=True)
                if g.email_debug:
                    g.log.info('comment: %r', comment.body)
                thing = comment
                author = Account._byID(comment.author_id, True)
                kind = Email.Kind.REALTIME_COMMENT
                template = 'email_realtime_comment.html'
                link = Link._byID(comment.link_id, data=True)  
                subject = 'Re: %s' % link.title
                sr_id = comment.sr_id
                
            elif fullname_type == 't6':
                # a post/link
                is_post = True
                link = Link._byID36(id36, data=True)
                if g.email_debug:
                    g.log.info('post: %r', link.title)
                thing = link
                author = Account._byID(link.author_id, True)
                kind = Email.Kind.REALTIME_POST
                template = 'email_realtime_post.html'
                subject = link.title
                sr_id = link.sr_id
                
            else:
                return
            
            sr = Subreddit._byID(sr_id, data=True)
            
            subject = "[%s] %s" % (sr.name, subject)
            
            for account in run_realtime_email_queue.accounts:
                
                sub = sr.get_subscriber(account)
                
                if is_com: 
                    if hasattr(sub,'email_comments') and sub.email_comments:
                        if g.email_debug:
                            g.log.info('  account %r: we should send this comment, because of the space setting', account.name)
                        whysend = 'space'
                    else:
                        email_thread = Link._somethinged(SaveHide, account, link, 'email')[account,link,'email']
                        if email_thread:
                            if g.email_debug:
                                g.log.info('  account %r: we should send this comment, because of the thread setting', account.name)
                            whysend = 'thread'
                        else:    
                            continue
                    
                elif is_post:
                    if hasattr(sub,'email_posts') and sub.email_posts:
                        if g.email_debug:
                            g.log.info('  account %r: we should send this post', account.name)
                        whysend = 'space'
                    else:
                        continue

                if not ('session' in locals()):
                    # Open the SMTP session
                    if g.email_debug:
                        g.log.info('Opening SMTP session')
                    session = open_smtp_session()

                # Render the template
                html_email_template = g.mako_lookup.get_template(template)
                html_body = html_email_template.render(link=link, comment=comment, thing=thing, account=account, sub=sub, whysend=whysend)
            
                from_email = '"%s" <%s>' % (g.realtime_email_from_name, g.share_reply,)
                send_html_email(account.email, g.share_reply, subject, html_body, from_full=from_email, session=session)
                if g.email_debug:
                    g.log.info('    sent to %r at %r', account.name, account.email)

        if g.email_debug:
            g.log.info('Done running queue')

        if 'session' in locals():
            # Close the session.
            session.quit()