Ejemplo n.º 1
0
    def get_object(self, request, channel_slug=None):
        self._root_url = get_base_url(request)
        if channel_slug:
            self.channel = get_object_or_404(Channel,
                                             slug__iexact=channel_slug)
        else:
            self.channel = Channel.objects.get(
                slug=settings.DEFAULT_CHANNEL_SLUG)
        if self.channel.cover_art:
            self.itunes_lg_url = thumbnail(self.channel.cover_art,
                                           '1400x1400').url
            self.itunes_sm_url = thumbnail(self.channel.cover_art,
                                           '144x144').url
            if self.itunes_lg_url.startswith('//'):
                # e.g. //cdn.example.com/media/cache/file.png
                protocol = self._root_url.split('//')[0]
                self.itunes_lg_url = protocol + self.itunes_lg_url
                self.itunes_sm_url = protocol + self.itunes_sm_url
            elif '://' not in self.itunes_lg_url:
                self.itunes_lg_url = self._root_url + self.itunes_lg_url
                self.itunes_sm_url = self._root_url + self.itunes_sm_url
        else:
            # Use the default ones
            self.itunes_sm_url = get_abs_static(
                'main/img/podcast-cover-144x144.png', request)
            self.itunes_lg_url = get_abs_static(
                'main/img/podcast-cover-1400x1400.png', request)

        super(ITunesFeed, self).get_object(request)
Ejemplo n.º 2
0
def redirect_event_thumbnail(request, id):
    """The purpose of this is to be able to NOT have to generate the
    thumbnail for each event in the events_data() view. It makes the JSON
    smaller and makes it possible to only need the thumbnail for few
    (at a time) thumbnails that we need. """
    event = get_object_or_404(Event, id=id)
    geometry = request.GET.get('geometry', '40x40')
    crop = request.GET.get('crop', 'center')
    if event.picture:
        thumb = thumbnail(event.picture.file, geometry, crop=crop)
    else:
        thumb = thumbnail(event.placeholder_img, geometry, crop=crop)

    return redirect(thumb.url)
Ejemplo n.º 3
0
 def render(self, name, value, attrs):
     pictures = []
     qs = Picture.objects.all()
     if self.event:
         qs = qs.filter(Q(event__isnull=True) | Q(event=self.event))
         # If the current event does use an inactive picture,
         # let it still be a choice.
         if self.event.picture_id:
             qs = qs.filter(Q(is_active=True) | Q(id=self.event.picture_id))
         else:
             qs = qs.filter(is_active=True)
     else:
         qs = qs.filter(
             event__isnull=True,
             is_active=True,
         )
     for pic in qs.order_by('event', '-created'):
         thumb = thumbnail(pic.file, '160x90', crop='center')
         pictures.append({
             'thumb': {
                 'url': thumb.url,
                 'width': thumb.width,
                 'height': thumb.height
             },
             'notes': pic.notes,
             'selected': value == pic.id,
             'id': pic.id,
         })
     context = {'pictures': pictures, 'current_id': value, 'name': name}
     return mark_safe(render_to_response('gallery.html', context).content)
Ejemplo n.º 4
0
 def render(self, name, value, attrs):
     pictures = []
     qs = Picture.objects.all()
     if self.event:
         qs = qs.filter(
             Q(event__isnull=True) |
             Q(event=self.event)
         )
     else:
         qs = qs.filter(event__isnull=True)
     for pic in qs.order_by('event', '-created'):
         thumb = thumbnail(pic.file, '160x90', crop='center')
         pictures.append({
             'thumb': {
                 'url': thumb.url,
                 'width': thumb.width,
                 'height': thumb.height
             },
             'notes': pic.notes,
             'selected': value == pic.id,
             'id': pic.id,
         })
     context = {
         'pictures': pictures,
         'current_id': value,
         'name': name
     }
     return mark_safe(render_to_response('gallery.html', context).content)
Ejemplo n.º 5
0
    def __init__(self, event, *args, **kwargs):
        super(EventTweetForm, self).__init__(*args, **kwargs)
        self.fields['text'].help_text = (
            '<b class="char-counter">140</b> characters left')
        # it's a NOT NULL field but it defaults to NOW()
        # in the views code
        self.fields['send_date'].required = False

        if event.tags.all():

            def pack_tags(tags):
                return '[%s]' % (','.join('"%s"' % x for x in tags))

            self.fields['text'].help_text += (
                '<br><a href="#" class="include-event-tags" '
                'data-tags=\'%s\'>include all event tags</a>' %
                pack_tags([x.name for x in event.tags.all()]))

        if event.placeholder_img:
            from airmozilla.main.helpers import thumbnail
            thumb = thumbnail(event.placeholder_img, '100x100')

            self.fields['include_placeholder'].help_text = (
                '<img src="%(url)s" alt="placeholder" class="thumbnail" '
                'width="%(width)s" width="%(height)s">' % {
                    'url': thumb.url,
                    'width': thumb.width,
                    'height': thumb.height
                })
        else:
            del self.fields['include_placeholder']

        if event.location:
            self.fields['send_date'].help_text = ('Timezone is %s' %
                                                  event.location.timezone)
Ejemplo n.º 6
0
 def render(self, name, value, attrs=None, **__):
     if value:
         picture = Picture.objects.get(id=value)
         thumb = thumbnail(picture.file, '96x54', crop='center')
         img = ('<img src="%s" width="%d" height="%d" alt="%s">' %
                (thumb.url, thumb.width, thumb.height,
                 picture.notes and cgi.escape(picture.notes) or ''))
         html = ('<input type="hidden" name="%s" value="%d">'
                 '<a href="%s" title="Current picture">%s</a> ' % (
                     name,
                     picture.id,
                     reverse('manage:picture_edit', args=(picture.id, )),
                     img,
                 ))
         if self.editable:
             html += ('<a href="%s?event=%d" '
                      'title="This will leave the editing without saving"'
                      '>Pick another</a>' %
                      (reverse('manage:picturegallery'), self.instance.id))
         else:
             html += ('You can pick a different picture later')
         return mark_safe(html)
     else:
         html = ('<a href="%s?event=%d" '
                 'title="This will leave the editing without saving">'
                 'Pick a picture from the gallery</a>' % (
                     reverse('manage:picturegallery'),
                     self.instance.id,
                 ))
         return mark_safe(html)
Ejemplo n.º 7
0
    def get_object(self, request, channel_slug=None):
        self._root_url = get_base_url(request)
        if channel_slug:
            self.channel = get_object_or_404(
                Channel,
                slug__iexact=channel_slug
            )
        else:
            self.channel = Channel.objects.get(
                slug=settings.DEFAULT_CHANNEL_SLUG
            )
        if self.channel.cover_art:
            self.itunes_lg_url = thumbnail(
                self.channel.cover_art, '1400x1400'
            ).url
            self.itunes_sm_url = thumbnail(
                self.channel.cover_art, '144x144'
            ).url
            if self.itunes_lg_url.startswith('//'):
                # e.g. //cdn.example.com/media/cache/file.png
                protocol = self._root_url.split('//')[0]
                self.itunes_lg_url = protocol + self.itunes_lg_url
                self.itunes_sm_url = protocol + self.itunes_sm_url
            elif '://' not in self.itunes_lg_url:
                self.itunes_lg_url = self._root_url + self.itunes_lg_url
                self.itunes_sm_url = self._root_url + self.itunes_sm_url
        else:
            # Use the default ones
            self.itunes_sm_url = get_abs_static(
                'main/img/podcast-cover-144x144.png',
                request
            )
            self.itunes_lg_url = get_abs_static(
                'main/img/podcast-cover-1400x1400.png',
                request
            )

        super(ITunesFeed, self).get_object(request)
Ejemplo n.º 8
0
def thumbnails(request):
    form = forms.ThumbnailsForm(request.GET)
    if not form.is_valid():
        return http.HttpResponseBadRequest(form.errors)
    id = form.cleaned_data['id']
    width = form.cleaned_data['width']
    height = form.cleaned_data['height']
    geometry = '%sx%s' % (width, height)
    event = get_object_or_404(Event, id=id)
    thumbnails = []
    for picture in Picture.objects.filter(event=event).order_by('created'):
        thumb = thumbnail(picture.file, geometry, crop='center')
        thumbnails.append(thumb.url)

    return {'thumbnails': thumbnails}
Ejemplo n.º 9
0
def thumbnails(request):
    form = forms.ThumbnailsForm(request.GET)
    if not form.is_valid():
        return http.HttpResponseBadRequest(form.errors)
    id = form.cleaned_data['id']
    width = form.cleaned_data['width']
    height = form.cleaned_data['height']
    geometry = '%sx%s' % (width, height)
    event = get_object_or_404(Event, id=id)
    thumbnails = []
    for picture in Picture.objects.filter(event=event).order_by('created'):
        thumb = thumbnail(picture.file, geometry, crop='center')
        thumbnails.append(thumb.url)

    return {'thumbnails': thumbnails}
Ejemplo n.º 10
0
    def __init__(self, event, *args, **kwargs):
        super(EventTweetForm, self).__init__(*args, **kwargs)
        self.fields['text'].help_text = (
            '<b class="char-counter">140</b> characters left. '
            '<span class="char-counter-warning"><b>Note!</b> Sometimes '
            'Twitter can count it as longer than it appears if you '
            'include a URL. '
            'It\'s usually best to leave a little room.</span>'
        )
        # it's a NOT NULL field but it defaults to NOW()
        # in the views code
        self.fields['send_date'].required = False

        if event.tags.all():

            def pack_tags(tags):
                return '[%s]' % (','.join('"%s"' % x for x in tags))

            self.fields['text'].help_text += (
                '<br><a href="#" class="include-event-tags" '
                'data-tags=\'%s\'>include all event tags</a>'
                % pack_tags([x.name for x in event.tags.all()])
            )

        if event.placeholder_img or event.picture:
            from airmozilla.main.helpers import thumbnail
            if event.picture:
                pic = event.picture.file
            else:
                pic = event.placeholder_img
            thumb = thumbnail(pic, '160x90', crop='center')

            self.fields['include_placeholder'].help_text = (
                '<img src="%(url)s" alt="placeholder" class="thumbnail" '
                'width="%(width)s" width="%(height)s">' %
                {
                    'url': thumb.url,
                    'width': thumb.width,
                    'height': thumb.height
                }
            )
        else:
            del self.fields['include_placeholder']

        if event.location:
            self.fields['send_date'].help_text = (
                'Timezone is %s' % event.location.timezone
            )
Ejemplo n.º 11
0
    def test_thumbnail_with_some_integrityerrors(self, mocked_get_thumbnail,
                                                 mocked_time):

        runs = []

        def proxy(*args, **kwargs):
            runs.append(args)
            if len(runs) < 3:
                raise IntegrityError('bla')
            return get_thumbnail(*args, **kwargs)

        mocked_get_thumbnail.side_effect = proxy

        nailed = thumbnail(os.path.basename(self.destination), '10x10')
        eq_(nailed.width, 10)
        # we don't want these lying around in local install
        nailed.delete()
Ejemplo n.º 12
0
    def test_thumbnail_with_some_integrityerrors(self, mocked_get_thumbnail,
                                                 mocked_time):

        runs = []

        def proxy(*args, **kwargs):
            runs.append(args)
            if len(runs) < 3:
                raise IntegrityError('bla')
            return get_thumbnail(*args, **kwargs)

        mocked_get_thumbnail.side_effect = proxy

        nailed = thumbnail(os.path.basename(self.destination), '10x10')
        eq_(nailed.width, 10)
        # we don't want these lying around in local install
        nailed.delete()
Ejemplo n.º 13
0
 def render(self, name, value, attrs=None, **__):
     if value:
         picture = Picture.objects.get(id=value)
         thumb = thumbnail(picture.file, '96x54', crop='center')
         img = (
             '<img src="%s" width="%d" height="%d" alt="%s">' % (
                 thumb.url,
                 thumb.width,
                 thumb.height,
                 picture.notes and cgi.escape(picture.notes) or ''
             )
         )
         html = (
             '<input type="hidden" name="%s" value="%d">'
             '<a href="%s" title="Current picture">%s</a> ' % (
                 name,
                 picture.id,
                 reverse('manage:picture_edit', args=(picture.id,)),
                 img,
             )
         )
         if self.editable:
             html += (
                 '<a href="%s?event=%d" '
                 'title="This will leave the editing without saving"'
                 '>Pick another</a>' % (
                     reverse('manage:picturegallery'),
                     self.instance.id
                 )
             )
         else:
             html += (
                 'You can pick a different picture later'
             )
         return mark_safe(html)
     else:
         html = (
             '<a href="%s?event=%d" '
             'title="This will leave the editing without saving">'
             'Pick a picture from the gallery</a>' % (
                 reverse('manage:picturegallery'),
                 self.instance.id,
             )
         )
         return mark_safe(html)
Ejemplo n.º 14
0
def send_tweet(event_tweet, save=True):
    if event_tweet.include_placeholder:
        thumb = thumbnail(event_tweet.event.placeholder_img, '300x300')
        file_path = thumb.storage.path(thumb.name)
    else:
        file_path = None

    text = event_tweet.text
    # due to a bug in twython
    # https://github.com/ryanmcgrath/twython/issues/154
    # we're not able to send non-ascii characters properly
    # Hopefully this can go away sometime soon.
    text = text.encode('utf-8')
    try:
        tweet_id = _send(text, file_path=file_path)
        event_tweet.tweet_id = tweet_id
        event_tweet.error = None
    except Exception, msg:
        logging.error("Failed to send tweet", exc_info=True)
        event_tweet.error = str(msg)
Ejemplo n.º 15
0
def send_tweet(event_tweet, save=True):
    if event_tweet.include_placeholder:
        thumb = thumbnail(event_tweet.event.placeholder_img, "300x300")
        file_path = thumb.storage.path(thumb.name)
    else:
        file_path = None

    text = event_tweet.text
    # due to a bug in twython
    # https://github.com/ryanmcgrath/twython/issues/154
    # we're not able to send non-ascii characters properly
    # Hopefully this can go away sometime soon.
    text = text.encode("utf-8")
    try:
        tweet_id = _send(text, file_path=file_path)
        event_tweet.tweet_id = tweet_id
        event_tweet.error = None
    except Exception, msg:
        logging.error("Failed to send tweet", exc_info=True)
        event_tweet.error = str(msg)
Ejemplo n.º 16
0
    def __init__(self, event, *args, **kwargs):
        super(EventTweetForm, self).__init__(*args, **kwargs)
        self.fields['text'].help_text = (
            '<b class="char-counter">140</b> characters left'
        )
        # it's a NOT NULL field but it defaults to NOW()
        # in the views code
        self.fields['send_date'].required = False

        if event.tags.all():

            def pack_tags(tags):
                return '[%s]' % (','.join('"%s"' % x for x in tags))

            self.fields['text'].help_text += (
                '<br><a href="#" class="include-event-tags" '
                'data-tags=\'%s\'>include all event tags</a>'
                % pack_tags([x.name for x in event.tags.all()])
            )

        if event.placeholder_img:
            from airmozilla.main.helpers import thumbnail
            thumb = thumbnail(event.placeholder_img, '100x100')

            #from sorl.thumbnail import get_thumbnail
            self.fields['include_placeholder'].help_text = (
                '<img src="%(url)s" alt="placeholder" class="thumbnail" '
                'width="%(width)s" width="%(height)s">' %
                {
                    'url': thumb.url,
                    'width': thumb.width,
                    'height': thumb.height
                }
            )
        else:
            del self.fields['include_placeholder']

        if event.location:
            self.fields['send_date'].help_text = (
                'Timezone is %s' % event.location.timezone
            )
Ejemplo n.º 17
0
def send_tweet(event_tweet, save=True):
    if event_tweet.include_placeholder:
        if event_tweet.event.picture:
            pic = event_tweet.event.picture.file
        else:
            pic = event_tweet.event.placeholder_img
        thumb = thumbnail(
            pic,
            '385x218',  # 16/9 ratio
            crop='center')
        file_path = thumb.storage.path(thumb.name)
    else:
        file_path = None

    try:
        tweet_id = _send(event_tweet.text, file_path=file_path)
        event_tweet.tweet_id = tweet_id
        event_tweet.error = None
    except Exception, msg:
        logging.error("Failed to send tweet", exc_info=True)
        event_tweet.error = str(msg)
        event_tweet.failed_attempts += 1
Ejemplo n.º 18
0
def send_tweet(event_tweet, save=True):
    if event_tweet.include_placeholder:
        if event_tweet.event.picture:
            pic = event_tweet.event.picture.file
        else:
            pic = event_tweet.event.placeholder_img
        thumb = thumbnail(
            pic,
            '385x218',  # 16/9 ratio
            crop='center'
        )
        file_path = thumb.storage.path(thumb.name)
    else:
        file_path = None

    try:
        tweet_id = _send(event_tweet.text, file_path=file_path)
        event_tweet.tweet_id = tweet_id
        event_tweet.error = None
    except Exception, msg:
        logging.error("Failed to send tweet", exc_info=True)
        event_tweet.error = str(msg)
        event_tweet.failed_attempts += 1
Ejemplo n.º 19
0
def get_thumbnail(event):
    image = event.picture and event.picture.file or event.placeholder_img
    geometry = '160x90'
    crop = 'center'

    return thumbnail(image, geometry, crop=crop)
Ejemplo n.º 20
0
 def test_thumbnail(self):
     nailed = thumbnail(os.path.basename(self.destination), '10x10')
     eq_(nailed.width, 10)
     # we don't want these lying around in local install
     nailed.delete()
Ejemplo n.º 21
0
def redirect_picture_thumbnail(request, id):
    picture = get_object_or_404(Picture, id=id)
    geometry = request.GET.get('geometry', '100x100')
    crop = request.GET.get('crop', 'center')
    thumb = thumbnail(picture.file, geometry, crop=crop)
    return redirect(thumb.url)
Ejemplo n.º 22
0
def redirect_picture_thumbnail(request, id):
    picture = get_object_or_404(Picture, id=id)
    geometry = request.GET.get('geometry', '100x100')
    crop = request.GET.get('crop', 'center')
    thumb = thumbnail(picture.file, geometry, crop=crop)
    return redirect(thumb.url)
Ejemplo n.º 23
0
def get_thumbnail(event):
    image = event.picture and event.picture.file or event.placeholder_img
    geometry = '160x90'
    crop = 'center'

    return thumbnail(image, geometry, crop=crop)
Ejemplo n.º 24
0
 def poster_url(geometry='896x504', crop='center'):
     image = event.picture and event.picture.file or event.placeholder_img
     return thumbnail(image, geometry, crop=crop).url
Ejemplo n.º 25
0
 def test_thumbnail(self):
     nailed = thumbnail(os.path.basename(self.destination), '10x10')
     eq_(nailed.width, 10)
     # we don't want these lying around in local install
     nailed.delete()