Skip to content
This repository has been archived by the owner on Oct 6, 2018. It is now read-only.

Commit

Permalink
Add a direct relationship from Upload to UploadGroup which automatica…
Browse files Browse the repository at this point in the history
…lly updates
  • Loading branch information
senturio committed Apr 17, 2013
1 parent 7fb666c commit e7d99f8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 40 deletions.
2 changes: 2 additions & 0 deletions batter/torrents/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def forwards(self, orm):
('uploader', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'])),
('parent_content_type', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['contenttypes.ContentType'])),
('parent_object_id', self.gf('django.db.models.fields.PositiveIntegerField')()),
('upload_group', self.gf('django.db.models.fields.related.ForeignKey')(related_name=u'uploads', to=orm['torrents.UploadGroup'])),
))
db.send_create_signal(u'torrents', ['Upload'])

Expand Down Expand Up @@ -137,6 +138,7 @@ def backwards(self, orm):
'parent_content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
'parent_object_id': ('django.db.models.fields.PositiveIntegerField', [], {}),
'torrent': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "u'upload'", 'unique': 'True', 'to': u"orm['torrents.Torrent']"}),
'upload_group': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "u'uploads'", 'to': u"orm['torrents.UploadGroup']"}),
'uploader': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"})
},
u'torrents.uploadgroup': {
Expand Down
52 changes: 13 additions & 39 deletions batter/torrents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,27 +241,23 @@ class Upload(InheritingModel, TimeStampedModel):
'parent_content_type', 'parent_object_id'
)

# this is used for quickly getting to all the uploads associated with
# an UploadGroup
upload_group = models.ForeignKey('UploadGroup', related_name='uploads')

def save(self, *args, **kwargs):
# rewrite my upload_group
current = self
while not isinstance(current, UploadGroup):
current = current.parent
self.upload_group = current

super(Upload, self).save(*args, **kwargs)

@staticmethod
def get_superclass_model():
return Upload

@staticmethod
def filter_queryset_for_parent(
self, qs, data
):
q = None
for dataset in data:
my_q = models.Q(
parent_content_type=dataset['content_type'],
parent_object_id=dataset['id']
)
if q is None:
q = my_q
else:
q = q | my_q

return qs.filter(q)


class UploadGroup(InheritingDescendingModel, TimeStampedModel):
tags = TaggableManager()
Expand All @@ -274,28 +270,6 @@ def get_child_model():
def get_superclass_model():
return UploadGroup

def get_parent_queryset(self):
return [self.pk]

@property
def uploads(self):
# down
chain_top = self.get_child_model()
chain_root = [chain_top]
while not issubclass(chain_top, Upload):
chain_top = chain_top.get_child_model()
chain_root.append(chain_top)

# and back up
qs = [self.pk]
for chain_bit in chain_root:
qs = chain_bit.objects.filter(

)
return chain_top.objects.filter(
**chain_top.get_parent_queryset_filter()
)


def recursive_drop_falsy(d):
"""Recursively drops falsy values from a given data structure."""
Expand Down
19 changes: 18 additions & 1 deletion batter/torrents_inheritance_tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,14 @@ def test_get_by_child_does_not_exist(self):
models.ExcitingGroup.objects.get_by_child(self.boring_group)

def test_get_uploads_from_root(self):
print self.boring_group.uploads
self.assertEquals(
self.boring_upload,
self.boring_group.uploads.get()
)
self.assertEquals(
self.boring_group.uploads.count(),
1
)


class InbetweenerTests(BaseTestCase):
Expand Down Expand Up @@ -207,3 +214,13 @@ def test_going_down(self):
self.inbetweener_group.children.all()[0].children.all()[0],
self.inbetweener_upload
)

def test_get_uploads_from_root(self):
self.assertEquals(
self.inbetweener_upload,
self.inbetweener_group.uploads.get()
)
self.assertEquals(
self.inbetweener_group.uploads.count(),
1
)

0 comments on commit e7d99f8

Please sign in to comment.