Example #1
0
    def test_simpleseo(self):
        raw_sitemap = self.client.get('/sitemap.xml')
        if raw_sitemap.status_code == 200:
            sitemap = ET.fromstring(raw_sitemap.content)
            namespaces = {'sm': 'http://www.sitemaps.org/schemas/sitemap/0.9'}
            for loc in sitemap.findall('sm:url/sm:loc', namespaces=namespaces):
                path = urlparse(loc.text).path
                if any(exclude in path for exclude in self.exclude_list):
                    continue
                self.location_list.append(path)

        for path in self.location_list:
            path = force_text(unquote(path))
            seo = SeoMetadata.objects.create(
                path=path, lang_code=get_generic_lang_code(),
                title=''.join(choice(ascii_letters) for _ in range(20)),
                description=''.join(choice(ascii_letters) for _ in range(120))
            )
            print('Test path: {0}'.format(path))
            response = self.client.get(path, follow=True)
            if response.status_code == 404:
                print('Warning: page not found')
            content = force_text(response.content[:1000])
            self.assertIn(seo.title, content)
            self.assertIn(seo.description, content)
Example #2
0
    def test_simpleseo(self):
        raw_sitemap = self.client.get('/sitemap.xml')
        if raw_sitemap.status_code == 200:
            sitemap = ET.fromstring(raw_sitemap.content)
            namespaces = {'sm': 'http://www.sitemaps.org/schemas/sitemap/0.9'}
            for loc in sitemap.findall('sm:url/sm:loc', namespaces=namespaces):
                path = urlparse(loc.text).path
                if any(exclude in path for exclude in self.exclude_list):
                    continue
                self.location_list.append(path)

        for path in self.location_list:
            path = force_str(unquote(path))
            seo = SeoMetadata.objects.create(
                path=path,
                lang_code=get_generic_lang_code(),
                title=''.join(choice(ascii_letters) for _ in range(20)),
                description=''.join(choice(ascii_letters) for _ in range(120)))
            print('Test path: {0}'.format(path))
            response = self.client.get(path, follow=True)
            if response.status_code == 404:
                print('Warning: page not found')
            content = force_str(response.content[:1000])
            self.assertIn(seo.title, content)
            self.assertIn(seo.description, content)
Example #3
0
def get_seo(context, **kwargs):
    path = context['request'].path

    metadata = SeoMetadata.objects.filter(
        path=path,
        lang_code__in=[get_language(), get_generic_lang_code()]).order_by(
            '-lang_code').first()  # lang_code: if en-gb not found, try en
    metadata = model_to_dict(metadata) if metadata is not None else {}
    result = {}
    for seo in ['title', 'description', 'keywords', 'text']:
        result[seo] = (metadata.get(seo) or kwargs.get(seo) or getattr(
            settings, 'SEO_DEFAULT_{0}'.format(seo.upper()), ''))
    return result
Example #4
0
def get_seo(context, **kwargs):
    path = context['request'].path

    metadata = SeoMetadata.objects.filter(
        path=path, lang_code__in=[get_language(), get_generic_lang_code()]
    ).order_by('-lang_code').first()  # lang_code: if en-gb not found, try en
    metadata = model_to_dict(metadata) if metadata is not None else {}
    result = {}
    for seo in ['title', 'description', 'keywords', 'text']:
        result[seo] = (
            metadata.get(seo) or kwargs.get(seo) or
            getattr(settings, 'SEO_DEFAULT_{0}'.format(seo.upper()), '')
        )
    return result
Example #5
0
class SeoMetadata(models.Model):
    content_type = models.ForeignKey(ContentType,
                                     on_delete=models.CASCADE,
                                     null=True,
                                     blank=True)
    object_id = models.PositiveIntegerField(null=True, blank=True)
    content_object = GenericForeignKey('content_type', 'object_id')
    path = models.CharField(verbose_name=_('Path'),
                            max_length=255,
                            db_index=True,
                            help_text=_(
                                "This should be an absolute path, excluding "
                                "the domain name. Example: '/foo/bar/'."))
    lang_code = models.CharField(verbose_name=_('Language'),
                                 max_length=255,
                                 choices=settings.LANGUAGES,
                                 default=get_generic_lang_code())
    title = models.CharField(
        verbose_name=_('Title'),
        max_length=255,
        blank=True,
        help_text=_("Recommended length: up to 70 symbols"))
    description = models.CharField(
        verbose_name=_('Description'),
        max_length=255,
        blank=True,
        help_text=_("Recommended length: up to 160 symbols."))
    keywords = models.CharField(
        verbose_name=_('Keywords'),
        max_length=255,
        blank=True,
        help_text=_("Recommended length: up to 10 keyword phrases."))
    text = models.TextField(verbose_name=_('Text'), blank=True)

    class Meta:
        verbose_name = _('SEO metadata')
        verbose_name_plural = _('SEO metadata')
        db_table = 'seo_metadata'
        unique_together = (('path', 'lang_code'), )
        ordering = ('path', 'lang_code')

    def __str__(self):
        return "Language: %s | URL: %s" % (self.lang_code, self.path)

    def get_absolute_url(self):
        return self.path