Esempio n. 1
0
def register(name, password, email, create_wiki_account=True):
    try:
        a = Account._by_name(name)
        raise AccountExists
    except NotFound:
        a = Account(name=name, password=passhash(name, password, True))
        a.email = email

        a.confirmation_code = random_key(6)
        a.email_validated = False
        a.wiki_account = '__error__'

        a._commit()

        from r2.lib import emailer
        emailer.confirmation_email(a)

        if create_wiki_account:
            if wiki_account.valid_name(name):

                def send_wiki_failed_email():
                    emailer.wiki_failed_email(a)

                a.create_associated_wiki_account(
                    password, on_request_error=send_wiki_failed_email)
            else:
                emailer.wiki_incompatible_name_email(a)

        # Clear memoization of both with and without deleted
        clear_memo('account._by_name', Account, name.lower(), True)
        clear_memo('account._by_name', Account, name.lower(), False)
        return a
Esempio n. 2
0
def register(name, password, email):
    try:
        a = Account._by_name(name)
        raise AccountExists
    except NotFound:
        a = Account(name = name,
                    password = passhash(name, password, True))
        a.email = email

        a.confirmation_code = random_key(6)
        a.email_validated = False
        a.wiki_account = '__error__'

        a._commit()

        from r2.lib import emailer
        emailer.confirmation_email(a)

        if wiki_account.valid_name(name):
            def send_wiki_failed_email():
                emailer.wiki_failed_email(a)
            a.create_associated_wiki_account(password,
                                             on_request_error=send_wiki_failed_email)
        else:
            emailer.wiki_incompatible_name_email(a)

        # Clear memoization of both with and without deleted
        clear_memo('account._by_name', Account, name.lower(), True)
        clear_memo('account._by_name', Account, name.lower(), False)
        return a
Esempio n. 3
0
    def _new(cls, author, link, parent, body, ip, spam = False):
        c = Comment(body = body,
                    link_id = link._id,
                    sr_id = link.sr_id,
                    author_id = author._id,
                    ip = ip)

        c._spam = spam

        #these props aren't relations
        if parent:
            c.parent_id = parent._id

        c._commit()

        link._incr('num_comments', 1)

        inbox_rel = None
        if parent:
            to = Account._byID(parent.author_id)
            # only global admins can be message spammed.
            if not c._spam or to.name in g.admins:
                inbox_rel = Inbox._add(to, c, 'inbox')

        #clear that chache
        clear_memo('builder.link_comments2', link._id)

        return (c, inbox_rel)
Esempio n. 4
0
def clear_account_by_name_cache():
    q = Account._query(Account.c._deleted == (True, False), data = True)
    for account in q:
        name = account.name
        clear_memo('account._by_name', Account, name.lower(), True)
        clear_memo('account._by_name', Account, name.lower(), False)
        print "Cleared cache for %s" % account.name
Esempio n. 5
0
File: link.py Progetto: cmak/reddit
    def _submit(cls, title, url, author, sr, ip, spam = False):
        from admintools import admintools
        if url != u'self':
            try:
                l = Link._by_url(url, sr)
                raise LinkExists
            except NotFound:
                pass

        l = cls(title = title,
                url = url,
                _spam = spam,
                author_id = author._id,
                sr_id = sr._id, 
                lang = sr.lang,
                ip = ip)
        l._commit()

        clear_memo('link._by_url', Link, url, sr)
        # clear cache for lookups without sr
        clear_memo('link._by_url', Link, url, None)

        utils.worker.do(lambda: admintools.add_thing(l))

        return l
Esempio n. 6
0
File: link.py Progetto: cmak/reddit
    def _new(cls, author, link, parent, body, ip, spam = False):
        c = Comment(body = body,
                    link_id = link._id,
                    sr_id = link.sr_id,
                    author_id = author._id,
                    ip = ip)

        c._spam = spam

        #these props aren't relations
        if parent:
            c.parent_id = parent._id

        c._commit()

        link._incr('num_comments', 1)

        if parent:
            to = Account._byID(parent.author_id)
            i = Inbox._add(to, c, 'inbox')

        #clear that chache
        clear_memo('builder.link_comments2', link._id)
        from admintools import admintools
        utils.worker.do(lambda: admintools.add_thing(c))

        return c
Esempio n. 7
0
    def set_amount(cls, r, amount):
        old_amount = int(r._name)
        if old_amount != amount:
            r._name = str(amount)
            r._commit()

        #update the cache for the amount = 0 and amount = None cases
        rel = cls.rels[(r._thing1.__class__, r._thing2.__class__)]
        for a in set((old_amount, amount, None)):
            # clear memoizing around this thing's author
            if not r._thing2._loaded: r._thing2._load()
            if hasattr(r._thing2, "author_id"):
                clear_memo('report._by_author',
                           cls,
                           r._thing2.author_id,
                           amount=a)

            for t in (r._thing1, r._thing2):
                thing_key = cls._cache_prefix(rel, t.__class__,
                                              amount=a) + str(t._id)
                v = cache.get(thing_key)
                if v is not None:
                    if a == old_amount and old_amount != amount and r._id in v:
                        v.remove(r._id)
                    elif r._id not in v:
                        v.append(r._id)
                    cache.set(thing_key, v)
Esempio n. 8
0
    def set_amount(cls, r, amount):
        old_amount = int(r._name)
        if old_amount != amount:
            r._name = str(amount)
            r._commit()
            
        #update the cache for the amount = 0 and amount = None cases
        rel = cls.rels[(r._thing1.__class__, r._thing2.__class__)]
        for a in set((old_amount, amount, None)):
            # clear memoizing around this thing's author
            if not r._thing2._loaded: r._thing2._load()
            if hasattr(r._thing2, "author_id"):
                clear_memo('report._by_author', cls, r._thing2.author_id,
                           amount = a)

            for t in (r._thing1, r._thing2):
                thing_key = cls._cache_prefix(rel, t.__class__,
                                              amount = a) + str(t._id)
                v = cache.get(thing_key)
                if v is not None:
                    if a == old_amount and old_amount != amount and r._id in v:
                        v.remove(r._id)
                    elif r._id not in v:
                        v.append(r._id)
                    cache.set(thing_key, v)
Esempio n. 9
0
 def userrel_add(self, user):
     fn = getattr(self, exists_name)
     if not fn(user):
         s = relation(self, user, name)
         s._commit()
         clear_memo(all_memo_str, self)
         clear_memo(reverse_memo_str, user)
         return s
Esempio n. 10
0
 def userrel_remove(self, user):
     fn = getattr(self, exists_name)
     s = fn(user)
     if s:
         s._delete()
         clear_memo(all_memo_str, self)
         clear_memo(reverse_memo_str, user)
         return True
Esempio n. 11
0
 def userrel_add(self, user):
     fn = getattr(self, exists_name)
     if not fn(user):
         s = relation(self, user, name)
         s._commit()
         clear_memo(all_memo_str, self)
         clear_memo(reverse_memo_str, user)
         return s
Esempio n. 12
0
 def userrel_remove(self, user):
     fn = getattr(self, exists_name)
     s = fn(user)
     if s:
         s._delete()
         clear_memo(all_memo_str, self)
         clear_memo(reverse_memo_str, user)
         return True
Esempio n. 13
0
    def delete(self):
        self._deleted = True
        self._commit()
        clear_memo("account._by_name", Account, self.name.lower(), False)

        # remove from friends lists
        q = Friend._query(Friend.c._thing2_id == self._id, Friend.c._name == "friend", eager_load=True)
        for f in q:
            f._thing1.remove_friend(f._thing2)
Esempio n. 14
0
 def _new(self, name, **kw):
     tag_name = name.lower()
     try:
         tag = Tag._by_name(tag_name)
         raise TagExists
     except NotFound:
         tag = Tag(name=tag_name, **kw)
         tag._commit()
         clear_memo('tag._by_name', Tag, name.lower())
         return tag
Esempio n. 15
0
 def _new(self, name, **kw):
     tag_name = name.lower()
     try:
         tag = Tag._by_name(tag_name)
         raise TagExists
     except NotFound:
         tag = Tag(name = tag_name, **kw)
         tag._commit()
         clear_memo('tag._by_name', Tag, name.lower())
         return tag
Esempio n. 16
0
 def _new(self, name, title, lang="en", type="public", over_18=False, **kw):
     try:
         sr = Subreddit._by_name(name)
         raise SubredditExists
     except NotFound:
         sr = Subreddit(name=name, title=title, lang=lang, type=type, over_18=over_18, **kw)
         sr._commit()
         clear_memo("subreddit._by_name", Subreddit, name.lower())
         clear_memo("subreddit.subreddits", Subreddit)
         return sr
