Skip to content

Commit

Permalink
Dealing with reverse one two one relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsensible committed Mar 8, 2013
1 parent a573ed4 commit 477815c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
30 changes: 21 additions & 9 deletions publish/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def publish_changes(self, dry_run=False, all_published=None, parent=None):
public_m2m_manager.remove(*old_objs)
public_m2m_manager.add(*public_objs)

# one-to-many reverse relations
# one-to-many and one-to-one reverse relations
for obj in self._meta.get_all_related_objects():
if issubclass(obj.model, Publishable):
name = obj.get_accessor_name()
Expand All @@ -333,16 +333,21 @@ def publish_changes(self, dry_run=False, all_published=None, parent=None):
continue
if obj.field.rel.multiple:
related_items = getattr(self, name).all()
for related_item in related_items:
related_item.publish(dry_run=dry_run, all_published=all_published, parent=self)

# make sure we tidy up anything that needs deleting
if self.public and not dry_run:
public_ids = related_items.values('public_id')
else:
try:
related_items = [getattr(self, name)]
except obj.model.DoesNotExist:
related_items = []

for related_item in related_items:
related_item.publish(dry_run=dry_run, all_published=all_published, parent=self)

# make sure we tidy up anything that needs deleting
if self.public and not dry_run:
if obj.field.rel.multiple:
public_ids = [r.public_id for r in related_items]
deleted_items = getattr(self.public, name).exclude(pk__in=public_ids)
deleted_items.delete(mark_for_deletion=False)
# for deleted_item in deleted_items:
# deleted_item.publish_deletions(dry_run=dry_run, all_published=all_published, parent=self)

self._post_publish(dry_run, all_published)

Expand Down Expand Up @@ -416,6 +421,13 @@ 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)
Expand Down
2 changes: 1 addition & 1 deletion publish/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.http import Http404

from publish.models import Publishable, FlatPage, Site, Page, PageBlock, \
Author, Tag, PageTagOrder, Comment, update_pub_date, \
Author, AuthorProfile, Tag, PageTagOrder, Comment, update_pub_date, \
PublishException

from publish.admin import PublishableAdmin, PublishableStackedInline
Expand Down

0 comments on commit 477815c

Please sign in to comment.