Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_save_marks_changed(self):
self.failUnlessEqual(Publishable.PUBLISH_DEFAULT, self.flat_page.publish_state)
self.flat_page.save(mark_changed=False)
self.failUnlessEqual(Publishable.PUBLISH_DEFAULT, self.flat_page.publish_state)
self.flat_page.save()
self.failUnlessEqual(Publishable.PUBLISH_CHANGED, self.flat_page.publish_state)
slugs.append(self.slug)
return slugs
def get_absolute_url(self):
url = '/'.join(self._get_all_slugs())
if self.is_public:
return reverse_url('public_page_detail', args=[url])
else:
return reverse_url('draft_page_detail', args=[url])
class PageBlock(Publishable):
page = models.ForeignKey(Page)
content = models.TextField(blank=True)
image = models.ForeignKey('Image', blank=True, null=True)
class Image(Publishable):
title = models.CharField(max_length=100)
image = models.ImageField(upload_to='images/')
def __unicode__(self):
return self.title
class Category(Publishable):
name = models.CharField(max_length=200)
slug = models.CharField(max_length=100, db_index=True)
def __unicode__(self):
return self.name
def save(self, mark_changed=True, *arg, **kw):
if not self.is_public and mark_changed:
if self.publish_state == Publishable.PUBLISH_DELETE:
raise PublishException("Attempting to save model marked for deletion")
self.publish_state = Publishable.PUBLISH_CHANGED
super(Publishable, self).save(*arg, **kw)
def _changes_need_publishing(self):
return self.publish_state == Publishable.PUBLISH_CHANGED or not self.public
# copy over many-to-many fields
for field in self._meta.many_to_many:
name = field.name
if name in excluded_fields:
continue
m2m_manager = getattr(self, name)
public_objs = list(m2m_manager.all())
field_object, model, direct, m2m = self._meta.get_field_by_name(name)
through_model = self._get_through_model(field_object)
if through_model:
# see if we can work out which reverse relationship this is
# see if we are using our own "through" table or not
if issubclass(through_model, Publishable):
# this will be db name (e.g. with _id on end)
m2m_reverse_name = field_object.m2m_reverse_name()
for reverse_field in through_model._meta.fields:
if reverse_field.column == m2m_reverse_name:
related_name = reverse_field.name
related_field = getattr(through_model, related_name).field
reverse_name = related_field.related.get_accessor_name()
reverse_fields_to_publish.append(reverse_name)
break
continue # m2m via through table won't be dealt with here
related = field_object.rel.to
if issubclass(related, Publishable):
public_objs = [p._get_public_or_publish(dry_run=dry_run, all_published=all_published, parent=self) for p in public_objs]
if not dry_run:
def is_publishable_filter(f):
return bool(f.rel) and issubclass(f.rel.to, Publishable)
def draft_and_deleted(self):
return self.filter(Publishable.Q_DRAFT | Publishable.Q_DELETED)
# page (as a child)
class PageBlock(Publishable):
page=models.ForeignKey('Page')
content = models.TextField(blank=True)
# non-publishable reverse relation to page (as a child)
class Comment(models.Model):
page=models.ForeignKey('Page')
comment = models.TextField()
def update_pub_date(page, field_name, value):
# ignore value entirely and replace with now
setattr(page, field_name, update_pub_date.pub_date)
update_pub_date.pub_date = datetime.now()
class Page(Publishable):
slug = models.CharField(max_length=100, db_index=True)
title = models.CharField(max_length=200)
content = models.TextField(blank=True)
pub_date = models.DateTimeField(default=datetime.now)
parent = models.ForeignKey('self', blank=True, null=True)
authors = models.ManyToManyField(Author, blank=True)
log = models.ManyToManyField(ChangeLog, blank=True)
tags = models.ManyToManyField(Tag, through='PageTagOrder', blank=True)
class Meta:
ordering = ['slug']
class PublishMeta(Publishable.PublishMeta):
publish_exclude_fields = ['log']
class Meta:
ordering = ['url']
def get_absolute_url(self):
if self.is_public:
return self.url
return '%s*' % self.url
class Author(Publishable):
name = models.CharField(max_length=100)
profile = models.TextField(blank=True)
class PublishMeta(Publishable.PublishMeta):
publish_reverse_fields = ['authorprofile']
class AuthorProfile(Publishable):
author = models.OneToOneField(Author)
extra_profile = models.TextField(blank=True)
class ChangeLog(models.Model):
changed = models.DateTimeField(db_index=True, auto_now_add=True)
message = models.CharField(max_length=200)
class Tag(models.Model):
title = models.CharField(max_length=100, unique=True)
slug = models.CharField(max_length=100)
# publishable model with a reverse relation to
# page (as a child)
class PageBlock(Publishable):
page=models.ForeignKey('Page')
content = models.TextField(blank=True)