1. hideki nara
  2. djpostal

Commits

hideki nara  committed d2df8c2

Initial codes

  • Participants
  • Parent commits da97e1b
  • Branches master

Comments (0)

Files changed (6)

File src/djpostal/admin.py

View file
+# -*- coding: utf-8 -*- 
+from django.contrib import admin
+from models import *
+
+class JpAddressAdmin(admin.ModelAdmin):
+    list_display=tuple([f.name for f in JpAddress._meta.fields])
+
+admin.site.register(JpAddress,JpAddressAdmin )
+

File src/djpostal/models.py

View file
+# -*- coding: utf-8 -*-
+
+from django.db import models
+
+class JpAddress(models.Model):
+    code = models.CharField(max_length=10,db_index=True,unique=False)
+    zip5 = models.CharField(max_length=5 )
+    zip  = models.CharField(max_length=7,db_index=True)
+    prefecture_kana = models.CharField(max_length =50,db_index=True )
+    city_kana = models.CharField(max_length =50 ,db_index=True)
+    town_kana = models.CharField(max_length =80 ,db_index=True)
+    prefecture= models.CharField(max_length =50 ,db_index=True)
+    city = models.CharField(max_length =50 ,db_index=True)
+    town = models.CharField(max_length =50 ,db_index=True)
+    is_split = models.BooleanField() 
+    is_small = models.BooleanField() 
+    is_towncode = models.BooleanField() 
+    is_multi  = models.BooleanField() 
+    changed  = models.IntegerField()  # 0 = no changed , 1 = changed , 2 = discontinued
+    reason   = models.IntegerField() 
+
+    @classmethod
+    def is_changed(cls,val):
+        if type(val) == dict :
+            if False == val.has_key('changed'):
+                return False
+            else:
+                val = val['changed']    
+        try:
+            return int(val) == 1
+        except:
+            return False

File src/djpostal/tests.py

View file
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        print "test1"
+        self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+>>> 1 + 1 == 2
+False
+"""}

File src/djpostal/urls.py

View file
+# -*- coding: utf-8 -*-

File src/djpostal/utils.py

View file
+''' 
+'''
+import csv
+
+from django.conf import settings
+from django.utils.encoding import force_unicode
+import pykf
+
+__all__ = ('CsvUnicodeWriter', 'CsvUnicodeReader')
+
+class CsvUnicodeWriter(object):
+    def __init__(self, stream, dialect=None, encoding=None, errors="strict", **kwds):
+        self.writer = csv.writer(stream, dialect=dialect or csv.excel, **kwds)
+        self.encoding = encoding or settings.DEFAULT_CHARSET
+        self.errors = errors
+
+    def writerow(self, row):
+        self.writer.writerow(
+            map(lambda s: force_unicode(s, errors=self.errors).encode(self.encoding, self.errors), 
+            row))
+
+    def writerows(self, rows):
+        for row in rows:
+            self.writerow(row)
+
+
+def detect_encoding(stream):
+    ret= {   
+        pykf.UTF8   : 'utf-8',
+        pykf.SJIS   : 'shift-jis',
+#        pykf.ASCII  : settings.DEFAULT_CHARSET, #: dirty fix : shift-jis
+    }.get( pykf.guess( stream.read() ),settings.DEFAULT_CHARSET)
+    stream.seek(0)
+    return ret
+
+class CsvUnicodeReader(object):
+    def __init__(self, stream, dialect=None, encoding=None, errors="strict", **kwds):
+        self.reader = csv.reader(stream, dialect=dialect or csv.excel, **kwds)
+        self.encoding = encoding  if encoding else detect_encoding(stream)
+        self.line_num = 0 # Needed for DictReader
+        self.errors = errors
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        return map(lambda s: 
+            force_unicode(s, encoding=self.encoding, errors=self.errors), 
+            self.reader.next())
+
+    @classmethod
+    def csv_enumerator(cls,stream, fieldnames=None,dialect=None, 
+                            encoding=None, errors="strict", **kwds):
+        ''' enumerator factory for CsvUnicodeReader
+
+            - Key must be encode in unicode.
+            - data_dict values must be "strip()"ed 
+              because there could be heading and trailing spaces around.
+    
+        sample::
+
+            for index,data_dict in CsvUnicodeReader.csv_enumerator(csv_straeam,encoding='utf8'):
+                inits = dict( [ ( k.encode('ascii') , v ) for k,v in data_dict.iteritems() ]
+                Account(**inits).save()
+        '''
+        csvreader = csv.DictReader(stream,fieldnames) 
+        csvreader.reader = cls(stream, dialect=dialect, 
+                            encoding=encoding, 
+                                errors=errors , **kwds)
+        return enumerate(csvreader)

File src/djpostal/views.py

View file
+# -*- coding: utf-8 -*-
+