Initial commit of the trash app
This commit is contained in:
7
apps/trash/__init__.py
Normal file
7
apps/trash/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from documents.models import Document
|
||||||
|
|
||||||
|
from .api import make_trashable
|
||||||
|
|
||||||
|
make_trashable(Document)
|
||||||
13
apps/trash/admin.py
Normal file
13
apps/trash/admin.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
from .models import TrashedItem
|
||||||
|
|
||||||
|
|
||||||
|
class TrashedItemAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('content_type', 'object_id', 'content_object',)
|
||||||
|
list_display_links = ('content_object',)
|
||||||
|
|
||||||
|
|
||||||
|
admin.site.register(TrashedItem, TrashedItemAdmin)
|
||||||
15
apps/trash/api.py
Normal file
15
apps/trash/api.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from .models import TrashableModelManager, new_delete_method
|
||||||
|
|
||||||
|
|
||||||
|
trashable_models = []
|
||||||
|
def make_trashable(model):
|
||||||
|
trashable_models.append(model)
|
||||||
|
#model.__class__.objects = TrashableModelManager()
|
||||||
|
#model.__class__._default_manager = TrashableModelManager()
|
||||||
|
#model.objects = TrashableModelManager()
|
||||||
|
model.add_to_class('objects', TrashableModelManager())
|
||||||
|
old_delete_method = model.delete
|
||||||
|
model.delete = new_delete_method(old_delete_method)
|
||||||
|
#model.add_to_class('is_in_trash', return True)
|
||||||
41
apps/trash/migrations/0001_initial.py
Normal file
41
apps/trash/migrations/0001_initial.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
# -*- coding: 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 'TrashedItem'
|
||||||
|
db.create_table('trash_trasheditem', (
|
||||||
|
('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')()),
|
||||||
|
))
|
||||||
|
db.send_create_signal('trash', ['TrashedItem'])
|
||||||
|
|
||||||
|
|
||||||
|
def backwards(self, orm):
|
||||||
|
# Deleting model 'TrashedItem'
|
||||||
|
db.delete_table('trash_trasheditem')
|
||||||
|
|
||||||
|
|
||||||
|
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'})
|
||||||
|
},
|
||||||
|
'trash.trasheditem': {
|
||||||
|
'Meta': {'object_name': 'TrashedItem'},
|
||||||
|
'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', [], {})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
complete_apps = ['trash']
|
||||||
0
apps/trash/migrations/__init__.py
Normal file
0
apps/trash/migrations/__init__.py
Normal file
52
apps/trash/models.py
Normal file
52
apps/trash/models.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
from django.contrib.contenttypes import generic
|
||||||
|
from django.db import models
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
|
class TrashedItemManager(models.Manager):
|
||||||
|
def is_in_trash(self, obj):
|
||||||
|
content_type = ContentType.objects.get_for_model(obj)
|
||||||
|
try:
|
||||||
|
self.model.objects.get(content_type=content_type, object_id=obj.id)
|
||||||
|
except self.model.DoesNotExist:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def ids(self):
|
||||||
|
return [trash_item.object_id for trash_item in self.model.objects.all()]
|
||||||
|
|
||||||
|
|
||||||
|
class TrashedItem(models.Model):
|
||||||
|
#trashed_at = models.DateTimeField(_('Trashed at'), editable=False, blank=True, null=True)
|
||||||
|
content_type = models.ForeignKey(ContentType)
|
||||||
|
object_id = models.PositiveIntegerField()
|
||||||
|
content_object = generic.GenericForeignKey('content_type', 'object_id')
|
||||||
|
|
||||||
|
objects = TrashedItemManager()
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return unicode(self.content_object)
|
||||||
|
|
||||||
|
def restore(self):
|
||||||
|
self.delete()
|
||||||
|
|
||||||
|
|
||||||
|
def new_delete_method(old_delete_method):
|
||||||
|
def delete(self, *args, **kwargs):
|
||||||
|
trash = kwargs.pop('trash', True)
|
||||||
|
|
||||||
|
if trash==False:
|
||||||
|
return old_delete_method(self, *args, **kwargs)
|
||||||
|
else:
|
||||||
|
trashed_item = TrashedItem.objects.create(content_object=self)#, trashed_at=datetime.now())
|
||||||
|
|
||||||
|
return delete
|
||||||
|
|
||||||
|
|
||||||
|
class TrashableModelManager(models.Manager):
|
||||||
|
def get_query_set(self):
|
||||||
|
print 'excluded', TrashedItem.objects.items()
|
||||||
|
query_set = super(TrashableModelManager, self).get_query_set().exclude(pk__in=TrashedItem.objects.ids())
|
||||||
|
return query_set
|
||||||
4
apps/trash/urls.py
Normal file
4
apps/trash/urls.py
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
from django.conf.urls.defaults import patterns, url
|
||||||
|
|
||||||
|
urlpatterns = patterns('trash.views',
|
||||||
|
)
|
||||||
1
apps/trash/views.py
Normal file
1
apps/trash/views.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# Create your views here.
|
||||||
@@ -190,6 +190,7 @@ INSTALLED_APPS = (
|
|||||||
'rest_api',
|
'rest_api',
|
||||||
'bootstrap',
|
'bootstrap',
|
||||||
'statistics',
|
'statistics',
|
||||||
|
'trash',
|
||||||
|
|
||||||
# Has to be last so the other apps can register it's signals
|
# Has to be last so the other apps can register it's signals
|
||||||
'signaler',
|
'signaler',
|
||||||
|
|||||||
1
urls.py
1
urls.py
@@ -42,6 +42,7 @@ urlpatterns = patterns('',
|
|||||||
(r'^maintenance/', include('maintenance.urls')),
|
(r'^maintenance/', include('maintenance.urls')),
|
||||||
(r'^statistics/', include('statistics.urls')),
|
(r'^statistics/', include('statistics.urls')),
|
||||||
(r'^clustering/', include('clustering.urls')),
|
(r'^clustering/', include('clustering.urls')),
|
||||||
|
(r'^trash/', include('trash.urls')),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user