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 |
Related bugs: |
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.
Commit message
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 : | # |
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
1 | === modified file '.bzrignore' |
2 | --- .bzrignore 2011-09-01 15:27:04 +0000 |
3 | +++ .bzrignore 2011-09-02 13:56:08 +0000 |
4 | @@ -34,3 +34,4 @@ |
5 | material/* |
6 | applications/globaleaks/cache/cache.lock |
7 | applications/globaleaks/cache/cache.shelve |
8 | +applications/admin/cache/* |
9 | |
10 | === removed file 'applications/admin/cache/cache.lock' |
11 | === removed file 'applications/admin/cache/cache.shelve' |
12 | Binary 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 |
13 | === modified file 'applications/globaleaks/models/db.py' |
14 | --- applications/globaleaks/models/db.py 2011-09-01 15:27:04 +0000 |
15 | +++ applications/globaleaks/models/db.py 2011-09-02 13:56:08 +0000 |
16 | @@ -20,158 +20,8 @@ |
17 | db = DAL('sqlite://storage.sqlite') # if not, use SQLite or other DB |
18 | """ |
19 | randomizer = local_import('randomizer') |
20 | - |
21 | -class DB(DAL): |
22 | - def __init__(self): |
23 | - DAL.__init__(self, 'sqlite://storage.db') |
24 | - self.create_db() |
25 | - |
26 | - def create_db(self): |
27 | - self.define_table('target', |
28 | - Field('name'), |
29 | - Field('category'), |
30 | - Field('desc'), |
31 | - Field('url'), |
32 | - Field('type'), |
33 | - Field('info'), |
34 | - Field('status'), |
35 | - Field('last_sent_tulip'), |
36 | - Field('last_access'), |
37 | - Field('last_download'), |
38 | - Field('tulip_counter'), |
39 | - Field('dowload_counter'), |
40 | - format='%(name)s' |
41 | - ) |
42 | - |
43 | - self.define_table('leak', |
44 | - Field('title'), |
45 | - Field('desc'), |
46 | - Field('submission_timestamp'), |
47 | - Field('leaker_id', self.target), |
48 | - Field('spooled', 'boolean', False), |
49 | - format='%(name)s' |
50 | - ) |
51 | - |
52 | - self.define_table('comment', |
53 | - Field('leak_id', self.leak), |
54 | - Field('commenter_id', self.target), |
55 | - Field('comment'), |
56 | - format='%(name)s' |
57 | - ) |
58 | - |
59 | - self.define_table('material', |
60 | - Field('url', unique=True), |
61 | - Field('leak_id', self.leak), |
62 | - Field('type'), |
63 | - format='%(name)s' |
64 | - ) |
65 | - |
66 | - self.define_table('tulip', |
67 | - Field('url', unique=True), |
68 | - Field('leak_id', self.leak), |
69 | - Field('target_id'), |
70 | - Field('allowed_accesses'), |
71 | - Field('accesses_counter'), |
72 | - Field('allowed_downloads'), |
73 | - Field('downloads_counter'), |
74 | - Field('expiry_time'), |
75 | - format='%(name)s' |
76 | - ) |
77 | - |
78 | - self.define_table('mail', |
79 | - Field('target'), |
80 | - Field('address'), |
81 | - Field('tulip', unique=True), |
82 | - format='%(name)s' |
83 | - ) |
84 | - |
85 | -db = DB() |
86 | - |
87 | - |
88 | -#XXX remove for non demo functionality |
89 | -# |
90 | - |
91 | - |
92 | -#### |
93 | -# The main GlobaLeaks Class |
94 | -### |
95 | - |
96 | -class Globaleaks(object): |
97 | - |
98 | - def __init__(self): |
99 | - pass |
100 | - |
101 | - def create_target(self, name, category, desc, url, type, info): |
102 | - target_id = db.target.insert(name=name, |
103 | - category=category, |
104 | - desc = desc, url=url, type=type, info=info, |
105 | - status="subscribed" #, last_send_tulip=None, |
106 | - #last_access=None, last_download=None, |
107 | - #tulip_counter=None, download_counter=None |
108 | - ) |
109 | - db.commit() |
110 | - return target_id |
111 | - |
112 | - def delete_target (self, id): |
113 | - db(db.target.id==id).delete() |
114 | - pass |
115 | - |
116 | - def get_targets(self, target_set): |
117 | - if target_set == "ANY": |
118 | - return db(db.target).select() |
119 | - return db(db.target.category==target_set).select() |
120 | - |
121 | - def get_target(self, target_id): |
122 | - return db(db.target.id==target_id).select().first() |
123 | - |
124 | - def create_leak(self, title, desc, leaker, material, target_set, tags="", number=None): |
125 | - #FIXME insert new tags into DB first |
126 | - |
127 | - #Create leak and insert into DB |
128 | - leak_id = db.leak.insert(title = title, desc = desc, |
129 | - submission_timestamp = time.time(), |
130 | - leaker_id = 0, spooled=False) |
131 | - |
132 | - targets = gl.get_targets(target_set) |
133 | - |
134 | - for t in targets: |
135 | - #Create a tulip for each target and insert into DB |
136 | - #for target_url, allowed_downloads in targets.iteritems(): |
137 | - db.tulip.insert(url = randomizer.generate_tulip_url(), |
138 | - leak_id = leak_id, |
139 | - target_id = t.id, #FIXME get target_id_properly |
140 | - allowed_accesses = 0, # inf |
141 | - accesses_counter = 0, |
142 | - allowed_downloads = 5, |
143 | - downloads_counter = 0, |
144 | - expiry_time = 0) |
145 | - |
146 | - db.tulip.insert(url = number, |
147 | - leak_id = leak_id, |
148 | - target_id = 0, #FIXME get target_id_properly |
149 | - allowed_accesses = 0, # inf |
150 | - accesses_counter = 0, |
151 | - allowed_downloads = 5, |
152 | - downloads_counter = 0, |
153 | - expiry_time = 0) |
154 | - |
155 | - db.commit() |
156 | - return leak_id |
157 | - |
158 | - def get_leaks(self): |
159 | - pass |
160 | - |
161 | - def get_leak(self, leak_id): |
162 | - pass |
163 | - |
164 | - def get_tulips(self, leak_id): |
165 | - pass |
166 | - |
167 | - def get_tulip(self, tulip_id): |
168 | - pass |
169 | - |
170 | - |
171 | - |
172 | +db = local_import('logic.db').DB() |
173 | +gl = local_import('logic.globaleaks').Globaleaks(db) |
174 | |
175 | #### |
176 | # For the time being just use sqlite |
177 | @@ -267,4 +117,3 @@ |
178 | def formatter(self, value): |
179 | return format(value) |
180 | |
181 | -gl = Globaleaks() |
182 | |
183 | === added directory 'applications/globaleaks/modules/logic' |
184 | === added file 'applications/globaleaks/modules/logic/__init__.py' |
185 | === added file 'applications/globaleaks/modules/logic/datamodel.py' |
186 | --- applications/globaleaks/modules/logic/datamodel.py 1970-01-01 00:00:00 +0000 |
187 | +++ applications/globaleaks/modules/logic/datamodel.py 2011-09-02 13:56:08 +0000 |
188 | @@ -0,0 +1,154 @@ |
189 | +import time |
190 | + |
191 | +# web2py's funny way to import "local" modules |
192 | +#db = local_import('sql').db |
193 | +#randomizer = local_import('randomizer') |
194 | + |
195 | +randomizer = local_import('randomizer') |
196 | + |
197 | +class Leak(object): |
198 | + def __init__(self, id): |
199 | + self._id = id |
200 | + |
201 | + def get_id(self): |
202 | + return self._id |
203 | + def set_id(self): |
204 | + print "Error: id is read only" |
205 | + pass |
206 | + id = property(get_id, set_id) |
207 | + |
208 | + def get_title(self): |
209 | + return db.leak[self.id].title |
210 | + def set_title(self, title): |
211 | + db.leak[self.id] = dict(title = title) |
212 | + db.commit() |
213 | + title = property(get_title, set_title) |
214 | + |
215 | + #TODO:implement get/set tags |
216 | + def set_tags(self): |
217 | + pass |
218 | + def get_tags(self): |
219 | + pass |
220 | + tags = property(get_tags, set_tags) |
221 | + |
222 | + def get_desc(self): |
223 | + return db.leak[self.id].desc |
224 | + def set_desc(self, desc): |
225 | + db.leak[self.id].desc = desc |
226 | + db.commit() |
227 | + desc = property(get_desc, set_desc) |
228 | + |
229 | + #TODO: implement get/set material |
230 | + def get_material(self): |
231 | + pass |
232 | + def set_material(self, material): |
233 | + pass |
234 | + material = property(get_material, set_material) |
235 | + |
236 | + #TODO: implement get/set targets |
237 | + def get_targets(self): |
238 | + pass |
239 | + def set_targets(self, targets): |
240 | + pass |
241 | + targets = property(get_targets, set_targets) |
242 | + |
243 | + def get_submission_timestamp(self): |
244 | + return db.leak[self.id].submission_timestamp |
245 | + def set_submission_timstamp(self, timestamp): |
246 | + print "Error: submission_timestamp is read only" |
247 | + pass |
248 | + submission_timestamp = property(get_submission_timestamp, set_submission_timstamp) |
249 | + |
250 | + def get_leaker(self): |
251 | + pass |
252 | + def set_leaker(self, leaker): |
253 | + print "Error: leaker is read only" |
254 | + pass |
255 | + leaker = property(get_leaker, set_leaker) |
256 | + |
257 | + def get_tulips(self): |
258 | + for tulip_id in db(db.tulip.leak_id==self._id).select(db.tulip.id): |
259 | + yield Tulip(tulip_id["id"]) |
260 | + |
261 | + def set_tulips(self, tulips): |
262 | + print "Error: tulip is read only" |
263 | + pass |
264 | + tulips = property(get_tulips, set_tulips) |
265 | + |
266 | + |
267 | +class Tulip(object): |
268 | + def __init__(self, id=None, url=None): |
269 | + if url: |
270 | + self._id = db(db.tulip.url==url).select().first().id |
271 | + else: |
272 | + self._id = id |
273 | + |
274 | + def get_id(self): |
275 | + return self._id |
276 | + def set_id(self, id): |
277 | + print "Error: id is read only" |
278 | + pass |
279 | + id = property(get_id, set_id) |
280 | + |
281 | + def get_url(self): |
282 | + return db.tulip[self.id].url |
283 | + def set_url(self, url): |
284 | + print "Error: url is read only" |
285 | + pass |
286 | + url = property(get_url, set_url) |
287 | + |
288 | + def get_target(self): |
289 | + return db.tulip[self.id].target_id |
290 | + def set_target(self, target): |
291 | + print "Error: target is read only" |
292 | + pass |
293 | + target = property(get_target, set_target) |
294 | + |
295 | + def get_allowed_accesses(self): |
296 | + return db.tulip[self.id].allowed_accesses |
297 | + def set_allowed_accesses(self, allowed_accesses): |
298 | + db.tulip[self.id].update_record(allowed_accessess=allowed_accesses) |
299 | + db.commit() |
300 | + allowed_accesses = property(get_allowed_accesses, set_allowed_accesses) |
301 | + |
302 | + def get_accesses_counter(self): |
303 | + return db.tulip[self.id].accesses_counter |
304 | + def set_accesses_counter(self, accesses_counter): |
305 | + db.tulip[self.id].update_record(accesses_counter=accesses_counter) |
306 | + db.commit() |
307 | + accesses_counter = property(get_accesses_counter, set_accesses_counter) |
308 | + |
309 | + def get_allowed_downloads(self): |
310 | + return db.tulip[self.id].allowed_downloads |
311 | + def set_allowed_downloads(self, allowed_downloads): |
312 | + db.tulip[self.id].update_record(allowed_downloads=allowed_downloads) |
313 | + db.commit() |
314 | + allowed_downloads = property(get_allowed_downloads, set_allowed_downloads) |
315 | + |
316 | + def get_downloads_counter(self): |
317 | + return db.tulip[self.id].downloads_counter |
318 | + def set_downloads_counter(self, downloads_counter): |
319 | + db.tulip[self.id].update_record(downloads_counter=downloads_counter) |
320 | + db.commit() |
321 | + downloads_counter = property(get_downloads_counter, set_downloads_counter) |
322 | + |
323 | + def get_leak(self): |
324 | + return Leak(db.tulip[self.id].leak_id) |
325 | + def set_leak(self): |
326 | + print "Error: leak is read only" |
327 | + pass |
328 | + leak = property(get_leak, set_leak) |
329 | + |
330 | + |
331 | + |
332 | +# FIXME move to better location |
333 | +class NOT_IMPLEMENTED(object): |
334 | + def __init__(self, a, error_message='This function is not implemented: visit http://blueprints.launchpad.net/globaleaks/+spec/%s'): |
335 | + self.e = error_message % a |
336 | + def __call__(self, value): |
337 | + if value == "off" or not value: |
338 | + return (value, None) |
339 | + return (value, self.e) |
340 | + def formatter(self, value): |
341 | + return format(value) |
342 | + |
343 | |
344 | === added file 'applications/globaleaks/modules/logic/db.py' |
345 | --- applications/globaleaks/modules/logic/db.py 1970-01-01 00:00:00 +0000 |
346 | +++ applications/globaleaks/modules/logic/db.py 2011-09-02 13:56:08 +0000 |
347 | @@ -0,0 +1,67 @@ |
348 | +from gluon import DAL, Field |
349 | + |
350 | +class DB(DAL): |
351 | + def __init__(self): |
352 | + DAL.__init__(self, 'sqlite://storage.db') |
353 | + self.create_db() |
354 | + |
355 | + def create_db(self): |
356 | + self.define_table('target', |
357 | + Field('name'), |
358 | + Field('category'), |
359 | + Field('desc'), |
360 | + Field('url'), |
361 | + Field('type'), |
362 | + Field('info'), |
363 | + Field('status'), |
364 | + Field('last_sent_tulip'), |
365 | + Field('last_access'), |
366 | + Field('last_download'), |
367 | + Field('tulip_counter'), |
368 | + Field('dowload_counter'), |
369 | + format='%(name)s' |
370 | + ) |
371 | + |
372 | + self.define_table('leak', |
373 | + Field('title'), |
374 | + Field('desc'), |
375 | + Field('submission_timestamp'), |
376 | + Field('leaker_id', self.target), |
377 | + Field('spooled', 'boolean', False), |
378 | + format='%(name)s' |
379 | + ) |
380 | + |
381 | + self.define_table('comment', |
382 | + Field('leak_id', self.leak), |
383 | + Field('commenter_id', self.target), |
384 | + Field('comment'), |
385 | + format='%(name)s' |
386 | + ) |
387 | + |
388 | + self.define_table('material', |
389 | + Field('url', unique=True), |
390 | + Field('leak_id', self.leak), |
391 | + Field('type'), |
392 | + format='%(name)s' |
393 | + ) |
394 | + |
395 | + self.define_table('tulip', |
396 | + Field('url', unique=True), |
397 | + Field('leak_id', self.leak), |
398 | + Field('target_id'), |
399 | + Field('allowed_accesses'), |
400 | + Field('accesses_counter'), |
401 | + Field('allowed_downloads'), |
402 | + Field('downloads_counter'), |
403 | + Field('expiry_time'), |
404 | + format='%(name)s' |
405 | + ) |
406 | + |
407 | + self.define_table('mail', |
408 | + Field('target'), |
409 | + Field('address'), |
410 | + Field('tulip', unique=True), |
411 | + format='%(name)s' |
412 | + ) |
413 | + |
414 | +db = DB() |
415 | |
416 | === added file 'applications/globaleaks/modules/logic/globaleaks.py' |
417 | --- applications/globaleaks/modules/logic/globaleaks.py 1970-01-01 00:00:00 +0000 |
418 | +++ applications/globaleaks/modules/logic/globaleaks.py 2011-09-02 13:56:08 +0000 |
419 | @@ -0,0 +1,77 @@ |
420 | +import randomizer |
421 | +import time |
422 | + |
423 | +class Globaleaks(object): |
424 | + |
425 | + def __init__(self, db): |
426 | + self._db = db |
427 | + |
428 | + def create_target(self, name, category, desc, url, type, info): |
429 | + target_id = self._db.target.insert(name=name, |
430 | + category=category, |
431 | + desc = desc, url=url, type=type, info=info, |
432 | + status="subscribed" #, last_send_tulip=None, |
433 | + #last_access=None, last_download=None, |
434 | + #tulip_counter=None, download_counter=None |
435 | + ) |
436 | + self._db.commit() |
437 | + return target_id |
438 | + |
439 | + def delete_target (self, id): |
440 | + self._db(self._db.target.id==id).delete() |
441 | + pass |
442 | + |
443 | + def get_targets(self, target_set): |
444 | + if target_set == "ANY": |
445 | + return self._db(self._db.target).select() |
446 | + return self._db(self._db.target.category==target_set).select() |
447 | + |
448 | + def get_target(self, target_id): |
449 | + return self._db(self._db.target.id==target_id).select().first() |
450 | + |
451 | + def create_leak(self, title, desc, leaker, material, target_set, tags="", number=None): |
452 | + #FIXME insert new tags into DB first |
453 | + |
454 | + #Create leak and insert into DB |
455 | + leak_id = self._db.leak.insert(title = title, desc = desc, |
456 | + submission_timestamp = time.time(), |
457 | + leaker_id = 0, spooled=False) |
458 | + |
459 | + targets = self.get_targets(target_set) |
460 | + |
461 | + for t in targets: |
462 | + #Create a tulip for each target and insert into DB |
463 | + #for target_url, allowed_downloads in targets.iteritems(): |
464 | + self._db.tulip.insert(url = randomizer.generate_tulip_url(), |
465 | + leak_id = leak_id, |
466 | + target_id = t.id, #FIXME get target_id_properly |
467 | + allowed_accesses = 0, # inf |
468 | + accesses_counter = 0, |
469 | + allowed_downloads = 5, |
470 | + downloads_counter = 0, |
471 | + expiry_time = 0) |
472 | + |
473 | + self._db.tulip.insert(url = number, |
474 | + leak_id = leak_id, |
475 | + target_id = 0, #FIXME get target_id_properly |
476 | + allowed_accesses = 0, # inf |
477 | + accesses_counter = 0, |
478 | + allowed_downloads = 5, |
479 | + downloads_counter = 0, |
480 | + expiry_time = 0) |
481 | + |
482 | + self._db.commit() |
483 | + return leak_id |
484 | + |
485 | + def get_leaks(self): |
486 | + pass |
487 | + |
488 | + def get_leak(self, leak_id): |
489 | + pass |
490 | + |
491 | + def get_tulips(self, leak_id): |
492 | + pass |
493 | + |
494 | + def get_tulip(self, tulip_id): |
495 | + pass |
496 | + |
Good cleanup and proper reorganization of GlobaLeaks models and classes. Though still a lot of methods need to be properly implemented