Example #1
0
    def revise(self, content, previous = None, author=None, force=False, reason=None):
        if content is None:
            content = ""
        if self.content == content:
            return
        force = True if previous is None else force
        max_length = special_length_restrictions_bytes.get(self.name, MAX_PAGE_LENGTH_BYTES)
        if len(content) > max_length:
            raise ContentLengthError(max_length)
        
        revision = getattr(self, 'revision', None)
        
        if not force and (revision and previous != revision):
            if previous:
                origcontent = WikiRevision.get(previous, pageid=self._id).content
            else:
                origcontent = ''
            try:
                content = threewaymerge(origcontent, content, self.content)
            except ConflictException as e:
                e.new_id = revision
                raise e
        
        wr = WikiRevision.create(self._id, content, author, reason)
        self.content = content
        self.last_edit_by = author
        self.last_edit_date = wr.date
        self.revision = str(wr._id)
        self._commit()

        LastModified.touch(self._fullname, "Edit")

        return wr
Example #2
0
def add_comment_tree(comments):
    # update the comment cache
    add_comments(comments)
    # update last modified
    links = Link._byID(list(set(com.link_id for com in tup(comments))), data=True, return_dict=False)
    for link in links:
        set_last_modified(link, "comments")
        LastModified.touch(link._fullname, "Comments")
Example #3
0
    def _commit(self, *a, **kw):
        assert self._id == self._rowkey(self.thing1_id, self.thing2_id)

        retval = ThingBase._commit(self, *a, **kw)

        from r2.models.last_modified import LastModified
        fullname = self._thing1_cls._fullname_from_id36(self.thing1_id)
        LastModified.touch(fullname, self._cf.column_family)

        return retval
Example #4
0
def update_vote_lookups(user, thing, direction):
    """Store info about the existence of this vote (before processing)."""
    # set the vote in memcached so the UI gets updated immediately
    key = prequeued_vote_key(user, thing)
    grace_period = int(g.vote_queue_grace_period.total_seconds())
    direction = Vote.serialize_direction(direction)
    g.cache.set(key, direction, time=grace_period+1)

    # update LastModified immediately to help us cull prequeued_vote lookups
    rel_cls = VotesByAccount.rel(thing.__class__)
    LastModified.touch(user._fullname, rel_cls._last_modified_name)
Example #5
0
    def update_last_visit(self, current_time):
        from admintools import apply_updates

        apply_updates(self)

        prev_visit = LastModified.get(self._fullname, "Visit")
        if prev_visit and current_time - prev_visit < timedelta(days=1):
            return

        g.log.debug("Updating last visit for %s from %s to %s" % (self.name, prev_visit, current_time))

        LastModified.touch(self._fullname, "Visit")
Example #6
0
    def update_last_visit(self, current_time):
        from admintools import apply_updates

        apply_updates(self)

        #prev_visit = getattr(self, 'last_visit', None)
        prev_visit = last_visit(self)

        if prev_visit and current_time - prev_visit < timedelta(1):
            return

        g.log.debug ("Updating last visit for %s from %s to %s" %
                    (self.name, prev_visit, current_time))
        set_last_visit(self)

        LastModified.touch(self._fullname, "Visit")
Example #7
0
    def update_last_visit(self, current_time):
        from admintools import apply_updates

        apply_updates(self)

        prev_visit = LastModified.get(self._fullname, "Visit")
        if prev_visit and current_time - prev_visit < timedelta(days=1):
            return

        g.log.debug("Updating last visit for %s from %s to %s" %
                    (self.name, prev_visit, current_time))

        LastModified.touch(self._fullname, "Visit")

        self.last_visit = int(time.time())
        self._commit()
