Commits

hadaraz committed 0dc171e

Comments (0)

Files changed (10)

+syntax: glob
+*.pyc
+*.db
+.DS_Store
+~*
+*~
+dist
+MANIFEST

__init__.py

Empty file added.

metatags/__init__.py

Empty file added.

metatags/admin.py

+from django.contrib import admin
+from metatags.models import Metatag, MetatagType
+
+class MetatagAdmin(admin.ModelAdmin):
+    list_display = ('type', '_content_object', 'content_type', 'object_id')
+    list_filter = ('type','content_type')
+    ordering = ('content_type','object_id')
+
+admin.site.register(MetatagType)
+admin.site.register(Metatag, MetatagAdmin)

metatags/fixtures/initial_data.json

+[
+    {
+        "pk": 3, 
+        "model": "metatags.metatagtype", 
+        "fields": {
+            "attribute": "name", 
+            "value": "author"
+        }
+    }, 
+    {
+        "pk": 6, 
+        "model": "metatags.metatagtype", 
+        "fields": {
+            "attribute": "http-equiv", 
+            "value": "content-type"
+        }
+    }, 
+    {
+        "pk": 1, 
+        "model": "metatags.metatagtype", 
+        "fields": {
+            "attribute": "name", 
+            "value": "description"
+        }
+    }, 
+    {
+        "pk": 2, 
+        "model": "metatags.metatagtype", 
+        "fields": {
+            "attribute": "name", 
+            "value": "keywords"
+        }
+    }, 
+    {
+        "pk": 5, 
+        "model": "metatags.metatagtype", 
+        "fields": {
+            "attribute": "http-equiv", 
+            "value": "refresh"
+        }
+    }, 
+    {
+        "pk": 4, 
+        "model": "metatags.metatagtype", 
+        "fields": {
+            "attribute": "http-equiv", 
+            "value": "robots"
+        }
+    }, 
+    {
+        "pk": 1, 
+        "model": "metatags.metatag", 
+        "fields": {
+            "content": "Metatag Example", 
+            "scheme": "", 
+            "type": 1, 
+            "object_id": 1, 
+            "content_type": 9
+        }
+    }, 
+    {
+        "pk": 2, 
+        "model": "metatags.metatag", 
+        "fields": {
+            "content": "Metatag, Django", 
+            "scheme": "", 
+            "type": 2, 
+            "object_id": 1, 
+            "content_type": 9
+        }
+    }
+]

metatags/managers.py

+from django.db import models
+from django.contrib.contenttypes.models import ContentType
+from django.utils.encoding import force_unicode
+
+class MetaTagManager(models.Manager):
+
+    def for_model(self, model):
+        """
+        QuerySet for all comments for a particular model (either an instance or
+        a class).
+        """
+        ct = ContentType.objects.get_for_model(model)
+        qs = self.get_query_set().filter(content_type=ct)
+        if isinstance(model, models.Model):
+            qs = qs.filter(object_id=model.id)
+        return qs

metatags/models.py

+from django.db import models
+from django.contrib.contenttypes.models import ContentType
+from django.contrib.contenttypes import generic
+from django.utils.translation import ugettext_lazy as _
+from metatags.managers import MetaTagManager
+from django.contrib import admin
+
+ATTRIBUTE_CHOICES = (
+    ('name','name'),
+    ('http-equiv','http-equiv'),
+)
+
+class MetatagType(models.Model):
+    #metatag attribute list
+    attribute   = models.CharField(_('attribute'),max_length=64,choices=ATTRIBUTE_CHOICES)
+    value       = models.CharField(_('value'),max_length=128)
+    
+    class Meta:
+        ordering = ["value"]
+
+    def __unicode__(self):
+        return self.value
+
+class Metatag(models.Model):
+    #meta tag attributes
+    type        = models.ForeignKey(MetatagType)
+    content     = models.TextField(_('content'), help_text = _('Content of the metatag.'))
+    scheme      = models.CharField(_('scheme'), max_length=64, blank=True, help_text = _('Scheme attribute'))
+    
+    # Content-object field
+    content_type   = models.ForeignKey(ContentType,
+            related_name="content_type_set_for_%(class)s")
+    object_id      = models.PositiveIntegerField(_('object ID'))
+    content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_id")
+
+    class meta():
+        unique_together = ("type","content_object")
+        verbose_name = _('Html metatag')
+        verbose_name_plural = _('Html metatags')
+
+    objects = MetaTagManager()
+    
+    def _content_object(self):
+        return "%s" % self.content_object
+    _content_object.short_description = "Content object"
+
+    def __unicode__(self):
+        return "%s: %s" % (self.type, self.content_object)
+    

metatags/templates/metatags.html

+{% for metatag in metatags %}
+    <meta {{metatag.type.attribute}}="{{metatag.type.value}}" content="{{metatag.content}}" {% if metatag.scheme %}scheme="metatag.scheme" {% endif %}/> {% endfor %}

metatags/templatetags/__init__.py

Empty file added.

metatags/templatetags/metatag.py

+from django import template
    """
    Gets a list of html metatags for the given object. Returns the list 
    as the given varible name.
    
    Syntax::
        {% get_metatags [object] %}
    Example usage::
        {% get_metatags flatpage %}
    """
    metatags = Metatag.objects.for_model(obj)
    return {'metatags': metatags}