-
Notifications
You must be signed in to change notification settings - Fork 2
/
makemodels.py
79 lines (58 loc) · 2.36 KB
/
makemodels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Not currently used in this app, but scrapes the schema of tables from FEC's web site and generates django models."""
import re
import urllib2
from BeautifulSoup import BeautifulSoup
re_char = re.compile('VARCHAR2\s*\((\d+)(?: Byte)?\)')
def getdatatype(fec):
if re_char.match(fec):
return 'Char', re_char.match(fec).groups()[0]
if re.match('NUMBER\s*\(\d+\)',fec):
return 'Integer',None
if re.match('NUMBER\s*\(\d+,\d\)',fec):
return 'Float',None
if re.match('DATE',fec):
return 'Date',None
return 'unknown',None
def unsoupify(cell):
if not cell.contents or not cell.contents[0]:
return ''
return unicode(cell.contents[0]).strip().replace('"','')
tables = {
'cm': 'CommitteeMaster',
'cn': 'CandidateMaster',
'ccl': 'CandCmteLinkage',
'indiv': 'ContributionsbyIndividuals',
'pas': 'ContributionstoCandidates',
'oth': 'CommitteetoCommittee',
'weball': 'WEBALL',
'weballk': 'WEBK',
}
def writemodels():
fout = open('models.py','w')
fout.write("from django.db import models\n\n\n")
for key in tables.keys():
html = urllib2.urlopen('http://www.fec.gov/finance/disclosure/metadata/DataDictionary%s.shtml' % tables[key]).read()
soup = BeautifulSoup(html).table.findAll('tr')[1:]
fout.write("""
class %s(models.Model):
class Meta:
verbose_name="%s"
cycle = models.IntegerField()
""" % (key, tables[key]))
for row in soup:
cells = row.findAll('td')
(name,longname,start,isnull,datatype,descrip) = [unsoupify(x) for x in cells]
(djangotype,length) = getdatatype(datatype)
if djangotype=='Char':
fout.write("""
%s = models.CharField(max_length=%s, blank=True, verbose_name="%s", help_text = "%s")""" % (name,length,longname,descrip))
elif djangotype=='Date':
fout.write("""
%s = models.DateField(blank=True, verbose_name="%s", help_text = "%s")""" % (name,longname,descrip))
elif djangotype=='Integer':
fout.write("""
%s = models.IntegerField(verbose_name="%s", help_text = "%s")""" % (name,longname,descrip))
elif djangotype=='Float':
fout.write("""
%s = models.FloatField(verbose_name="%s", help_text = "%s")""" % (name,longname,descrip))
writemodels()