示例#1
0
    def __init__(self, user_id=None):
        self.user_id = user_id

        self._palettes = None

        if user_id:
            self._palettes = RedisSet('user:{}:palettes'.format(user_id))
示例#2
0
 def _record(timestamp_key):
     if request:
         RedisSet(timestamp_key + ":unique_ips").sadd(
             util.ip_to_int(request.META.get('REMOTE_ADDR')))
     if user:
         RedisSet(timestamp_key + ":uniques").sadd(user.id)
     RedisKey(timestamp_key + ":count").incr(1)
 def forwards(self, orm):
     User = orm['auth.User']
     
     for x in range(40):
         day = datetime.date.today() - datetime.timedelta(x)
         dayset = RedisSet('metrics:signup:%s:uniques' % day.strftime("%Y.%m.%d"))
         for user_id in User.objects.filter(date_joined__range=(day, day+datetime.timedelta(1))).values_list('id', flat=True):
             dayset.sadd(user_id)
示例#4
0
    def forwards(self, orm):
        User = orm['auth.User']

        for x in range(40):
            day = datetime.date.today() - datetime.timedelta(x)
            dayset = RedisSet('metrics:signup:%s:uniques' %
                              day.strftime("%Y.%m.%d"))
            for user_id in User.objects.filter(
                    date_joined__range=(day, day +
                                        datetime.timedelta(1))).values_list(
                                            'id', flat=True):
                dayset.sadd(user_id)
示例#5
0
文件: models.py 项目: StetHD/canvas-2
    def __init__(self, user_id=None):
        self.user_id = user_id

        self._palettes = None

        if user_id:
            self._palettes = RedisSet('user:{}:palettes'.format(user_id))
示例#6
0
    def forwards(self, orm):
        "Write your forwards methods here."

        count = 0

        for c in orm.Comment.objects.all():
            if c.category is not None:
                cname = c.category.name
                tags = RedisSet("comment:{}:tags".format(c.id))
                if tags.scard() == 0:
                    tags.sadd(cname)

                Tag(cname).tag_comment(c, c.timestamp)


            count += 1
            if count % 10000 == 0:
                print count
示例#7
0
class ExperimentBranch(object):
    def __init__(self, experiment, name, weight):
        self.experiment = experiment
        self.name = name
        self.weight = weight

    def __eq__(self, other):
        return self.name == other.name and self.experiment == other.experiment

    users = property(lambda self: RedisSet('experiment:%s:%s' % (self.experiment.name, self.name)))
示例#8
0
文件: models.py 项目: StetHD/canvas-2
class UserPalettes(object):
    def __init__(self, user_id=None):
        self.user_id = user_id

        self._palettes = None

        if user_id:
            self._palettes = RedisSet('user:{}:palettes'.format(user_id))

    def __iter__(self):
        yield DEFAULT_PALETTE

        if self._palettes:
            for palette in self._palettes.smembers():
                yield get_palette_by_id(palette)

    def __contains__(self, palette):
        if palette.id == DEFAULT_PALETTE.id:
            return True

        return self._palettes and palette.id in self._palettes

    def to_client(self):
        return [palette for palette in self]

    def unlock(self, palette):
        if not self.user_id:
            raise TypeError("Cannot unlock a palette for a logged-out user.")

        if isinstance(palette, basestring):
            palette = get_palette_by_name(name)

        if palette.id == DEFAULT_PALETTE.id:
            return

        self._palettes.sadd(palette.id)
