Browse files

Fixing PathField (custom field) and updating torrents data model

  • Loading branch information...
1 parent e8d6406 commit e4ee21779983f9b229e1fc36b7722162e9760a1e @yorkedork committed Aug 8, 2011
View
2 media_library/admin.py
@@ -9,8 +9,6 @@ class MediaObjectAdmin(admin.ModelAdmin):
list_display = ('created_on',)
list_filter = ('created_on',)
- fieldsets = PHYSICAL_MEDIA_OBJECT_ADMIN_FIELDSETS
-
class PhysicalMediaObjectAdmin(MediaObjectAdmin):
list_display = ('path',) + MediaObjectAdmin.list_display
list_display_links = ('path',)
View
9 media_library/fieldsets.py
@@ -1,15 +1,8 @@
from django.utils.translation import ugettext_lazy as _
-MEDIA_OBJECT_ADMIN_FIELDSETS = (
- # (_(u'Metadata'), {
- # 'fields':('created_on', 'modified_on',),
- # 'classes':('collapse',),
- # }),
-)
-
PHYSICAL_MEDIA_OBJECT_ADMIN_FIELDSETS = (
(_(u'Media information'), {
'fields':('path',)
}),
-) + MEDIA_OBJECT_ADMIN_FIELDSETS
+)
View
26 media_library/forms/fields.py
@@ -1,37 +1,39 @@
-from django.forms.fields import ChoiceField
+from django.utils.translation import ugettext_lazy as _
+from django.core.exceptions import ValidationError
+from django.forms.fields import CharField
+
import os
-class PathField(ChoiceField):
+class PathField(CharField):
def __init__(self, path, match=None, recursive=False, required=True,
widget=None, label=None, initial=None, help_text=None,
*args, **kwargs):
self.path, self.match, self.recursive = path, match, recursive
- super(PathField, self).__init__(choices=(), required=required,
+ super(PathField, self).__init__(required=required,
widget=widget, label=label, initial=initial, help_text=help_text,
*args, **kwargs)
+ self.help_text = _(u"Enter a valid path rooted at '%s'" % (self.path))
- if self.required:
- self.choices = []
- else:
- self.choices = [("", "---------")]
+ def validate(self, value):
+ valid_paths = []
if self.match is not None:
self.match_re = re.compile(self.match)
- if recursive:
+ if self.recursive:
for root, dirs, files in sorted(os.walk(self.path)):
for d in dirs:
if self.match is None or self.match_re.search(d):
- d = os.path.join(root, d)
- self.choices.append((d, d.replace(path, "", 1)))
+ valid_paths.append( os.path.join(root, d) )
else:
try:
for d in sorted(os.listdir(self.path)):
full_path = os.path.join(self.path, d)
if os.path.isdir(full_path) and (self.match is None or self.match_re.search(d)):
- self.choices.append((full_path, d))
+ valid_paths.append( full_path )
except OSError:
pass
- self.widget.choices = self.choices
+ if value not in valid_paths:
+ raise ValidationError(u"Chosen path %s does not exist." % (value))
View
4 media_library/torrents/fieldsets.py
@@ -1,6 +1,6 @@
from django.utils.translation import ugettext_lazy as _
from media_library.fieldsets import (
- MEDIA_OBJECT_ADMIN_FIELDSETS, PHYSICAL_MEDIA_OBJECT_ADMIN_FIELDSETS)
+ PHYSICAL_MEDIA_OBJECT_ADMIN_FIELDSETS)
TRACKER_ADMIN_FIELDSETS = (
@@ -11,7 +11,7 @@
'fields':('user', 'passcode',),
'classes':('collapse',),
}),
-)# + MEDIA_OBJECT_ADMIN_FIELDSETS
+)
TORRENT_ADMIN_FIELDSETS = (
(_(u'Torrent information'), {
View
85 ...migrations/0002_auto__add_torrentregistry__add_unique_torrentregistry_content_type_obj.py
@@ -0,0 +1,85 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Adding model 'TorrentRegistry'
+ db.create_table('torrents_torrentregistry', (
+ ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('content_type', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['contenttypes.ContentType'])),
+ ('object_id', self.gf('django.db.models.fields.PositiveIntegerField')()),
+ ('torrent', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['torrents.Torrent'])),
+ ))
+ db.send_create_signal('torrents', ['TorrentRegistry'])
+
+ # Adding unique constraint on 'TorrentRegistry', fields ['content_type', 'object_id', 'torrent']
+ db.create_unique('torrents_torrentregistry', ['content_type_id', 'object_id', 'torrent_id'])
+
+
+ def backwards(self, orm):
+
+ # Removing unique constraint on 'TorrentRegistry', fields ['content_type', 'object_id', 'torrent']
+ db.delete_unique('torrents_torrentregistry', ['content_type_id', 'object_id', 'torrent_id'])
+
+ # Deleting model 'TorrentRegistry'
+ db.delete_table('torrents_torrentregistry')
+
+
+ models = {
+ 'contenttypes.contenttype': {
+ 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+ 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ },
+ 'taggit.tag': {
+ 'Meta': {'object_name': 'Tag'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '100', 'db_index': 'True'})
+ },
+ 'taggit.taggeditem': {
+ 'Meta': {'object_name': 'TaggedItem'},
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'taggit_taggeditem_tagged_items'", 'to': "orm['contenttypes.ContentType']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'object_id': ('django.db.models.fields.IntegerField', [], {'db_index': 'True'}),
+ 'tag': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'taggit_taggeditem_items'", 'to': "orm['taggit.Tag']"})
+ },
+ 'torrents.torrent': {
+ 'Meta': {'object_name': 'Torrent'},
+ 'created_on': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'modified_on': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'blank': 'True'}),
+ 'path': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
+ 'tracker': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['torrents.Tracker']"}),
+ 'type': ('django.db.models.fields.PositiveSmallIntegerField', [], {})
+ },
+ 'torrents.torrentregistry': {
+ 'Meta': {'unique_together': "(('content_type', 'object_id', 'torrent'),)", 'object_name': 'TorrentRegistry'},
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'object_id': ('django.db.models.fields.PositiveIntegerField', [], {}),
+ 'torrent': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['torrents.Torrent']"})
+ },
+ 'torrents.tracker': {
+ 'Meta': {'object_name': 'Tracker'},
+ 'created_on': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_private': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'modified_on': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'blank': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'passcode': ('django.db.models.fields.CharField', [], {'max_length': '64', 'blank': 'True'}),
+ 'slug': ('django.db.models.fields.SlugField', [], {'db_index': 'True', 'max_length': '50', 'blank': 'True'}),
+ 'torrent_directory': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'url': ('django.db.models.fields.URLField', [], {'max_length': '200'}),
+ 'user': ('django.db.models.fields.CharField', [], {'max_length': '64', 'blank': 'True'})
+ }
+ }
+
+ complete_apps = ['torrents']
View
15 media_library/torrents/models.py
@@ -1,4 +1,6 @@
+from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
+from django.contrib.contenttypes import generic
from django.conf import settings
from django.db import models
@@ -16,7 +18,7 @@ class Tracker(MediaObject):
is_private = models.BooleanField(_(u'is private'))
user = models.CharField(_(u'user'), max_length=64, blank=True)
passcode = models.CharField(_(u'passcode'), max_length=64, blank=True)
- torrent_directory = PathField(_(u'torrent directory'), path=settings.TORRENT_PATH, recursive=True)
+ torrent_directory = PathField(_(u'torrent directory'), path=settings.TORRENT_PATH)
class Meta:
app_label = 'torrents'
@@ -34,3 +36,14 @@ class Meta:
def __unicode__(self):
return u'Torrent %s on tracker %s' % (self.path, self.tracker.name)
+
+class TorrentRegistry(models.Model):
+ content_type = models.ForeignKey(ContentType)
+ object_id = models.PositiveIntegerField()
+ media_object = generic.GenericForeignKey()
+ torrent = models.ForeignKey('torrents.Torrent')
+
+ class Meta:
+ app_label = 'torrents'
+ verbose_name = _(u'torrent registry')
+ unique_together = ('content_type', 'object_id', 'torrent')

0 comments on commit e4ee217

Please sign in to comment.