Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. from django.contrib.sites.models import Site
  4. import datetime
  5.  
  6. CONTENT_TYPES = (
  7.     ('txt', 'text'),
  8.     ('img', 'image'),
  9.     ('vid', 'video'),
  10.     ('snd', 'sound'),
  11. )
  12.  
  13. FEATURE_TYPES = (
  14.     (0, 'share'),
  15.     (1, 'rate'),
  16.     (2, 'comment'),
  17.     (3, 'bookmark'),
  18.     (4, 'search'),
  19. )
  20.  
  21. FEATURE_LOOKUP = dict([(a[1], a[0]) for a in FEATURE_TYPES])
  22.  
  23. class DateAwareModel(models.Model):
  24.     inserted = models.DateTimeField(auto_now_add=True, editable=False)
  25.     updated = models.DateTimeField(auto_now=True, editable=False)    
  26.  
  27.     class Meta:
  28.         abstract = True
  29.  
  30. class UserAwareModel(models.Model):
  31.     user = models.ForeignKey(User)
  32.  
  33.     class Meta:
  34.         abstract = True
  35.  
  36. class Node(DateAwareModel,UserAwareModel):
  37.     parent = models.ForeignKey(
  38.         'self',
  39.         related_name='children',
  40.         blank=True,
  41.         null=True)
  42.  
  43. class Comment(Node):
  44.     content = models.TextField(blank=True)
  45.  
  46. class Tag(Node):
  47.     content = models.CharField(max_length=64)
  48.  
  49. class RBGroup(models.Model):
  50.     name = models.CharField(max_length=250)
  51.     short_name = models.CharField(max_length=25)
  52.     language = models.CharField(max_length=25,default="en")
  53.     blessed_tags = models.CharField(max_length=250,blank=True)
  54.     valid_domains = models.CharField(max_length=250,blank=True)
  55.  
  56.     # black/whitelist fields
  57.     anno_whitelist = models.CharField(max_length=250,blank=True)
  58.     img_whitelist = models.CharField(max_length=250,blank=True)
  59.     img_blacklist = models.CharField(max_length=250,blank=True)
  60.     no_readr = models.CharField(max_length=250,blank=True)
  61.    
  62.     # logo fields
  63.     logo_url_sm = models.URLField(blank=True,verify_exists=False)
  64.     logo_url_med = models.URLField(blank=True,verify_exists=False)
  65.     logo_url_lg = models.URLField(blank=True,verify_exists=False)
  66.    
  67.     # css
  68.     css_url = models.URLField(blank=True,verify_exists=False)
  69.    
  70.     def get_feature(self, name):
  71.         try:
  72.             feature_id = FEATURE_LOOKUP[name]
  73.         except:
  74.             raise Exception("Invalid feature name")
  75.         try:
  76.             return self.feature_set.values('text','images','flash').get(kind=feature_id)
  77.         except:
  78.             raise Exception("Feature instance not yet created")
  79.  
  80.     # TODO: write code to overwrite save method + create feature instances on
  81.     # the first save of the model.
  82.     # def save(self):
  83.  
  84.     def __unicode__(self):
  85.         return self.name
  86.        
  87.     class Meta:
  88.         ordering = ['short_name']
  89.  
  90. class Feature(models.Model):
  91.     kind = models.PositiveSmallIntegerField(choices=FEATURE_TYPES,default=1)
  92.     text = models.BooleanField()
  93.     images = models.BooleanField()
  94.     flash = models.BooleanField()
  95.     rb_group = models.ForeignKey(RBGroup,default=1)
  96.  
  97.     def __unicode__(self):
  98.         return (self.rb_group.short_name +":"+  FEATURE_TYPES[self.kind][1])
  99.  
  100. class RBSite(Site):
  101.     rb_group = models.ForeignKey(RBGroup)
  102.     include_selectors = models.CharField(max_length=250, blank=True)
  103.     no_rdr_selectors = models.CharField(max_length=250, blank=True)
  104.     css = models.URLField(blank=True)
  105.  
  106. class RBPage(models.Model):
  107.     rb_site = models.ForeignKey(RBSite)
  108.     url = models.URLField(verify_exists=False)
  109.     canonical_url = models.URLField(verify_exists=False)
  110.  
  111.     def __unicode__(self):
  112.         return self.canonical_url
  113.  
  114. class ContentNode(DateAwareModel):
  115.     user = models.ForeignKey(User)
  116.     content_type = models.CharField(max_length=3, choices=CONTENT_TYPES)
  117.     rb_page = models.ForeignKey(RBPage)
  118.     hash = models.CharField(max_length=32, editable=True)
  119.  
  120.     def __unicode__(self):
  121.         return self.hash
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement