Skip to content

Instantly share code, notes, and snippets.

@tfausak
Created March 17, 2011 22:35
Show Gist options
  • Save tfausak/875269 to your computer and use it in GitHub Desktop.
Save tfausak/875269 to your computer and use it in GitHub Desktop.
Wrapper around mongoengine's Document class to avoid deleting things.
from datetime import datetime
from mongoengine import *
from mongoengine.queryset import queryset_manager
from re import sub
class FamigoDocument(Document):
_active = BooleanField(default=True, required=True)
@queryset_manager
def active(cls, queryset):
return queryset.filter(_active=True)
@queryset_manager
def inactive(cls, queryset):
return queryset.filter(_active=False)
def activate(self, *args, **kwargs):
if not self._active:
for field, value in self._fields.items():
if not value.unique or not isinstance(value, StringField):
continue
self[field] = sub(r' \(Deactivated [0-9]+\)', '',
self[field])
self._active = True
self.save()
def deactivate(self, *args, **kwargs):
if self._active:
for field, value in self._fields.items():
if not value.unique or not isinstance(value, StringField):
continue
self[field] = '{0} (Deactivated {1})'.format(self[field],
datetime.now().strftime('%Y%m%d%H%M%S%f'))
self._active = False
self.save()
class Foo(FamigoDocument):
meta = {'collection': 'foo'}
string = StringField(unique=True)
connect('tmp4')
Foo.drop_collection()
foo = Foo(string='asdf')
foo.save()
print '>', foo.string, '<'
foo.deactivate()
print '>', foo.string, '<'
foo.activate()
print '>', foo.string, '<'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment