Beispiel #1
0
    def clean(self, *args, **kwargs):
        if 'url' in self.cleaned_data:
            url = self.cleaned_data['url']
            try:
                params = {}
                
                if settings.OEMBED_MAX_WIDTH:
                    params.update({'maxwidth': settings.OEMBED_MAX_WIDTH})
                    
                if settings.OEMBED_MAX_HEIGHT:
                    params.update({'maxheight': settings.OEMBED_MAX_HEIGHT})

                response = DefaultOEmbedConsumer.embed(url, **params)

                logger.debug('Found OEmbed info for %s' % url)
                for field in response:
                    if hasattr(self.instance, field) and \
                            not getattr(self.instance, field):
                        logger.debug('Setting field %s: %s' \
                                        % (field, response[field]))
                        setattr(self.instance, field, response[field])

                        # If we set the title, also set the slug
                        if field == 'title' and not self.instance.slug:
                            logger.debug('Setting title with slug')
                            
                            self.instance.slug = \
                                make_unique_slug(Post.objects.all(), \
                                                 response['title'])

            except OEmbedError, e:
                logger.warn('Something went wrong with OEmbed: %s' % e)

                raise forms.ValidationError(_('Something went wrong looking \
                    up embed information for this URL: %s') % e)
Beispiel #2
0
    def clean(self, *args, **kwargs):
        if 'url' in self.cleaned_data:
            url = self.cleaned_data['url']
            try:
                params = {}

                if settings.OEMBED_MAX_WIDTH:
                    params.update({'maxwidth': settings.OEMBED_MAX_WIDTH})

                if settings.OEMBED_MAX_HEIGHT:
                    params.update({'maxheight': settings.OEMBED_MAX_HEIGHT})

                response = DefaultOEmbedConsumer.embed(url, **params)

                logger.debug('Found OEmbed info for %s' % url)
                for field in response:
                    if hasattr(self.instance, field) and \
                            not getattr(self.instance, field):
                        logger.debug('Setting field %s: %s' \
                                        % (field, response[field]))
                        setattr(self.instance, field, response[field])

                        # If we set the title, also set the slug
                        if field == 'title' and not self.instance.slug:
                            logger.debug('Setting title with slug')

                            self.instance.slug = \
                                make_unique_slug(self.Meta.model.objects.all(), \
                                                 response['title'])

            except OEmbedError, e:
                logger.warn('Something went wrong with OEmbed: %s' % e)

                raise forms.ValidationError(
                    _('Something went wrong looking \
                    up embed information for this URL: %s') % e)