Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 't10_signal'

  • Loading branch information...
commit 440e343be07e43da122208005f1a62784d530c5e 2 parents b4cbb5e + 095148d
@andreysobol authored
Showing with 42 additions and 1 deletion.
  1. +23 −0 testsobol/mainapp/models.py
  2. +19 −1 testsobol/mainapp/tests.py
View
23 testsobol/mainapp/models.py
@@ -1,4 +1,5 @@
from django.db import models
+from django.db.models.signals import post_save, post_delete
from utils import Render
@@ -24,3 +25,25 @@ class Request(models.Model):
date = models.DateTimeField()
url = models.CharField(max_length=250)
ip = models.CharField(max_length=250)
+
+class Log(models.Model):
+ date = models.DateTimeField(auto_now_add=True)
+ model = models.CharField(max_length=250)
+ signal = models.CharField(max_length=250)
+
+def save(sender, **kwargs):
+ if sender != Log:
+ if kwargs['created']:
+ signal='create'
+ else:
+ signal='update'
+
+ log = Log(model = sender.__name__, signal = signal)
+ log.save()
+
+def delete(sender, **kwargs):
+ log = Log(model = sender.__name__, signal = 'delete')
+ log.save()
+
+post_save.connect(save)
+post_delete.connect(delete)
View
20 testsobol/mainapp/tests.py
@@ -11,7 +11,7 @@
from django.test import TestCase
from django.forms.models import model_to_dict
-from models import Bio, Request
+from models import Bio, Request, Log
class IndexViewTest(TestCase):
@@ -127,3 +127,21 @@ def test(self):
self.assertTrue((f.read()).find('error') != -1)
f.close()
Popen("rm " + d, stdout=PIPE, stderr=PIPE, shell=True)
+
+
+class Signal(TestCase):
+
+ def test(self):
+ b = Bio(bio="Noooooooooooooo", surname="Sobol", name="Andrey", other="pigeon post - white pigeon only", birth="1990-09-18", skype="andreysobol", jabber="pisecs@gmail.com", email="asobol@mail.ua", img="img/1.jpg")
+ b.save()
+ self.assertEqual(Log.objects.order_by('-id')[0].model,'Bio')
+ self.assertEqual(Log.objects.order_by('-id')[0].signal,'create')
+
+ b.bio="sfsf"
+ b.save()
+ self.assertEqual(Log.objects.order_by('-id')[0].model,'Bio')
+ self.assertEqual(Log.objects.order_by('-id')[0].signal,'update')
+
+ b.delete()
+ self.assertEqual(Log.objects.order_by('-id')[0].model,'Bio')
+ self.assertEqual(Log.objects.order_by('-id')[0].signal,'delete')
Please sign in to comment.
Something went wrong with that request. Please try again.