Example #8
0
def handle_vote(user,
                thing,
                dir,
                ip,
                organic,
                cheater=False,
                foreground=False):
    from r2.lib.db import tdb_sql
    from sqlalchemy.exc import IntegrityError
    try:
        v = Vote.vote(user, thing, dir, ip, organic, cheater=cheater)
    except (tdb_sql.CreationError, IntegrityError):
        g.log.error("duplicate vote for: %s" % str((user, thing, dir)))
        return

    timestamps = []
    if isinstance(thing, Link):
        new_vote(v, foreground=foreground)

        #update the modified flags
        if user._id == thing.author_id:
            timestamps.append('Overview')
            timestamps.append('Submitted')
            #update sup listings
            sup.add_update(user, 'submitted')

            #update sup listings
            if dir:
                timestamps.append('Liked')
                sup.add_update(user, 'liked')
            elif dir is False:
                timestamps.append('Disliked')
                sup.add_update(user, 'disliked')

    elif isinstance(thing, Comment):
        #update last modified
        if user._id == thing.author_id:
            timestamps.append('Overview')
            timestamps.append('Commented')
            #update sup listings
            sup.add_update(user, 'commented')

    for timestamp in timestamps:
        set_last_modified(user, timestamp.lower())
    LastModified.touch(user._fullname, timestamps)
Example #9
0
def handle_vote(user, thing, dir, ip, organic,
                cheater=False, foreground=False, timer=None):
    if timer is None:
        timer = SimpleSillyStub()

    from r2.lib.db import tdb_sql
    from sqlalchemy.exc import IntegrityError
    try:
        v = Vote.vote(user, thing, dir, ip, organic, cheater = cheater,
                      timer=timer)
    except (tdb_sql.CreationError, IntegrityError):
        g.log.error("duplicate vote for: %s" % str((user, thing, dir)))
        return

    timestamps = []
    if isinstance(thing, Link):
        new_vote(v, foreground=foreground, timer=timer)

        #update the modified flags
        if user._id == thing.author_id:
            timestamps.append('Overview')
            timestamps.append('Submitted')
            #update sup listings
            sup.add_update(user, 'submitted')

            #update sup listings
            if dir:
                sup.add_update(user, 'liked')
            elif dir is False:
                sup.add_update(user, 'disliked')

    elif isinstance(thing, Comment):
        #update last modified
        if user._id == thing.author_id:
            timestamps.append('Overview')
            timestamps.append('Commented')
            #update sup listings
            sup.add_update(user, 'commented')

    timer.intermediate("sup")

    for timestamp in timestamps:
        set_last_modified(user, timestamp.lower())
    LastModified.touch(user._fullname, timestamps)
    timer.intermediate("last_modified")
Example #10
0
    def update_last_visit(self, current_time):
        from admintools import apply_updates

        timer = g.stats.get_timer("account.update_last_visit")
        timer.start()

        apply_updates(self, timer)

        prev_visit = LastModified.get(self._fullname, "Visit")
        timer.intermediate("get_last_modified")

        if prev_visit and current_time - prev_visit < timedelta(days=1):
            timer.intermediate("set_last_modified.noop")
            timer.stop()
            return

        LastModified.touch(self._fullname, "Visit")
        timer.intermediate("set_last_modified.done")
        timer.stop()
Example #11
0
    def update_last_visit(self, current_time):
        from admintools import apply_updates

        timer = g.stats.get_timer("account.update_last_visit")
        timer.start()

        apply_updates(self, timer)

        prev_visit = LastModified.get(self._fullname, "Visit")
        timer.intermediate("get_last_modified")

        if prev_visit and current_time - prev_visit < timedelta(days=1):
            timer.intermediate("set_last_modified.noop")
            timer.stop()
            return

        LastModified.touch(self._fullname, "Visit")
        timer.intermediate("set_last_modified.done")
        timer.stop()
Example #12
0
    def create(cls, thing1, thing2s, **kw):
        """Create a relationship between thing1 and thing2s.

        If there are any other views of this data, they will be updated as
        well.

        Takes kwargs which can be used by views
        or value_for to get additional information.

        """
        thing2s = tup(thing2s)
        values = {thing2._id36 : cls.value_for(thing1, thing2, **kw)
                  for thing2 in thing2s}
        cls._cf.insert(thing1._id36, values, ttl=cls._ttl)

        for view in cls._views:
            view.create(thing1, thing2s, **kw)

        if cls._write_last_modified:
            from r2.models.last_modified import LastModified
            LastModified.touch(thing1._fullname, cls._last_modified_name)
Example #13
0
    def revise(self,
               content,
               previous=None,
               author=None,
               force=False,
               reason=None):
        if content is None:
            content = ""
        if self.content == content:
            return
        force = True if previous is None else force
        max_length = special_length_restrictions_bytes.get(
            self.name, MAX_PAGE_LENGTH_BYTES)
        if len(content) > max_length:
            raise ContentLengthError(max_length)

        revision = getattr(self, 'revision', None)

        if not force and (revision and previous != revision):
            if previous:
                origcontent = WikiRevision.get(previous,
                                               pageid=self._id).content
            else:
                origcontent = ''
            try:
                content = threewaymerge(origcontent, content, self.content)
            except ConflictException as e:
                e.new_id = revision
                raise e

        wr = WikiRevision.create(self._id, content, author, reason)
        self.content = content
        self.last_edit_by = author
        self.last_edit_date = wr.date
        self.revision = str(wr._id)
        self._commit()

        LastModified.touch(self._fullname, "Edit")

        return wr
Example #14
0
    def fast_query(cls, thing1, thing2s):
        """Find relationships between thing1 and various thing2s."""
        thing2s, thing2s_is_single = tup(thing2s, ret_is_single=True)

        if not thing1:
            return {}

        # don't bother looking up relationships for items that were created
        # since the last time the thing1 created a relationship of this type
        if cls._last_modified_name:
            from r2.models.last_modified import LastModified
            timestamp = LastModified.get(thing1._fullname,
                                         cls._last_modified_name)
            if timestamp:
                thing2s = [thing2 for thing2 in thing2s
                           if thing2._date <= timestamp]
            else:
                thing2s = []

        if not thing2s:
            return {}

        # fetch the row from cassandra. if it doesn't exist, thing1 has no
        # relation of this type to any thing2!
        try:
            columns = [thing2._id36 for thing2 in thing2s]
            results = cls._cf.get(thing1._id36, columns)
        except NotFoundException:
            results = {}

        # return the data in the expected format
        if not thing2s_is_single:
            # {(thing1, thing2) : value}
            thing2s_by_id = {thing2._id36 : thing2 for thing2 in thing2s}
            return {(thing1, thing2s_by_id[k]) : v
                    for k, v in results.iteritems()}
        else:
            if results:
                assert len(results) == 1
                return results.values()[0]
            else:
                raise NotFound("<%s %r>" % (cls.__name__, (thing1._id36,
                                                           thing2._id36)))
Example #15
0
def change_password(user, newpassword):
    user.password = bcrypt_password(newpassword)
    user._commit()
    LastModified.touch(user._fullname, 'Password')
    return True
Example #16
0
 def add_comments(self, comments):
     impl = self.IMPLEMENTATIONS[self.link.comment_tree_version]
     impl.add_comments(self, comments)
     utils.set_last_modified(self.link, 'comments')
     LastModified.touch(self.link._fullname, 'Comments')
Example #17
0
def change_password(user, newpassword):
    user.password = bcrypt_password(newpassword)
    user._commit()
    LastModified.touch(user._fullname, 'Password')
    return True
Example #18
0
    def _fast_query(cls, thing1s, thing2s, properties = None, **kw):
        """Find all of the relations of this class between all of the
           members of thing1_ids and thing2_ids"""
        thing1s, thing1s_is_single = tup(thing1s, True)
        thing2s, thing2s_is_single = tup(thing2s, True)

        if not thing1s or not thing2s:
            return {}

        # grab the last time each thing1 modified this relation class so we can
        # know which relations not to even bother looking up
        if thing1s:
            from r2.models.last_modified import LastModified
            fullnames = [cls._thing1_cls._fullname_from_id36(thing1._id36)
                         for thing1 in thing1s]
            timestamps = LastModified.get_multi(fullnames,
                                                cls._cf.column_family)

        # build up a list of ids to look up, throwing out the ones that the
        # timestamp fetched above indicates are pointless
        ids = set()
        thing1_ids, thing2_ids = {}, {}
        for thing1 in thing1s:
            last_modification = timestamps.get(thing1._fullname)

            if not last_modification:
                continue

            for thing2 in thing2s:
                key = cls._rowkey(thing1._id36, thing2._id36)

                if key in ids:
                    continue

                if thing2._date > last_modification:
                    continue

                ids.add(key)
                thing2_ids[thing2._id36] = thing2
            thing1_ids[thing1._id36] = thing1

        # all relations must load these properties, even if unrequested
        if properties is not None:
            properties = set(properties)

            properties.add('thing1_id')
            properties.add('thing2_id')

        rels = {}
        if ids:
            rels = cls._byID(ids, properties=properties).values()

        if thing1s_is_single and thing2s_is_single:
            if rels:
                assert len(rels) == 1
                return rels[0]
            else:
                raise NotFound("<%s %r>" % (cls.__name__,
                                            cls._rowkey(thing1s[0]._id36,
                                                        thing2s[0]._id36)))

        return dict(((thing1_ids[rel.thing1_id], thing2_ids[rel.thing2_id]), rel)
                    for rel in rels)
Example #19
0
 def add_comments(self, comments):
     impl = self.IMPLEMENTATIONS[self.link.comment_tree_version]
     impl.add_comments(self, comments)
     utils.set_last_modified(self.link, 'comments')
     LastModified.touch(self.link._fullname, 'Comments')
Example #20
0
    def _fast_query(cls, thing1s, thing2s, properties=None, **kw):
        """Find all of the relations of this class between all of the
           members of thing1_ids and thing2_ids"""
        thing1s, thing1s_is_single = tup(thing1s, True)
        thing2s, thing2s_is_single = tup(thing2s, True)

        if not thing1s or not thing2s:
            return {}

        # grab the last time each thing1 modified this relation class so we can
        # know which relations not to even bother looking up
        if thing1s:
            from r2.models.last_modified import LastModified
            fullnames = [
                cls._thing1_cls._fullname_from_id36(thing1._id36)
                for thing1 in thing1s
            ]
            timestamps = LastModified.get_multi(fullnames,
                                                cls._cf.column_family)

        # build up a list of ids to look up, throwing out the ones that the
        # timestamp fetched above indicates are pointless
        ids = set()
        thing1_ids, thing2_ids = {}, {}
        for thing1 in thing1s:
            last_modification = timestamps.get(thing1._fullname)

            if not last_modification:
                continue

            for thing2 in thing2s:
                key = cls._rowkey(thing1._id36, thing2._id36)

                if key in ids:
                    continue

                if thing2._date > last_modification:
                    continue

                ids.add(key)
                thing2_ids[thing2._id36] = thing2
            thing1_ids[thing1._id36] = thing1

        # all relations must load these properties, even if unrequested
        if properties is not None:
            properties = set(properties)

            properties.add('thing1_id')
            properties.add('thing2_id')

        rels = {}
        if ids:
            rels = cls._byID(ids, properties=properties).values()

        if thing1s_is_single and thing2s_is_single:
            if rels:
                assert len(rels) == 1
                return rels[0]
            else:
                raise NotFound(
                    "<%s %r>" %
                    (cls.__name__,
                     cls._rowkey(thing1s[0]._id36, thing2s[0]._id36)))

        return dict(
            ((thing1_ids[rel.thing1_id], thing2_ids[rel.thing2_id]), rel)
            for rel in rels)