示例#9
0
class UserPalettes(object):
    def __init__(self, user_id=None):
        self.user_id = user_id

        self._palettes = None

        if user_id:
            self._palettes = RedisSet('user:{}:palettes'.format(user_id))

    def __iter__(self):
        yield DEFAULT_PALETTE

        if self._palettes:
            for palette in self._palettes.smembers():
                yield get_palette_by_id(palette)

    def __contains__(self, palette):
        if palette.id == DEFAULT_PALETTE.id:
            return True

        return self._palettes and palette.id in self._palettes

    def to_client(self, **kwargs):
        return [palette for palette in self]

    def unlock(self, palette):
        if not self.user_id:
            raise TypeError("Cannot unlock a palette for a logged-out user.")

        if isinstance(palette, basestring):
            palette = get_palette_by_name(name)

        if palette.id == DEFAULT_PALETTE.id:
            return

        self._palettes.sadd(palette.id)
示例#10
0
    def forwards(self, orm):
        "Write your forwards methods here."

        count = 0

        for c in orm.Comment.objects.all():
            if c.visibility in [Visibility.PUBLIC, Visibility.CURATED] and c.reply_content is not None:
                tags = RedisSet("comment:{}:tags".format(c.id)).smembers()
                for tag in tags:
                    t = Tag(tag)
                    t.images_only.bump(c.id, score=c.timestamp)

            count += 1
            if count % 10000 == 0:
                print count
示例#11
0
文件: models.py 项目: eiritana/canvas
            ua.broadcast(payload)
            util.logger.info(
                "Sent global push notification with alert: {}".format(alert))
    elif type_ in PERSONAL_PUSH_NOTIFICATION_TYPES:
        if settings.PRODUCTION and not is_unsubscribed(recipient, type_):
            ua.push(payload, aliases=[recipient.username])
    else:
        raise ValueError(
            "Invalid push notification type '{}'.".format(notification_type))


# Subscriptions only matter for personal notifications. Global notification subscriptions are handled on the device.

unsubscriptions = dict((
    name,
    RedisSet('push_notification:{0}:unsubscriptions'.format(id_)),
) for name, id_ in PERSONAL_PUSH_NOTIFICATION_TYPES.items())


def _check_personal_type(push_notification_type):
    if push_notification_type not in PERSONAL_PUSH_NOTIFICATION_TYPES:
        raise ValueError("Invalid personal push notification type '{}'".format(
            push_notification_type))


def unsubscribe(user, push_notification_type):
    _check_personal_type(push_notification_type)
    unsubscriptions[push_notification_type].sadd(user.id)


def resubscribe(user, push_notification_type):
示例#12
0
 def hourly_uniques(self, datetime, ip=False):
     return int(
         RedisSet(self.basekey + ":" + datetime.strftime("%Y.%m.%d.%H") +
                  (":uniques" if not ip else ":unique_ips")).scard() or 0)
示例#13
0
 def uniques(self, day, ip=False):
     return RedisSet(self.daykey(day,
                                 'uniques' if not ip else 'unique_ips'))
示例#14
0
文件: models.py 项目: eiritana/canvas
import datetime
from collections import defaultdict

from canvas.redis_models import redis, RedisSet, RedisSortedSet, RedisKey, RedisLastBumpedBuffer, DateKey, RealtimeChannel
from canvas import knobs
from services import Services

all_tags = RedisSet("tags:all")

class Tag(object):
    top = property(lambda self: DateKey(lambda key: RedisLastBumpedBuffer(key, 30*30), self.base_key, ':top'))

    updates_channel = property(lambda self: RealtimeChannel('tu:%s' %  self.name, 5, ttl=24*60*60))

    def __repr__(self):
        return self.name

    def __init__(self, name):
        self.name = name.lower().strip().replace('#', '')
        self.base_key = 'tag:{}:posts'.format(self.name)
        self.new = RedisSortedSet(self.base_key)
        self.images_only = RedisLastBumpedBuffer(self.base_key + ':images', 1000)
        self.popular = RedisLastBumpedBuffer(self.base_key + ':popular', 1000)
        self.post_count = RedisKey(self.base_key + ':count')

    def to_client(self):
        return self.name

    def tag_comment(self, comment, timestamp=None):
        if timestamp is None:
            timestamp = Services.time.time()