Esempio n. 17
0
    def delete(self):
        self._deleted = True
        self._commit()
        clear_memo('account._by_name', Account, self.name.lower(), False)

        #remove from friends lists
        q = Friend._query(Friend.c._thing2_id == self._id,
                          Friend.c._name == 'friend',
                          eager_load=True)
        for f in q:
            f._thing1.remove_friend(f._thing2)
Esempio n. 18
0
def register(name, password):
    try:
        a = Account._by_name(name)
        raise AccountExists
    except NotFound:
        a = Account(name = name,
                    password = passhash(name, password, True))

        a._commit()
        clear_memo('account._by_name', Account, name.lower(), False)
        return a
Esempio n. 19
0
def register(name, password):
    try:
        a = Account._by_name(name)
        raise AccountExists
    except NotFound:
        a = Account(name = name,
                    password = passhash(name, password, True))

        a._commit()
        # Clear memoization of both with and without deleted
        clear_memo('account._by_name', Account, name.lower(), True)
        clear_memo('account._by_name', Account, name.lower(), False)
        return a
Esempio n. 20
0
 def opt_in(self, msg_hash):
     """Removes recipient of the email from the opt-out list"""
     email = self.get_recipient(msg_hash)
     if email:
         o = self.opt_table
         if self.has_opted_out(email):
             sa.delete(o, o.c.email == email).execute()
             clear_memo('r2.models.mail_queue.has_opted_out',
                        email)
             clear_memo('r2.models.mail_queue.opt_count')
             return (email, True)
         else:
             return (email, False)
     return (None, False)
Esempio n. 21
0
 def opt_out(self, msg_hash):
     """Adds the recipient of the email to the opt-out list and returns
     that address."""
     email = self.get_recipient(msg_hash)
     if email:
         o = self.opt_table
         try:
             o.insert().execute({o.c.email: email, o.c.msg_hash: msg_hash})
             clear_memo('r2.models.mail_queue.has_opted_out', 
                        email)
             clear_memo('r2.models.mail_queue.opt_count')
             return (email, True)
         except sa.exceptions.SQLError:
             return (email, False)
     return (None, False)
Esempio n. 22
0
 def _new(self, name, title, lang='en', type='public', over_18=False, **kw):
     try:
         sr = Subreddit._by_name(name)
         raise SubredditExists
     except NotFound:
         sr = Subreddit(name=name,
                        title=title,
                        lang=lang,
                        type=type,
                        over_18=over_18,
                        **kw)
         sr._commit()
         clear_memo('subreddit._by_name', Subreddit, name.lower())
         clear_memo('subreddit.subreddits', Subreddit)
         return sr
Esempio n. 23
0
def register(name, password, email=None):
    try:
        a = Account._by_name(name)
        raise AccountExists
    except NotFound:
        a = Account(name=name, password=passhash(name, password, True))
        if email:
            a.email = email

        a._commit()

        # Clear memoization of both with and without deleted
        clear_memo('account._by_name', Account, name.lower(), True)
        clear_memo('account._by_name', Account, name.lower(), False)
        return a
Esempio n. 24
0
def register(name, password, email, create_wiki_account=True):
    try:
        a = Account._by_name(name)
        raise AccountExists
    except NotFound:
        a = Account(name=name, password=passhash(name, password, True))
        a.email = email

        a.confirmation_code = random_key(6)
        a.email_validated = False

        a._commit()

        from r2.lib import emailer
        emailer.confirmation_email(a)

        # Clear memoization of both with and without deleted
        clear_memo('account._by_name', Account, name.lower(), True)
        clear_memo('account._by_name', Account, name.lower(), False)
        return a
Esempio n. 25
0
    def _new(cls, author, link, parent, body, ip, spam = False, date = None):
        comment = Comment(body = body,
                          link_id = link._id,
                          sr_id = link.sr_id,
                          author_id = author._id,
                          ip = ip,
                          date = date)

        comment._spam = spam

        #these props aren't relations
        if parent:
            comment.parent_id = parent._id

        comment._commit()

        link._incr('num_comments', 1)

        inbox_rel = None
        if parent:
            to = Account._byID(parent.author_id)
            # only global admins can be message spammed.
            if not comment._spam or to.name in g.admins:
                inbox_rel = Inbox._add(to, comment, 'inbox')

        #clear that chache
        clear_memo('builder.link_comments2', link._id)

        # flag search indexer that something has changed
        tc.changed(comment)

        #update last modified
        set_last_modified(author, 'overview')
        set_last_modified(author, 'commented')
        set_last_modified(link, 'comments')

        #update the comment cache
        from r2.lib.comment_tree import add_comment
        add_comment(comment)

        return (comment, inbox_rel)
