mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
First code commit.
Some of it works, some doesn't. Some of the changes are not included (soon). git-svn-id: http://code.djangoproject.com/svn/django/branches/full-history@3351 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
247b4ba5ac
commit
1ce3fe5a96
0
django/contrib/history/__init__.py
Normal file
0
django/contrib/history/__init__.py
Normal file
BIN
django/contrib/history/__init__.pyc
Normal file
BIN
django/contrib/history/__init__.pyc
Normal file
Binary file not shown.
39
django/contrib/history/api.py
Normal file
39
django/contrib/history/api.py
Normal file
@ -0,0 +1,39 @@
|
||||
from django.db import models
|
||||
from django.db.models import signals
|
||||
from django.dispatch import dispatcher
|
||||
#from django.shortcuts import get_object_or_404
|
||||
from django.contrib.history.models import ChangeLog
|
||||
import cPickle as Pickle
|
||||
from tut1.polls.models import Poll #Temp import of used models
|
||||
|
||||
def get_object(change):
|
||||
""" Returns unpickled object. """
|
||||
return Pickle.loads(change.object)
|
||||
|
||||
def get_revision_number(change):
|
||||
""" Returns the ID/revision number of ChangeLog entry. """
|
||||
return change.id
|
||||
|
||||
def get_revision():
|
||||
pass
|
||||
|
||||
def list_history(type, parent_id):
|
||||
return type.objects.all().filter(pk=parent_id)
|
||||
|
||||
def version(num=-1):
|
||||
pass
|
||||
|
||||
def save_new_revision(sender, instance, signal, *args, **kwargs):
|
||||
""" Saves a current copy of the record into the History table."""
|
||||
log = ChangeLog()
|
||||
log.parent_id = instance.id
|
||||
#log.user_id = .user_id
|
||||
log.object = Pickle.dumps(instance, protocol=0)
|
||||
log.save()
|
||||
print "New change saved."
|
||||
|
||||
def _get_original_object(type, id):
|
||||
pass
|
||||
|
||||
|
||||
dispatcher.connect( save_new_revision, signal=signals.post_save, sender=Poll )
|
BIN
django/contrib/history/api.pyc
Normal file
BIN
django/contrib/history/api.pyc
Normal file
Binary file not shown.
84
django/contrib/history/models.py
Normal file
84
django/contrib/history/models.py
Normal file
@ -0,0 +1,84 @@
|
||||
from django.db import models
|
||||
from django.db.models import signals
|
||||
from django.dispatch import dispatcher
|
||||
from django.contrib.auth.models import User
|
||||
from tut1.polls.models import Poll #Temp import of used models
|
||||
# Misc stuff
|
||||
import cPickle as Pickle
|
||||
from datetime import datetime
|
||||
#from django.contrib.history.api import get_object, save_new_revision
|
||||
|
||||
class ChangeLog(models.Model):
|
||||
change_time = models.DateTimeField(_('time of change'), auto_now=True)
|
||||
parent = models.ForeignKey(Poll)
|
||||
user = models.ForeignKey(User, default="1")
|
||||
object = models.TextField()
|
||||
#object_type = models.CharField(maxlength=50)
|
||||
#pub_date = models.DateTimeField('date published')
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('changelog entry')
|
||||
verbose_name_plural = _('changelog entries')
|
||||
db_table = _('history_changelog')
|
||||
|
||||
class Admin:
|
||||
fields = (
|
||||
('Meta info', {'fields': ('change_time', 'parent', 'user',)}),
|
||||
('Object', {'fields': ('object',),}),
|
||||
)
|
||||
|
||||
list_display = ('parent', 'user', 'change_time')
|
||||
|
||||
def get_object(self):
|
||||
""" Returns unpickled object. """
|
||||
return Pickle.loads(self.object)
|
||||
|
||||
def get_revision_number(self):
|
||||
""" Returns the ID/revision number of ChangeLog entry. """
|
||||
return self.id
|
||||
|
||||
################
|
||||
# Other (API) methods
|
||||
################
|
||||
|
||||
def get_version(object, offset=1):
|
||||
""" Returns 'current-offset' revision of the 'object' """
|
||||
list = ChangeLog.objects.order_by('-id').filter(parent=object.id)[offset]
|
||||
print list.get_object()
|
||||
return list
|
||||
|
||||
def list_history(parent_id, *args):
|
||||
""" Returns a list of all revisions for that id. """
|
||||
if args:
|
||||
return ChangeLog.objects.filter(parent=parent_id)[:args[0]]
|
||||
#print "extra"
|
||||
#return ChangeLog.objects.filter(parent=parent_id)
|
||||
else:
|
||||
return ChangeLog.objects.filter(parent=parent_id)
|
||||
|
||||
def version(object, num=5):
|
||||
""" Returns last 'num' revisions of the 'object'. """
|
||||
return ChangeLog.objects.order_by('-id').filter(parent=object.id)[:num]
|
||||
|
||||
def version_by_date(object, date):
|
||||
""" Returns a list of revisions made at 'date'. """
|
||||
return ChangeLog.objects.filter(parent=object.id).filter(change_time__exact=date)
|
||||
|
||||
|
||||
def save_new_revision(sender, instance, signal, *args, **kwargs):
|
||||
""" Saves a old copy of the record into the History table."""
|
||||
#modelname = instance.__class__.__name__
|
||||
#print modelname
|
||||
old = Poll.objects.filter(pk=instance.id)
|
||||
log = ChangeLog()
|
||||
log.parent_id = instance.id
|
||||
#log.user_id = .user_id
|
||||
log.object = Pickle.dumps(old[0], protocol=0)
|
||||
log.save()
|
||||
print "New change saved."
|
||||
|
||||
def _get_first_revision(object):
|
||||
pass
|
||||
|
||||
|
||||
dispatcher.connect( save_new_revision, signal=signals.pre_save, sender=Poll )
|
BIN
django/contrib/history/models.pyc
Normal file
BIN
django/contrib/history/models.pyc
Normal file
Binary file not shown.
3
django/contrib/history/templates/base.html
Normal file
3
django/contrib/history/templates/base.html
Normal file
@ -0,0 +1,3 @@
|
||||
<ul>
|
||||
<li><a href="/polls/">{{ test }}</a></li>
|
||||
</ul>
|
@ -0,0 +1,10 @@
|
||||
{% for object in object_list %}
|
||||
<h2>{{ object.question }}</h2>
|
||||
<ul>
|
||||
<li>ID: {{ object.id }}</li>
|
||||
<li>PID: {{ object.parent_id }}</li>
|
||||
<li>Change time: {{ object.change_time }}</li>
|
||||
<li>Question: {{ object.object }}</li>
|
||||
<li>Pub date: {{ object.pub_date }}</li>
|
||||
</ul>
|
||||
{% endfor %}
|
15
django/contrib/history/templates/history/detail.html
Normal file
15
django/contrib/history/templates/history/detail.html
Normal file
@ -0,0 +1,15 @@
|
||||
{% if change %}
|
||||
<ul>
|
||||
<li>ID: {{ change.id }}</li>
|
||||
<li>PID: {{ change.parent_id }}</li>
|
||||
<li>UID: {{ change.user_id }}</li>
|
||||
<li>Change time: {{ change.change_time }}</li>
|
||||
|
||||
<li>ID: {{ object.id }}
|
||||
<li>Question: {{ object.question }}</li>
|
||||
<li>Pub date: {{ object.pub_date }}</li>
|
||||
|
||||
</ul>
|
||||
{% else %}
|
||||
<p>No changes are available.</p>
|
||||
{% endif %}
|
19
django/contrib/history/templates/history/list.html
Normal file
19
django/contrib/history/templates/history/list.html
Normal file
@ -0,0 +1,19 @@
|
||||
{% if changes_list %}
|
||||
<table border="1">
|
||||
<tr>
|
||||
<td>ID:</td>
|
||||
<td>PID:</td>
|
||||
<td>Change time:</td>
|
||||
|
||||
</tr>
|
||||
{% for change in changes_list %}
|
||||
<tr>
|
||||
<td><a href="detail/{{ change.id }}/">{{ change.id }}</td>
|
||||
<td>{{ change.parent_id }}</td>
|
||||
<td>{{ change.change_time }}</td>
|
||||
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<p>No changes are available.</p>
|
||||
{% endif %}
|
13
django/contrib/history/urls.py
Normal file
13
django/contrib/history/urls.py
Normal file
@ -0,0 +1,13 @@
|
||||
from django.conf.urls.defaults import *
|
||||
from django.contrib.history.models import ChangeLog
|
||||
|
||||
info_dict = {
|
||||
'queryset': ChangeLog.objects.all(),
|
||||
}
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r'^$', 'django.contrib.history.views.main.index'),
|
||||
(r'^list/$', 'django.contrib.history.views.main.list'),
|
||||
(r'^detail/(?P<change_id>\d+)/$', 'django.contrib.history.views.main.detail'),
|
||||
(r'^changes/(?P<parent_id>\d+)/$', 'django.contrib.history.views.main.changes'),
|
||||
)
|
BIN
django/contrib/history/urls.pyc
Normal file
BIN
django/contrib/history/urls.pyc
Normal file
Binary file not shown.
0
django/contrib/history/views/__init__.py
Normal file
0
django/contrib/history/views/__init__.py
Normal file
BIN
django/contrib/history/views/__init__.pyc
Normal file
BIN
django/contrib/history/views/__init__.pyc
Normal file
Binary file not shown.
62
django/contrib/history/views/main.py
Normal file
62
django/contrib/history/views/main.py
Normal file
@ -0,0 +1,62 @@
|
||||
from django.db import models
|
||||
from django.db.models import get_models
|
||||
from django.utils.text import capfirst
|
||||
from django.contrib.history.models import ChangeLog, get_version, list_history, version_by_date
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import get_object_or_404, render_to_response
|
||||
from datetime import datetime
|
||||
#import cPickle as Pickle
|
||||
#from django.contrib.history.api import get_object, list_history
|
||||
|
||||
def index(request):
|
||||
changes_list = ChangeLog.objects.all()
|
||||
return render_to_response('history/list.html',
|
||||
{'changes_list': changes_list})
|
||||
|
||||
def list(request):
|
||||
app_list = []
|
||||
|
||||
for app in models.get_apps():
|
||||
app_models = get_models(app)
|
||||
app_label = app_models[0]._meta.app_label
|
||||
|
||||
model_list = []
|
||||
|
||||
for m in app_models:
|
||||
model_list.append({
|
||||
'name': capfirst(m._meta.verbose_name_plural),
|
||||
})
|
||||
|
||||
if model_list:
|
||||
model_list.sort()
|
||||
app_list.append({
|
||||
'name': app_label.title(),
|
||||
'models': model_list,
|
||||
})
|
||||
|
||||
for app in app_list:
|
||||
print app['name']
|
||||
print app['models']
|
||||
|
||||
changes_list = ChangeLog.objects.all()
|
||||
return render_to_response('history/list.html',
|
||||
{'changes_list': changes_list})
|
||||
|
||||
|
||||
def detail(request, change_id):
|
||||
change = get_object_or_404(ChangeLog, pk=change_id)
|
||||
object = change.get_object()
|
||||
|
||||
bla = version_by_date(object, datetime(2006, 6, 7))
|
||||
for b in bla:
|
||||
print b.change_time
|
||||
|
||||
bla2 = get_version(object)
|
||||
|
||||
return render_to_response('history/detail.html', {'change': change,
|
||||
'object': object})
|
||||
|
||||
def changes(request, parent_id):
|
||||
changes_list = list_history(parent_id, 2)
|
||||
return render_to_response('history/list.html',
|
||||
{'changes_list': changes_list})
|
BIN
django/contrib/history/views/main.pyc
Normal file
BIN
django/contrib/history/views/main.pyc
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user