Guest User

Untitled

a guest
Dec 21st, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. class ItemBase(models.Model):
  2.     post = models.ForeignKey(Post, related_name='%(class)s_related')
  3.     title = models.CharField(max_length=250)
  4.     created = models.DateTimeField(auto_now_add=True)
  5.     updated = models.DateTimeField(auto_now=True)
  6.  
  7.     class Meta:
  8.         abstract = True
  9.  
  10.     def __str__(self):
  11.         return self.title
  12.  
  13.     def render(self):
  14.         return render_to_string('content/items/{}.html'.format(self._meta.model_name), {'item': self})
  15.  
  16.  
  17. class Image(ItemBase):
  18.     image_url = models.URLField(blank=True, null=True)
  19.     image = models.ImageField(upload_to='posts/%Y/%m/%d', blank=True, null=True)
  20.  
  21.     def get_remote_image(self):
  22.         if self.image_url and not self.image_file:
  23.             result = urllib.urlretrieve(self.image_url)
  24.             self.image_file.save(
  25.                     self.title,
  26.                     File(open(result[0]))
  27.                     )
  28.             self.save()
Advertisement
Add Comment
Please, Sign In to add comment