Esempio n. 26
0
    def _new(cls, author, link, parent, body, ip, spam=False, date=None):
        comment = Comment(body=body,
                          link_id=link._id,
                          sr_id=link.sr_id,
                          author_id=author._id,
                          ip=ip,
                          date=date)

        comment._spam = spam

        #these props aren't relations
        if parent:
            comment.parent_id = parent._id

        comment._commit()

        link._incr('num_comments', 1)

        inbox_rel = None
        if parent:
            to = Account._byID(parent.author_id)
            # only global admins can be message spammed.
            if not comment._spam or to.name in g.admins:
                inbox_rel = Inbox._add(to, comment, 'inbox')

        #clear that chache
        clear_memo('builder.link_comments2', link._id)

        # flag search indexer that something has changed
        tc.changed(comment)

        #update last modified
        set_last_modified(author, 'overview')
        set_last_modified(author, 'commented')
        set_last_modified(link, 'comments')

        #update the comment cache
        from r2.lib.comment_tree import add_comment
        add_comment(comment)

        return (comment, inbox_rel)
Esempio n. 27
0
def register(name, password, email, create_wiki_account=True):
    try:
        a = Account._by_name(name)
        raise AccountExists
    except NotFound:
        a = Account(name = name,
                    password = passhash(name, password, True))
        a.email = email

        a.confirmation_code = random_key(6)
        a.email_validated = False

        a._commit()

        from r2.lib import emailer
        emailer.confirmation_email(a)

        # Clear memoization of both with and without deleted
        clear_memo('account._by_name', Account, name.lower(), True)
        clear_memo('account._by_name', Account, name.lower(), False)
        return a
Esempio n. 28
0
    def _new(cls, author, link, parent, body, ip, spam=False, date=None):
        comment = Comment(body=body,
                          link_id=link._id,
                          sr_id=link.sr_id,
                          author_id=author._id,
                          ip=ip,
                          date=date)

        comment._spam = spam

        #these props aren't relations
        if parent:
            comment.parent_id = parent._id

        comment._commit()

        link._incr('num_comments', 1)

        inbox_rel = comment._send_post_notifications(link, comment, parent)

        #clear that chache
        clear_memo('builder.link_comments2', link._id)

        # flag search indexer that something has changed
        tc.changed(comment)

        #update last modified
        set_last_modified(author, 'overview')
        set_last_modified(author, 'commented')
        set_last_modified(link, 'comments')

        #update the comment cache
        from r2.lib.comment_tree import add_comment
        add_comment(comment)

        return (comment, inbox_rel)
Esempio n. 29
0
    def _new(cls, author, link, parent, body, ip, spam = False, date = None):
        comment = Comment(body = body,
                          link_id = link._id,
                          sr_id = link.sr_id,
                          author_id = author._id,
                          ip = ip,
                          date = date)

        comment._spam = spam

        #these props aren't relations
        if parent:
            comment.parent_id = parent._id

        comment._commit()

        link._incr('num_comments', 1)

        inbox_rel = comment._send_post_notifications(link, comment, parent)

        #clear that chache
        clear_memo('builder.link_comments2', link._id)

        # flag search indexer that something has changed
        tc.changed(comment)

        #update last modified
        set_last_modified(author, 'overview')
        set_last_modified(author, 'commented')
        set_last_modified(link, 'comments')

        #update the comment cache
        from r2.lib.comment_tree import add_comment
        add_comment(comment)

        return (comment, inbox_rel)
Esempio n. 30
0
def set_promoted(link_names):
    # caller is assumed to execute me inside a lock if necessary
    g.permacache.set(promoted_memo_key, link_names)

    clear_memo(promoted_memo_key)
Esempio n. 31
0
def set_promoted(link_names):
    # caller is assumed to execute me inside a lock if necessary
    g.permacache.set(promoted_memo_key, link_names)

    clear_memo(promoted_memo_key)