Merge lp:~seif/globaleaks/demo into lp:globaleaks/demo

Proposed by Seif Lotfy
Status: Merged
Approved by: Arturo
Approved revision: 52
Merged at revision: 50
Proposed branch: lp:~seif/globaleaks/demo
Merge into: lp:globaleaks/demo
Diff against target: 496 lines (+301/-153)
5 files modified
.bzrignore (+1/-0)
applications/globaleaks/models/db.py (+2/-153)
applications/globaleaks/modules/logic/datamodel.py (+154/-0)
applications/globaleaks/modules/logic/db.py (+67/-0)
applications/globaleaks/modules/logic/globaleaks.py (+77/-0)
To merge this branch: bzr merge lp:~seif/globaleaks/demo
Reviewer Review Type Date Requested Status
Arturo Approve
Review via email: mp+73818@code.launchpad.net

This proposal supersedes a proposal from 2011-09-02.

Description of the change

Clean up code
moved all globaleaks logic into modules

To post a comment you must log in.
Revision history for this message
Arturo (hellais) wrote :

Good cleanup and proper reorganization of GlobaLeaks models and classes. Though still a lot of methods need to be properly implemented

Revision history for this message
Arturo (hellais) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file '.bzrignore'
--- .bzrignore 2011-09-01 15:27:04 +0000
+++ .bzrignore 2011-09-02 13:56:08 +0000
@@ -34,3 +34,4 @@
34material/*34material/*
35applications/globaleaks/cache/cache.lock35applications/globaleaks/cache/cache.lock
36applications/globaleaks/cache/cache.shelve36applications/globaleaks/cache/cache.shelve
37applications/admin/cache/*
3738
=== removed file 'applications/admin/cache/cache.lock'
=== removed file 'applications/admin/cache/cache.shelve'
38Binary files applications/admin/cache/cache.shelve 2011-08-28 08:06:54 +0000 and applications/admin/cache/cache.shelve 1970-01-01 00:00:00 +0000 differ39Binary files applications/admin/cache/cache.shelve 2011-08-28 08:06:54 +0000 and applications/admin/cache/cache.shelve 1970-01-01 00:00:00 +0000 differ
=== modified file 'applications/globaleaks/models/db.py'
--- applications/globaleaks/models/db.py 2011-09-01 15:27:04 +0000
+++ applications/globaleaks/models/db.py 2011-09-02 13:56:08 +0000
@@ -20,158 +20,8 @@
20 db = DAL('sqlite://storage.sqlite') # if not, use SQLite or other DB20 db = DAL('sqlite://storage.sqlite') # if not, use SQLite or other DB
21"""21"""
22randomizer = local_import('randomizer')22randomizer = local_import('randomizer')
2323db = local_import('logic.db').DB()
24class DB(DAL):24gl = local_import('logic.globaleaks').Globaleaks(db)
25 def __init__(self):
26 DAL.__init__(self, 'sqlite://storage.db')
27 self.create_db()
28
29 def create_db(self):
30 self.define_table('target',
31 Field('name'),
32 Field('category'),
33 Field('desc'),
34 Field('url'),
35 Field('type'),
36 Field('info'),
37 Field('status'),
38 Field('last_sent_tulip'),
39 Field('last_access'),
40 Field('last_download'),
41 Field('tulip_counter'),
42 Field('dowload_counter'),
43 format='%(name)s'
44 )
45
46 self.define_table('leak',
47 Field('title'),
48 Field('desc'),
49 Field('submission_timestamp'),
50 Field('leaker_id', self.target),
51 Field('spooled', 'boolean', False),
52 format='%(name)s'
53 )
54
55 self.define_table('comment',
56 Field('leak_id', self.leak),
57 Field('commenter_id', self.target),
58 Field('comment'),
59 format='%(name)s'
60 )
61
62 self.define_table('material',
63 Field('url', unique=True),
64 Field('leak_id', self.leak),
65 Field('type'),
66 format='%(name)s'
67 )
68
69 self.define_table('tulip',
70 Field('url', unique=True),
71 Field('leak_id', self.leak),
72 Field('target_id'),
73 Field('allowed_accesses'),
74 Field('accesses_counter'),
75 Field('allowed_downloads'),
76 Field('downloads_counter'),
77 Field('expiry_time'),
78 format='%(name)s'
79 )
80
81 self.define_table('mail',
82 Field('target'),
83 Field('address'),
84 Field('tulip', unique=True),
85 format='%(name)s'
86 )
87
88db = DB()
89
90
91#XXX remove for non demo functionality
92#
93
94
95####
96# The main GlobaLeaks Class
97###
98
99class Globaleaks(object):
100
101 def __init__(self):
102 pass
103
104 def create_target(self, name, category, desc, url, type, info):
105 target_id = db.target.insert(name=name,
106 category=category,
107 desc = desc, url=url, type=type, info=info,
108 status="subscribed" #, last_send_tulip=None,
109 #last_access=None, last_download=None,
110 #tulip_counter=None, download_counter=None
111 )
112 db.commit()
113 return target_id
114
115 def delete_target (self, id):
116 db(db.target.id==id).delete()
117 pass
118
119 def get_targets(self, target_set):
120 if target_set == "ANY":
121 return db(db.target).select()
122 return db(db.target.category==target_set).select()
123
124 def get_target(self, target_id):
125 return db(db.target.id==target_id).select().first()
126
127 def create_leak(self, title, desc, leaker, material, target_set, tags="", number=None):
128 #FIXME insert new tags into DB first
129
130 #Create leak and insert into DB
131 leak_id = db.leak.insert(title = title, desc = desc,
132 submission_timestamp = time.time(),
133 leaker_id = 0, spooled=False)
134
135 targets = gl.get_targets(target_set)
136
137 for t in targets:
138 #Create a tulip for each target and insert into DB
139 #for target_url, allowed_downloads in targets.iteritems():
140 db.tulip.insert(url = randomizer.generate_tulip_url(),
141 leak_id = leak_id,
142 target_id = t.id, #FIXME get target_id_properly
143 allowed_accesses = 0, # inf
144 accesses_counter = 0,
145 allowed_downloads = 5,
146 downloads_counter = 0,
147 expiry_time = 0)
148
149 db.tulip.insert(url = number,
150 leak_id = leak_id,
151 target_id = 0, #FIXME get target_id_properly
152 allowed_accesses = 0, # inf
153 accesses_counter = 0,
154 allowed_downloads = 5,
155 downloads_counter = 0,
156 expiry_time = 0)
157
158 db.commit()
159 return leak_id
160
161 def get_leaks(self):
162 pass
163
164 def get_leak(self, leak_id):
165 pass
166
167 def get_tulips(self, leak_id):
168 pass
169
170 def get_tulip(self, tulip_id):
171 pass
172
173
174
17525
176####26####
177# For the time being just use sqlite27# For the time being just use sqlite
@@ -267,4 +117,3 @@
267 def formatter(self, value):117 def formatter(self, value):
268 return format(value)118 return format(value)
269119
270gl = Globaleaks()
271120
=== added directory 'applications/globaleaks/modules/logic'
=== added file 'applications/globaleaks/modules/logic/__init__.py'
=== added file 'applications/globaleaks/modules/logic/datamodel.py'
--- applications/globaleaks/modules/logic/datamodel.py 1970-01-01 00:00:00 +0000
+++ applications/globaleaks/modules/logic/datamodel.py 2011-09-02 13:56:08 +0000
@@ -0,0 +1,154 @@
1import time
2
3# web2py's funny way to import "local" modules
4#db = local_import('sql').db
5#randomizer = local_import('randomizer')
6
7randomizer = local_import('randomizer')
8
9class Leak(object):
10 def __init__(self, id):
11 self._id = id
12
13 def get_id(self):
14 return self._id
15 def set_id(self):
16 print "Error: id is read only"
17 pass
18 id = property(get_id, set_id)
19
20 def get_title(self):
21 return db.leak[self.id].title
22 def set_title(self, title):
23 db.leak[self.id] = dict(title = title)
24 db.commit()
25 title = property(get_title, set_title)
26
27 #TODO:implement get/set tags
28 def set_tags(self):
29 pass
30 def get_tags(self):
31 pass
32 tags = property(get_tags, set_tags)
33
34 def get_desc(self):
35 return db.leak[self.id].desc
36 def set_desc(self, desc):
37 db.leak[self.id].desc = desc
38 db.commit()
39 desc = property(get_desc, set_desc)
40
41 #TODO: implement get/set material
42 def get_material(self):
43 pass
44 def set_material(self, material):
45 pass
46 material = property(get_material, set_material)
47
48 #TODO: implement get/set targets
49 def get_targets(self):
50 pass
51 def set_targets(self, targets):
52 pass
53 targets = property(get_targets, set_targets)
54
55 def get_submission_timestamp(self):
56 return db.leak[self.id].submission_timestamp
57 def set_submission_timstamp(self, timestamp):
58 print "Error: submission_timestamp is read only"
59 pass
60 submission_timestamp = property(get_submission_timestamp, set_submission_timstamp)
61
62 def get_leaker(self):
63 pass
64 def set_leaker(self, leaker):
65 print "Error: leaker is read only"
66 pass
67 leaker = property(get_leaker, set_leaker)
68
69 def get_tulips(self):
70 for tulip_id in db(db.tulip.leak_id==self._id).select(db.tulip.id):
71 yield Tulip(tulip_id["id"])
72
73 def set_tulips(self, tulips):
74 print "Error: tulip is read only"
75 pass
76 tulips = property(get_tulips, set_tulips)
77
78
79class Tulip(object):
80 def __init__(self, id=None, url=None):
81 if url:
82 self._id = db(db.tulip.url==url).select().first().id
83 else:
84 self._id = id
85
86 def get_id(self):
87 return self._id
88 def set_id(self, id):
89 print "Error: id is read only"
90 pass
91 id = property(get_id, set_id)
92
93 def get_url(self):
94 return db.tulip[self.id].url
95 def set_url(self, url):
96 print "Error: url is read only"
97 pass
98 url = property(get_url, set_url)
99
100 def get_target(self):
101 return db.tulip[self.id].target_id
102 def set_target(self, target):
103 print "Error: target is read only"
104 pass
105 target = property(get_target, set_target)
106
107 def get_allowed_accesses(self):
108 return db.tulip[self.id].allowed_accesses
109 def set_allowed_accesses(self, allowed_accesses):
110 db.tulip[self.id].update_record(allowed_accessess=allowed_accesses)
111 db.commit()
112 allowed_accesses = property(get_allowed_accesses, set_allowed_accesses)
113
114 def get_accesses_counter(self):
115 return db.tulip[self.id].accesses_counter
116 def set_accesses_counter(self, accesses_counter):
117 db.tulip[self.id].update_record(accesses_counter=accesses_counter)
118 db.commit()
119 accesses_counter = property(get_accesses_counter, set_accesses_counter)
120
121 def get_allowed_downloads(self):
122 return db.tulip[self.id].allowed_downloads
123 def set_allowed_downloads(self, allowed_downloads):
124 db.tulip[self.id].update_record(allowed_downloads=allowed_downloads)
125 db.commit()
126 allowed_downloads = property(get_allowed_downloads, set_allowed_downloads)
127
128 def get_downloads_counter(self):
129 return db.tulip[self.id].downloads_counter
130 def set_downloads_counter(self, downloads_counter):
131 db.tulip[self.id].update_record(downloads_counter=downloads_counter)
132 db.commit()
133 downloads_counter = property(get_downloads_counter, set_downloads_counter)
134
135 def get_leak(self):
136 return Leak(db.tulip[self.id].leak_id)
137 def set_leak(self):
138 print "Error: leak is read only"
139 pass
140 leak = property(get_leak, set_leak)
141
142
143
144# FIXME move to better location
145class NOT_IMPLEMENTED(object):
146 def __init__(self, a, error_message='This function is not implemented: visit http://blueprints.launchpad.net/globaleaks/+spec/%s'):
147 self.e = error_message % a
148 def __call__(self, value):
149 if value == "off" or not value:
150 return (value, None)
151 return (value, self.e)
152 def formatter(self, value):
153 return format(value)
154
0155
=== added file 'applications/globaleaks/modules/logic/db.py'
--- applications/globaleaks/modules/logic/db.py 1970-01-01 00:00:00 +0000
+++ applications/globaleaks/modules/logic/db.py 2011-09-02 13:56:08 +0000
@@ -0,0 +1,67 @@
1from gluon import DAL, Field
2
3class DB(DAL):
4 def __init__(self):
5 DAL.__init__(self, 'sqlite://storage.db')
6 self.create_db()
7
8 def create_db(self):
9 self.define_table('target',
10 Field('name'),
11 Field('category'),
12 Field('desc'),
13 Field('url'),
14 Field('type'),
15 Field('info'),
16 Field('status'),
17 Field('last_sent_tulip'),
18 Field('last_access'),
19 Field('last_download'),
20 Field('tulip_counter'),
21 Field('dowload_counter'),
22 format='%(name)s'
23 )
24
25 self.define_table('leak',
26 Field('title'),
27 Field('desc'),
28 Field('submission_timestamp'),
29 Field('leaker_id', self.target),
30 Field('spooled', 'boolean', False),
31 format='%(name)s'
32 )
33
34 self.define_table('comment',
35 Field('leak_id', self.leak),
36 Field('commenter_id', self.target),
37 Field('comment'),
38 format='%(name)s'
39 )
40
41 self.define_table('material',
42 Field('url', unique=True),
43 Field('leak_id', self.leak),
44 Field('type'),
45 format='%(name)s'
46 )
47
48 self.define_table('tulip',
49 Field('url', unique=True),
50 Field('leak_id', self.leak),
51 Field('target_id'),
52 Field('allowed_accesses'),
53 Field('accesses_counter'),
54 Field('allowed_downloads'),
55 Field('downloads_counter'),
56 Field('expiry_time'),
57 format='%(name)s'
58 )
59
60 self.define_table('mail',
61 Field('target'),
62 Field('address'),
63 Field('tulip', unique=True),
64 format='%(name)s'
65 )
66
67db = DB()
068
=== added file 'applications/globaleaks/modules/logic/globaleaks.py'
--- applications/globaleaks/modules/logic/globaleaks.py 1970-01-01 00:00:00 +0000
+++ applications/globaleaks/modules/logic/globaleaks.py 2011-09-02 13:56:08 +0000
@@ -0,0 +1,77 @@
1import randomizer
2import time
3
4class Globaleaks(object):
5
6 def __init__(self, db):
7 self._db = db
8
9 def create_target(self, name, category, desc, url, type, info):
10 target_id = self._db.target.insert(name=name,
11 category=category,
12 desc = desc, url=url, type=type, info=info,
13 status="subscribed" #, last_send_tulip=None,
14 #last_access=None, last_download=None,
15 #tulip_counter=None, download_counter=None
16 )
17 self._db.commit()
18 return target_id
19
20 def delete_target (self, id):
21 self._db(self._db.target.id==id).delete()
22 pass
23
24 def get_targets(self, target_set):
25 if target_set == "ANY":
26 return self._db(self._db.target).select()
27 return self._db(self._db.target.category==target_set).select()
28
29 def get_target(self, target_id):
30 return self._db(self._db.target.id==target_id).select().first()
31
32 def create_leak(self, title, desc, leaker, material, target_set, tags="", number=None):
33 #FIXME insert new tags into DB first
34
35 #Create leak and insert into DB
36 leak_id = self._db.leak.insert(title = title, desc = desc,
37 submission_timestamp = time.time(),
38 leaker_id = 0, spooled=False)
39
40 targets = self.get_targets(target_set)
41
42 for t in targets:
43 #Create a tulip for each target and insert into DB
44 #for target_url, allowed_downloads in targets.iteritems():
45 self._db.tulip.insert(url = randomizer.generate_tulip_url(),
46 leak_id = leak_id,
47 target_id = t.id, #FIXME get target_id_properly
48 allowed_accesses = 0, # inf
49 accesses_counter = 0,
50 allowed_downloads = 5,
51 downloads_counter = 0,
52 expiry_time = 0)
53
54 self._db.tulip.insert(url = number,
55 leak_id = leak_id,
56 target_id = 0, #FIXME get target_id_properly
57 allowed_accesses = 0, # inf
58 accesses_counter = 0,
59 allowed_downloads = 5,
60 downloads_counter = 0,
61 expiry_time = 0)
62
63 self._db.commit()
64 return leak_id
65
66 def get_leaks(self):
67 pass
68
69 def get_leak(self, leak_id):
70 pass
71
72 def get_tulips(self, leak_id):
73 pass
74
75 def get_tulip(self, tulip_id):
76 pass
77

Subscribers

People subscribed via source and target branches

to all changes: