示例#1
0
class UploadedFile(models.Model):
    FILES_ROOT = 'files'  # Directory name inside MEDIA_ROOT

    def _generate_filename(instance, filename):
        """Generate a new unique filename while preserving the original
        filename extension. If an existing UploadedFile gets updated
        do not generate a new filename.
        """

        # Instance is new UploadedFile, generate a filename
        if not instance.id:
            ext = os.path.splitext(filename)[1]
            filename = str(uuid.uuid4()) + ext
            return os.path.join(UploadedFile.FILES_ROOT, filename)

        # Use existing filename.
        obj = UploadedFile.objects.get(id=instance.id)
        return obj.file.name

    file = models.FileField(storage=OverwriteStorage(), upload_to=_generate_filename)
    name = models.CharField(max_length=255)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.name

    @property
    def url(self):
        site_url = getattr(settings, 'CDN_URL', settings.SITE_URL)
        full_url = urljoin(site_url, self.file.url)
        return full_url

    @property
    def snippets(self):
        return Snippet.objects.filter(
            models.Q(data__contains=self.file.url) |
            models.Q(template__code__contains=self.file.url)
        )
示例#2
0
    def __init__(self, client):
        self.client = client
        self.storage = OverwriteStorage()

        self._snippets = None
示例#3
0
class SnippetBundle(object):
    """
    Group of snippets to be sent to a particular client configuration.
    """
    def __init__(self, client):
        self.client = client
        self.storage = OverwriteStorage()

        self._snippets = None

    @property
    def key(self):
        """A unique key for this bundle as a sha1 hexdigest."""
        # Key should consist of snippets that are in the bundle plus any
        # properties of the client that may change the snippet code
        # being sent.
        key_properties = ['{id}-{date}'.format(id=snippet.id, date=snippet.modified.isoformat())
                          for snippet in self.snippets]
        key_properties.extend([
            self.client.startpage_version,
            self.client.locale,
        ])

        key_string = u'_'.join(unicode(prop) for prop in key_properties)
        return hashlib.sha1(key_string).hexdigest()

    @property
    def cache_key(self):
        return u'bundle_' + self.key

    @property
    def expired(self):
        """
        If True, the code for this bundle should be re-generated before
        use.
        """
        return not cache.get(self.cache_key)

    @property
    def filename(self):
        return u'bundles/bundle_{0}.html'.format(self.key)

    @property
    def url(self):
        return self.storage.url(self.filename)

    @property
    def snippets(self):
        # Lazy-load snippets on first access.
        if self._snippets is None:
            self._snippets = (Snippet.cached_objects
                              .filter(disabled=False)
                              .match_client(self.client)
                              .order_by('priority')
                              .select_related('template')
                              .prefetch_related('countries', 'exclude_from_search_providers')
                              .filter_by_available())
        return self._snippets

    def generate(self):
        """Generate and save the code for this snippet bundle."""
        bundle_content = render_to_string('base/fetch_snippets.html', {
            'snippets': self.snippets,
            'client': self.client,
            'locale': self.client.locale,
        })

        if isinstance(bundle_content, unicode):
            bundle_content = bundle_content.encode('utf-8')
        self.storage.save(self.filename, ContentFile(bundle_content))
        cache.set(self.cache_key, True, settings.SNIPPET_BUNDLE_TIMEOUT)
示例#4
0
    def __init__(self, client):
        self.client = client
        self.storage = OverwriteStorage()

        self._snippets = None
示例#5
0
class SnippetBundle(object):
    """
    Group of snippets to be sent to a particular client configuration.
    """
    def __init__(self, client):
        self.client = client
        self.storage = OverwriteStorage()

        self._snippets = None

    @property
    def key(self):
        """A unique key for this bundle as a sha1 hexdigest."""
        # Key should consist of snippets that are in the bundle plus any
        # properties of the client that may change the snippet code
        # being sent.
        key_properties = [snippet.id for snippet in self.snippets]
        key_properties.extend([
            self.client.startpage_version,
            self.client.locale,
        ])

        key_string = u'_'.join(unicode(prop) for prop in key_properties)
        return hashlib.sha1(key_string).hexdigest()

    @property
    def cache_key(self):
        return u'bundle_' + self.key

    @property
    def expired(self):
        """
        If True, the code for this bundle should be re-generated before
        use.
        """
        return not cache.get(self.cache_key)

    @property
    def filename(self):
        return u'bundles/{0}.html'.format(self.key)

    @property
    def url(self):
        return self.storage.url(self.filename)

    @property
    def snippets(self):
        # Lazy-load snippets on first access.
        if self._snippets is None:
            self._snippets = (Snippet.cached_objects
                              .filter(disabled=False)
                              .match_client(self.client)
                              .order_by('priority')
                              .select_related('template')
                              .filter_by_available())
        return self._snippets

    def generate(self):
        """Generate and save the code for this snippet bundle."""
        bundle_content = render_to_string('base/fetch_snippets.html', {
            'snippets': self.snippets,
            'client': self.client,
            'locale': self.client.locale,
        })

        self.storage.save(self.filename, ContentFile(bundle_content))
        cache.set(self.cache_key, True, settings.SNIPPET_BUNDLE_TIMEOUT)
示例#6
0
class SnippetBundle(object):
    """
    Group of snippets to be sent to a particular client configuration.
    """
    def __init__(self, client):
        self.client = client
        self.storage = OverwriteStorage()

        self._snippets = None

    @property
    def key(self):
        """A unique key for this bundle as a sha1 hexdigest."""
        # Key should consist of snippets that are in the bundle plus any
        # properties of the client that may change the snippet code
        # being sent.
        key_properties = [
            '{id}-{date}'.format(id=snippet.id,
                                 date=snippet.modified.isoformat())
            for snippet in self.snippets
        ]
        key_properties.extend([
            self.client.startpage_version,
            self.client.locale,
            self.client.channel,
            SNIPPET_JS_TEMPLATE_HASH,
            SNIPPET_CSS_TEMPLATE_HASH,
            SNIPPET_FETCH_TEMPLATE_HASH,
        ])

        key_string = u'_'.join(unicode(prop) for prop in key_properties)
        return hashlib.sha1(key_string).hexdigest()

    @property
    def cache_key(self):
        return u'bundle_' + self.key

    @property
    def expired(self):
        """
        If True, the code for this bundle should be re-generated before
        use.
        """
        return not cache.get(self.cache_key)

    @property
    def filename(self):
        return u'bundles/bundle_{0}.html'.format(self.key)

    @property
    def url(self):
        bundle_url = self.storage.url(self.filename)
        site_url = getattr(settings, 'CDN_URL', settings.SITE_URL)
        full_url = urljoin(site_url, bundle_url)
        return full_url

    @property
    def snippets(self):
        # Lazy-load snippets on first access.
        if self._snippets is None:
            self._snippets = (Snippet.cached_objects.filter(
                disabled=False).match_client(self.client).order_by(
                    'priority').select_related('template').prefetch_related(
                        'countries',
                        'exclude_from_search_providers').filter_by_available())
        return self._snippets

    def generate(self):
        """Generate and save the code for this snippet bundle."""
        bundle_content = render_to_string(
            'base/fetch_snippets.html', {
                'snippet_ids': [snippet.id for snippet in self.snippets],
                'snippets_json': json.dumps(
                    [s.to_dict() for s in self.snippets]),
                'client': self.client,
                'locale': self.client.locale,
                'settings': settings,
            })

        if isinstance(bundle_content, unicode):
            bundle_content = bundle_content.encode('utf-8')
        self.storage.save(self.filename, ContentFile(bundle_content))
        cache.set(self.cache_key, True, settings.SNIPPET_BUNDLE_TIMEOUT)