[damned-lies] Replace BranchCharField by model clean method



commit 99c329b549445ae062660e05760ad4b96a4ed0ee
Author: Claude Paroz <claude 2xlibre net>
Date:   Tue May 18 21:45:33 2010 +0200

    Replace BranchCharField by model clean method
    
    From this commit, Django 1.2 is required (model validation)

 README                                      |    2 +-
 docs/django_patches.txt                     |    4 ++--
 stats/migrations/0001_initial.py            |    2 +-
 stats/migrations/0002_add_release_weight.py |    2 +-
 stats/migrations/0003_add_branch_weight.py  |    2 +-
 stats/models.py                             |   21 ++++++++++-----------
 stats/tests/__init__.py                     |    4 ++--
 7 files changed, 18 insertions(+), 19 deletions(-)
---
diff --git a/README b/README
index 3150aa6..8b42cbe 100644
--- a/README
+++ b/README
@@ -13,7 +13,7 @@ The Data model is in the /docs directory.
 Requirements
 ============
 
-1 - Django 1.1.X
+1 - Django 1.2.X
 
 2 - Python 2.5 (for hashlib module).
 
diff --git a/docs/django_patches.txt b/docs/django_patches.txt
index 0e7ab5e..01ce197 100644
--- a/docs/django_patches.txt
+++ b/docs/django_patches.txt
@@ -1,12 +1,12 @@
 Django patches for damned-lies
 ==============================
 
-Django version: 1.1
+Django version: 1.2
 
 *   Add support for translator comments
     File: django/core/management/commands/makemessages.py
     Add -c flag to xgettext calls, to extract translator comments in po files,
-    at lines 126 and 151
+    at lines 193 and 228
     See http://code.djangoproject.com/ticket/10004
 
 *   Add support for nds (Low German) language
diff --git a/stats/migrations/0001_initial.py b/stats/migrations/0001_initial.py
index d57c816..3ad7e7d 100644
--- a/stats/migrations/0001_initial.py
+++ b/stats/migrations/0001_initial.py
@@ -230,7 +230,7 @@ class Migration:
             'Meta': {'unique_together': "(('name', 'module'),)", 'db_table': "'branch'"},
             'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
             'module': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['stats.Module']"}),
-            'name': ('BranchCharField', [], {'max_length': '50'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
             'vcs_subpath': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True', 'blank': 'True'})
         },
         'stats.category': {
diff --git a/stats/migrations/0002_add_release_weight.py b/stats/migrations/0002_add_release_weight.py
index 1a6cfa9..0bf3c09 100644
--- a/stats/migrations/0002_add_release_weight.py
+++ b/stats/migrations/0002_add_release_weight.py
@@ -77,7 +77,7 @@ class Migration:
             'Meta': {'unique_together': "(('name', 'module'),)", 'db_table': "'branch'"},
             'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
             'module': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['stats.Module']"}),
-            'name': ('BranchCharField', [], {'max_length': '50'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
             'vcs_subpath': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True', 'blank': 'True'})
         },
         'stats.category': {
diff --git a/stats/migrations/0003_add_branch_weight.py b/stats/migrations/0003_add_branch_weight.py
index 606ba05..785014d 100644
--- a/stats/migrations/0003_add_branch_weight.py
+++ b/stats/migrations/0003_add_branch_weight.py
@@ -77,7 +77,7 @@ class Migration:
             'Meta': {'unique_together': "(('name', 'module'),)", 'db_table': "'branch'"},
             'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
             'module': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['stats.Module']"}),
-            'name': ('BranchCharField', [], {'max_length': '50'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
             'vcs_subpath': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True', 'blank': 'True'}),
             'weight': ('django.db.models.fields.IntegerField', [], {'default': '0'})
         },
diff --git a/stats/models.py b/stats/models.py
index ff7a18f..4850c78 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -24,6 +24,7 @@ import threading
 from datetime import datetime
 
 from django.conf import settings
+from django.core.exceptions import ValidationError
 from django.utils.translation import ungettext, ugettext as _, ugettext_noop
 from django.utils import dateformat
 from django.utils.datastructures import SortedDict
@@ -127,19 +128,9 @@ class Module(models.Model):
             return True
         return False
 
-class BranchCharField(models.CharField):
-    def pre_save(self, model_instance, add):
-        """ Check if branch is valid before saving the instance """
-        if model_instance.checkout_on_creation:
-            try:
-                model_instance.checkout()
-            except:
-                raise ValueError("Branch not valid: error while checking out the branch (%s)." % sys.exc_info()[1])
-        return getattr(model_instance, self.attname)
-
 class Branch(models.Model):
     """ Branch of a module """
-    name = BranchCharField(max_length=50)
+    name        = models.CharField(max_length=50)
     #description = models.TextField(null=True)
     vcs_subpath = models.CharField(max_length=50, null=True, blank=True)
     module      = models.ForeignKey(Module)
@@ -164,6 +155,13 @@ class Branch(models.Model):
     def __unicode__(self):
         return "%s (%s)" % (self.name, self.module)
 
+    def clean(self):
+        if self.checkout_on_creation:
+            try:
+                self.checkout()
+            except:
+                raise ValidationError("Branch not valid: error while checking out the branch (%s)." % sys.exc_info()[1])
+
     def save(self, force_insert=False, force_update=False, update_statistics=True):
         super(Branch, self).save(force_insert, force_update)
         if update_statistics:
@@ -471,6 +469,7 @@ class Branch(models.Model):
                     "localdir" : modulepath,
                     })
             elif vcs_type == "git":
+                # tester "cd \"%(localdir)s\" && git checkout %(branch)s && git clean -dfq && git pull origin/%(branch)s"
                 commandList.append("cd \"%(localdir)s\" && git checkout %(branch)s && git fetch && git reset --hard origin/%(branch)s && git clean -dfq" % {
                     "localdir" : modulepath,
                     "branch" : self.name,
diff --git a/stats/tests/__init__.py b/stats/tests/__init__.py
index 752c08f..0540dce 100644
--- a/stats/tests/__init__.py
+++ b/stats/tests/__init__.py
@@ -23,6 +23,7 @@ from datetime import date
 from django.test import TestCase
 from django.test.client import Client
 from django.core import mail
+from django.core.exceptions import ValidationError
 from django.conf import settings
 from stats.models import Module, Domain, Branch, Category, Release, Statistics, Information
 from languages.models import Language
@@ -150,8 +151,7 @@ class ModuleTestCase(TestCase):
         Branch.checkout_on_creation = True
         branch = Branch(name="trunk2",
                         module = self.mod)
-        self.assertRaises(ValueError, branch.save)
-        Branch.checkout_on_creation = False
+        self.assertRaises(ValidationError, branch.clean)
 
     def testDynamicPO(self):
         """ Test the creation of a blank po file for a new language """



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]