Exemple #1
0
    def _import_videos(self, form):
        for video in form.import_data.videos:
            video.rockpack_curated = True
            video.category = form.category.data
        count = Video.add_videos(form.import_data.videos, form.source.data)

        if not form.channel.data and not form.user.data:
            self._set_form_data_from_source(form)

        channelid = form.channel.data
        if channelid.startswith('_new:'):
            channel = Channel.create(
                title=channelid.split(':', 1)[1],
                owner=form.user.data,
                description=form.channel_description.data,
                cover=form.cover.data,
                category=form.category.data)
            self.record_action('created', channel)
        else:
            channel = Channel.query.get_or_404(channelid)
        channel.add_videos(
            form.import_data.videos,
            form.tags.data,
            category=form.category.data,
            date_added=form.date_added.data
        )
        self.record_action('imported', channel, '%d videos' % count)
        push_config = getattr(form.import_data, 'push_config', None)
        if push_config and channel.id:
            try:
                subscribe(push_config.hub, push_config.topic, channel.id)
            except Exception, e:
                flash('Unable to subscribe to channel: %s' % e.message, 'error')
Exemple #2
0
 def setUp(self, requests_post):
     super(PubSubHubbubTestCase, self).setUp()
     self.ctx = self.app.test_request_context()
     self.ctx.push()
     self.channel = Channel(title='', description='', cover='', owner=UserData.test_user_a.id).save()
     assert Channel.query.get(self.channel.id), self.channel.id
     self.channel.add_meta('en-us')
     self.subid = subscribe('test', uuid.uuid4().hex, self.channel.id).id
Exemple #3
0
 def channels(self):
     user = request.args.get('user', '')
     if 'user' in request.args.keys():
         if not re.match('^[\w-]+$', user):
             user = None
         return jsonify(Channel.get_form_choices(owner=user))
     exact_name = request.args.get('exact_name', '')
     if exact_name:
         channels = list(Channel.query.filter(Channel.title == exact_name).values(Channel.id, Channel.title))
         if not channels:
             channels = list(Channel.query.filter(Channel.id == exact_name).values(Channel.id, Channel.title))
         return jsonify(channels)
     prefix = request.args.get('prefix', '').lower()
     if prefix and re.match('^[!&#\w ]+$', prefix):
         return jsonify(Channel.query.filter(
             Channel.deleted == False,
             Channel.public == True,
             func.lower(Channel.title).like(prefix + '%')
         ).values(Channel.id, Channel.title))
     return jsonify([])
Exemple #4
0
class PubSubHubbubTestCase(RockPackTestCase):

    @patch('rockpack.mainsite.requests.post')
    def setUp(self, requests_post):
        super(PubSubHubbubTestCase, self).setUp()
        self.ctx = self.app.test_request_context()
        self.ctx.push()
        self.channel = Channel(title='', description='', cover='', owner=UserData.test_user_a.id).save()
        assert Channel.query.get(self.channel.id), self.channel.id
        self.channel.add_meta('en-us')
        self.subid = subscribe('test', uuid.uuid4().hex, self.channel.id).id

    def tearDown(self):
        self.ctx.pop()
        super(PubSubHubbubTestCase, self).tearDown()

    def test_verify(self):
        subs = Subscription.query.get(self.subid)
        self.assertFalse(bool(subs.verified))
        with self.app.test_client() as client:
            r = client.get(
                '/ws/pubsubhubbub/callback',
                query_string={
                    'id': subs.id,
                    'hub.topic': subs.topic,
                    'hub.challenge': 123,
                    'hub.verify_token': subs.verify_token,
                    'hub.mode': 'subscribe',
                    'hub.lease_seconds': 60,
                })
            self.assertEquals(r.status_code, 200)
        self.assertTrue(subs.verified)

    def _post_entries(self, subs, data):
        sig = hmac.new(subs.secret, data, hashlib.sha1)
        with self.app.test_client() as client:
            r = client.post(
                '/ws/pubsubhubbub/callback',
                query_string={'id': subs.id},
                content_type='application/atom+xml',
                data=data,
                headers={'X-Hub-Signature': 'sha1=' + sig.hexdigest()})
            self.assertEquals(r.status_code, 204)

    def test_post(self):
        entry = PUSH_ENTRY_XML % dict(videoid='xyzzy')
        data = PUSH_ATOM_XML % dict(entry=entry)
        subs = Subscription.query.get(self.subid)
        self._post_entries(subs, data)
        video = subs.channel.video_instances[0].video_rel
        self.assertEquals(video.source_videoid, 'xyzzy')
        self.assertEquals(video.duration, 248)
        self.assertIn('BQ', [rst.country for rst in video.restrictions])

    def test_post_update(self):
        # Double-check that subsequent updates and repeated videos
        # are handled correctly.
        ids = map(str, range(3))
        entry = PUSH_ENTRY_XML % dict(videoid=ids[0])
        for id in ids:
            entry += PUSH_ENTRY_XML % dict(videoid=id)
            data = PUSH_ATOM_XML % dict(entry=entry)
            subs = Subscription.query.get(self.subid)
            self._post_entries(subs, data)
        channel_videos = [
            v.video_rel.source_videoid for v in subs.channel.video_instances]
        self.assertEquals(sorted(channel_videos), ids)