1 | ## Script (Python) "writeStudentImages" |
---|
2 | ##bind container=container |
---|
3 | ##bind context=context |
---|
4 | ##bind namespace= |
---|
5 | ##bind script=script |
---|
6 | ##bind subpath=traverse_subpath |
---|
7 | ##parameters= |
---|
8 | ##title= |
---|
9 | ## |
---|
10 | # $Id: removeImagesFromZODB.py 2419 2007-10-22 19:37:42Z henrik $ |
---|
11 | """ |
---|
12 | """ |
---|
13 | try: |
---|
14 | from Products.zdb import set_trace |
---|
15 | except: |
---|
16 | def set_trace(): |
---|
17 | pass |
---|
18 | |
---|
19 | mtool = context.portal_membership |
---|
20 | member = mtool.getAuthenticatedMember() |
---|
21 | if str(member) not in ('admin','joachim'): |
---|
22 | return |
---|
23 | |
---|
24 | |
---|
25 | import logging |
---|
26 | import DateTime |
---|
27 | logger = logging.getLogger('Skins.removeImagesFromZODB') |
---|
28 | |
---|
29 | request = context.REQUEST |
---|
30 | session = request.SESSION |
---|
31 | response = request.RESPONSE |
---|
32 | setheader = request.RESPONSE.setHeader |
---|
33 | def rwrite(s): |
---|
34 | response.setHeader('Content-type','text/html; charset=ISO-8859-15') |
---|
35 | response.write("%s<br>\n\r" % s) |
---|
36 | |
---|
37 | #logger.info('') |
---|
38 | students = context.students_catalog() |
---|
39 | starttime = DateTime.DateTime() |
---|
40 | logger.info("Start removing %d student images" % len(students)) |
---|
41 | count = 0 |
---|
42 | chunk = 50 |
---|
43 | stool = context.portal_schemas |
---|
44 | schemas = ['student_application', |
---|
45 | #'student_clearance', |
---|
46 | ] |
---|
47 | students_folder = context.portal_url.getPortalObject().campus.students |
---|
48 | image_keys = {} |
---|
49 | for schema_id in schemas: |
---|
50 | schema = getattr(stool,schema_id) |
---|
51 | image_keys[schema_id] = [key for key in schema.keys() |
---|
52 | if schema[key].meta_type == "CPS Image Field"] |
---|
53 | for student in students: |
---|
54 | count += 1 |
---|
55 | if count and not count % chunk: |
---|
56 | logger.info("removed %d of %d student images " % (count,len(students))) |
---|
57 | context.waeup_tool.doCommit() |
---|
58 | for schema_id in schemas: |
---|
59 | try: |
---|
60 | doc_id = getattr(getattr(students_folder,student.id), |
---|
61 | schema_id[len('student_'):]).getContent().getId() |
---|
62 | except: |
---|
63 | continue |
---|
64 | doc = context.portal_repository[doc_id] |
---|
65 | ids = [key for key in image_keys[schema_id] |
---|
66 | if key in doc.objectIds()] |
---|
67 | if not ids: |
---|
68 | continue |
---|
69 | try: |
---|
70 | doc.manage_delObjects(ids) |
---|
71 | logger.info("removed %s images " % (student.id)) |
---|
72 | except: |
---|
73 | set_trace() |
---|
74 | logger.info("!!!!!!!could not remove %s images " % (student.id)) |
---|
75 | logger.info("Finished removing %d student images" % len(students)) |
---|