Beispiel #1
0
    def update(self, fs=None, web=None):
        """
        Renders the footer image and saves it to the filesystem (i.e. S3 in production).

        `web` is an optional perf optimization.
        """
        from canvas.thumbnailer import generate_thumbnails
        data = self.render_image().getvalue()
        footer_meta = generate_thumbnails(data,
                                          fs=fs,
                                          image_type='footer',
                                          filename=self.get_path())

        # Update the Content details.
        content_key = self.comment.reply_content.details_key
        old_raw_meta = redis.get(content_key)
        if old_raw_meta:
            meta = util.loads(old_raw_meta)
        else:
            meta = {}
        meta.update(footer_meta)
        redis.set(content_key, util.dumps(meta))

        self.comment.reply_content.details.force()
        self.comment.details.force()

        return meta
Beispiel #2
0
def create_gif_content(**kwargs):
    content = create_content(animated=True, **kwargs)
    key = 'content:%s:details' % content.id
    details = util.loads(redis.get(key))
    details['original']['animated'] = True
    redis.set(key, util.dumps(details))
    return content
def create_gif_content(**kwargs):
    content = create_content(**kwargs)
    key = "content:%s:details" % content.id
    details = util.loads(redis.get(key))
    details["original"]["animated"] = True
    redis.set(key, util.dumps(details))
    return content
Beispiel #4
0
    def test_properties_dont_get_serialized(self):
        # CommentDetails should only serialize its dict contents, not any of its member properties.
        cmt = create_comment().details()
        cmt.test_foo_property = 1

        d = util.loads(util.dumps(cmt))
        self.assertFalse('test_foo_property' in d)
Beispiel #5
0
    def test_properties_dont_get_serialized(self):
        # CommentDetails should only serialize its dict contents, not any of its member properties.
        cmt = create_comment().details()
        cmt.test_foo_property = 1

        d = util.loads(util.dumps(cmt))
        self.assertFalse('test_foo_property' in d)
Beispiel #6
0
def fake_api_request(api_path, data={}):
    path = ('/api/' + api_path)
    req = FakeRequest(path=path,
                      GET=None,
                      extra_META={'CONTENT_TYPE': 'application/json'})
    req.body = util.dumps(data)
    req.method = 'POST'
    return req
Beispiel #7
0
def update_sticky_thread_cache():
    threads = RedisList(STICKY_THREAD_CACHE_KEY)
    threads.delete()
    for x in [
            util.dumps((x.comment.id, x.text))
            for x in StickyThread.objects.all()
    ]:
        threads.rpush(x)
Beispiel #8
0
 def post(cls, path, data=None):
     # TODO: get rid of this when the api doesnt require staff
     user = create_staff()
     data = util.dumps(data) if data else None
     return cls._http_verb('post',
                           path,
                           data=data,
                           user=user,
                           content_type='application/json')
Beispiel #9
0
def create_content(ip, fs, data, remix_of, stamps_used, text_used, source_url='', is_quest=False):
    exclude_types = []

    if settings.PROJECT == 'drawquest':
        if not is_quest:
            exclude_types = ['homepage_featured']

    meta = generate_thumbnails(data, fs=fs, exclude_types=exclude_types)

    if remix_of:
        remix_of = Content.all_objects.get(id=remix_of)
        remix_of.hide_if_unpublished()
    else:
        remix_of = None

    if stamps_used:
        stamps_used = [Content.all_objects.get_or_none(id=stamp_id) for stamp_id in stamps_used if stamp_id]
        stamps_used = [stamp for stamp in stamps_used if stamp]

    try:
        content = Content.all_objects.get(id=meta['id'])
        # Don't allow uploading content that has been disabled.
        if content.visibility == Visibility.DISABLED:
            return {
                'success': False,
                'reason': 'This image has been banned.',
            }
    except Content.DoesNotExist:
        url_mapping = ContentUrlMapping()
        url_mapping.save()

        content = Content(
            id = meta['id'],
            visibility = Visibility.UNPUBLISHED,
            url_mapping = url_mapping,
            ip = ip,
            timestamp = time(),
            remix_of = remix_of,
            remix_text = text_used,
            source_url = source_url,
        )
        update_metadata(content, meta)

        try:
            content.save()
        except IntegrityError:
            # Race condition, retry
            return create_content(ip, fs, data, remix_of, stamps_used, text_used, source_url)

        content.stamps_used.add(*stamps_used)
        redis.set(content.details_key, util.dumps(meta))

    existing_url = content.first_caption and content.first_caption.details().url
    return {'success': True, 'content': content.details.force(), 'existing_url': existing_url}
Beispiel #10
0
 def _api_call(cls, path, data=None, client=None, user=None, password=None, method="post"):
     data = data or {}
     response = getattr(cls, method)(path,
                                     data=util.dumps(data),
                                     client=client,
                                     user=user,
                                     password=password,
                                     content_type="application/json")
     try:
         content = util.loads(response.content)
     except ValueError:
         # Probably not a JSON response, so just return a string.
         content = response.content
     return content
def request_jsonp(request, data):
    response = util.dumps(data)

    # Handle JSONP requests.
    callbacks = request.args.get('callback')
    if callbacks:
        callback = callbacks[0]
        # If the callback isn't alphanumeric, we could be executing arbitrary script on the client.
        if not callback.replace('_', '').isalnum():
            request.setResponseCode(500)
            response = 'JSONP callbacks must be alpha-numeric.'
        else:
            response = "%s(%s);" % (callback, response)
    return response
Beispiel #12
0
def request_jsonp(request, data):
    response = util.dumps(data)

    # Handle JSONP requests.
    callbacks = request.args.get('callback')
    if callbacks:
        callback = callbacks[0]
        # If the callback isn't alphanumeric, we could be executing arbitrary script on the client.
        if not callback.replace('_', '').isalnum():
            request.setResponseCode(500)
            response = 'JSONP callbacks must be alpha-numeric.'
        else:
            response = "%s(%s);" % (callback, response)
    return response
Beispiel #13
0
    def from_redis_activity(cls, activity, key=None):
        act = cls()

        if activity.actor:
            act.actor = User.objects.get(pk=activity.actor['id'])

        act.timestamp = activity.timestamp
        act.activity_type = activity.TYPE
        act.key = key
        discard_keys = ['actor', 'ts', 'type']
        base = activity.to_dict()
        act._data = {k: base[k] for k in base.keys() if k not in discard_keys}
        act.data = util.dumps(act._data)
        act.save()
        return act
Beispiel #14
0
    def from_redis_activity(cls, activity, key=None):
        act = cls()

        if activity.actor:
            act.actor = User.objects.get(pk=activity.actor['id'])

        act.timestamp = activity.timestamp
        act.activity_type = activity.TYPE
        act.key = key
        discard_keys = ['actor', 'ts', 'type']
        base = activity.to_dict()
        act._data = {k: base[k] for k in base.keys() if k not in discard_keys}
        act.data = util.dumps(act._data)
        act.save()
        return act
Beispiel #15
0
def update(fs, content, image_type, save_to_db=True):
    filename = content.details()['original']['name']
    # Prevent issues with unicode filenames.
    filename = filename.encode('ascii')
    data = fs.read(filename)

    thumbnailer = Thumbnailer(fs)

    meta = util.loads(redis.get(content.details_key))
    meta.update(thumbnailer.store(data, image_type))
    update_metadata(content, meta)

    if save_to_db:
        content.save()
    redis.set(content.details_key, util.dumps(meta))
    content.details.force()
Beispiel #16
0
def update(fs, content, image_type, save_to_db=True):
    filename = content.details()['original']['name']
    # Prevent issues with unicode filenames.
    filename = filename.encode('ascii')
    data = fs.read(filename)

    thumbnailer = Thumbnailer(fs)

    meta = util.loads(redis.get(content.details_key))
    meta.update(thumbnailer.store(data, image_type))
    update_metadata(content, meta)

    if save_to_db:
        content.save()
    redis.set(content.details_key, util.dumps(meta))
    content.details.force()
    def from_redis_activity(cls, activity, key=None):
        act = cls()

        if activity.actor:
            act.actor_id = int(activity.actor["id"])

        act.timestamp = activity.timestamp
        act.activity_type = activity.TYPE
        act.key = key
        discard_keys = ["actor", "ts", "type"]
        base = activity.to_dict()
        act._data = {k: base[k] for k in base.keys() if k not in discard_keys}
        act.data = util.dumps(act._data)
        act.save()

        return act
Beispiel #18
0
 def test_signup_with_reply(self):
     op = self.post_comment(reply_content=create_content().id)
     info = {
         u'url': u'https://savnac.com/p/66',
         u'post': {
             u'category': None,
             u'parent_comment': op.id,
             u'replied_comment': None,
             u'anonymous': False,
             u'reply_text': u'ooooooo',
             u'reply_content': None
         },
         u'reason': u'reply',
     }
     def get_op():
         return Comment.objects.get(pk=op.pk)
     self.assertEqual(0, get_op().replies.count())
     self.signup('ilovetotest', extra_info=dumps(info))
     self.assertEqual(1, get_op().replies.count())
    def from_redis_activity(cls, activity, key=None):
        act = cls()

        activity_data = activity.to_dict()

        if activity.actor:
            act.actor_id = int(activity.actor["id"])

        act.quest_id = activity_data.get("quest_id")
        act.comment_id = activity_data.get("comment_id")

        act.timestamp = activity.timestamp
        act.activity_type = ACTIVITY_TYPE_IDS[activity.TYPE]
        act.key = key
        discard_keys = ["actor", "ts", "type", "quest_id", "comment_id"]
        act._data = {k: activity_data[k] for k in activity_data.keys() if k not in discard_keys}
        act.data = util.dumps(act._data)
        act.save()

        return act
Beispiel #20
0
    def test_signup_with_reply(self):
        op = self.post_comment(reply_content=create_content().id)
        info = {
            u'url': u'https://savnac.com/p/66',
            u'post': {
                u'category': None,
                u'parent_comment': op.id,
                u'replied_comment': None,
                u'anonymous': False,
                u'reply_text': u'ooooooo',
                u'reply_content': None
            },
            u'reason': u'reply',
        }

        def get_op():
            return Comment.objects.get(pk=op.pk)

        self.assertEqual(0, get_op().replies.count())
        self.signup('ilovetotest', extra_info=dumps(info))
        self.assertEqual(1, get_op().replies.count())
Beispiel #21
0
    def from_redis_activity(cls, activity, key=None):
        act = cls()

        activity_data = activity.to_dict()

        if activity.actor:
            act.actor_id = int(activity.actor['id'])

        act.quest_id = activity_data.get('quest_id')
        act.comment_id = activity_data.get('comment_id')

        act.timestamp = activity.timestamp
        act.activity_type = ACTIVITY_TYPE_IDS[activity.TYPE]
        act.key = key
        discard_keys = ['actor', 'ts', 'type', 'quest_id', 'comment_id']
        act._data = {
            k: activity_data[k]
            for k in activity_data.keys() if k not in discard_keys
        }
        act.data = util.dumps(act._data)
        act.save()

        return act
    def update(self, fs=None, web=None):
        """
        Renders the footer image and saves it to the filesystem (i.e. S3 in production).

        `web` is an optional perf optimization.
        """
        from canvas.thumbnailer import generate_thumbnails
        data = self.render_image().getvalue()
        footer_meta = generate_thumbnails(data, fs=fs, image_type='footer', filename=self.get_path())

        # Update the Content details.
        content_key = self.comment.reply_content.details_key
        old_raw_meta = redis.get(content_key)
        if old_raw_meta:
            meta = util.loads(old_raw_meta)
        else:
            meta = {}
        meta.update(footer_meta)
        redis.set(content_key, util.dumps(meta))

        self.comment.reply_content.details.force()
        self.comment.details.force()

        return meta
def fake_api_request(api_path, data={}):
    path = "/api/" + api_path
    req = FakeRequest(path=path, GET=None, extra_META={"CONTENT_TYPE": "application/json"})
    req.body = util.dumps(data)
    req.method = "POST"
    return req
Beispiel #24
0
def return_json(request, response):
    request.setHeader('Content-type', 'application/json')
    return util.dumps(response)
Beispiel #25
0
 def post(cls, path, data=None):
     # TODO: get rid of this when the api doesnt require staff
     user = create_staff()
     data = util.dumps(data) if data else None
     return cls._http_verb('post', path, data=data, user=user, content_type='application/json')
 def remote_set(self, value):
     """ Sets the remote cache. """
     cache_data = util.dumps(value)
     cache.set(self.key, cache_data, self.timeout)
     return cache_data
def return_json(request, response):
    request.setHeader('Content-type', 'application/json')
    return util.dumps(response)
Beispiel #28
0
 def stringify(obj):
     return util.dumps(obj, indent=2)
Beispiel #29
0
 def _assertJSON(self, value):
     self.assertEqual(value, util.loads(util.dumps(value)))
def update_sticky_thread_cache():
    threads = RedisList(STICKY_THREAD_CACHE_KEY)
    threads.delete()
    for x in [util.dumps((x.comment.id, x.text)) for x in StickyThread.objects.all()]:
        threads.rpush(x)
Beispiel #31
0
 def send(self, data):
     key = self.queue.add(util.dumps(data))
     data['nkey'] = key
     data['msg_type'] = "notification"
     self.channel.publish(data)
     return key
 def _assertJSON(self, value):
     self.assertEqual(value, util.loads(util.dumps(value)))
 def remote_set(self, value):
     """ Sets the remote cache. """
     cache_data = util.dumps(value)
     cache.set(self.key, cache_data, self.timeout)
     return cache_data
 def send(self, data):
     key = self.queue.add(util.dumps(data))
     data['nkey'] = key
     data['msg_type'] = "notification"
     self.channel.publish(data)
     return key
Beispiel #35
0
def create_content(ip,
                   fs,
                   data,
                   remix_of,
                   stamps_used,
                   text_used,
                   source_url='',
                   is_quest=False):
    exclude_types = []

    if settings.PROJECT == 'drawquest':
        if not is_quest:
            exclude_types = ['homepage_featured']

    meta = generate_thumbnails(data, fs=fs, exclude_types=exclude_types)

    if remix_of:
        remix_of = Content.all_objects.get(id=remix_of)
        remix_of.hide_if_unpublished()
    else:
        remix_of = None

    if stamps_used:
        stamps_used = [
            Content.all_objects.get_or_none(id=stamp_id)
            for stamp_id in stamps_used if stamp_id
        ]
        stamps_used = [stamp for stamp in stamps_used if stamp]

    try:
        content = Content.all_objects.get(id=meta['id'])
        # Don't allow uploading content that has been disabled.
        if content.visibility == Visibility.DISABLED:
            return {
                'success': False,
                'reason': 'This image has been banned.',
            }
    except Content.DoesNotExist:
        url_mapping = ContentUrlMapping()
        url_mapping.save()

        content = Content(
            id=meta['id'],
            visibility=Visibility.UNPUBLISHED,
            url_mapping=url_mapping,
            ip=ip,
            timestamp=time(),
            remix_of=remix_of,
            remix_text=text_used,
            source_url=source_url,
        )
        update_metadata(content, meta)

        try:
            content.save()
        except IntegrityError:
            # Race condition, retry
            return create_content(ip, fs, data, remix_of, stamps_used,
                                  text_used, source_url)

        content.stamps_used.add(*stamps_used)
        redis.set(content.details_key, util.dumps(meta))

    existing_url = content.first_caption and content.first_caption.details(
    ).url
    return {
        'success': True,
        'content': content.details.force(),
        'existing_url': existing_url
    }
 def resource_render(self, request, path, filedata):
     log.msg("ScriptDrop resource_render %r" % (path,))
     request.setHeader('Expires', 'Expires: Thu, 31 Dec 2020 16:00:00 GMT')
     _, ext = os.path.splitext(path)
     request.setHeader('Content-type', 'application/javascript')
     request.write(util.dumps({'source': filedata}))
Beispiel #37
0
 def test_simplest_strings(self):
     self.assertEqual("{}", util.dumps({}))
     self.assertEqual("[]", util.dumps([]))
Beispiel #38
0
def fake_api_request(api_path, data={}):
    path = ('/api/' + api_path)
    req = FakeRequest(path=path, GET=None, extra_META={'CONTENT_TYPE': 'application/json'})
    req.raw_post_data = util.dumps(data)
    req.method = 'POST'
    return req
Beispiel #39
0
 def resource_render(self, request, path, filedata):
     log.msg("ScriptDrop resource_render %r" % (path, ))
     request.setHeader('Expires', 'Expires: Thu, 31 Dec 2020 16:00:00 GMT')
     _, ext = os.path.splitext(path)
     request.setHeader('Content-type', 'application/javascript')
     request.write(util.dumps({'source': filedata}))
 def test_simplest_strings(self):
     self.assertEqual("{}", util.dumps({}))
     self.assertEqual("[]", util.dumps([]))
Beispiel #41
0
 def stringify(obj):
     return util.dumps(obj, indent=2)