source: WAeUP_SRP/trunk/skins/waeup_pins/search_pins.py @ 8794

Last change on this file since 8794 was 5533, checked in by Henrik Bettermann, 14 years ago

allow isouaba to see pins

File size: 5.9 KB
Line 
1## Script (Python) "search_pins"
2##bind container=container
3##bind context=context
4##bind namespace=
5##bind script=script
6##bind subpath=traverse_subpath
7##parameters=REQUEST
8##title=
9##
10# $Id: search_students.py 911 2006-11-20 15:11:29Z henrik $
11"""
12list Students for ClearanceOfficers
13"""
14try:
15    from Products.zdb import set_trace
16except:
17    def set_trace():
18        pass
19
20import datetime
21
22request = REQUEST
23wftool = context.portal_workflow
24mtool = context.portal_membership
25member = mtool.getAuthenticatedMember()
26roles = member.getRolesInContext(context)
27pincat = context.portal_pins
28students = context.portal_url.getPortalObject().campus.students
29def check_hostel_pins(term):
30    items = pincat(prefix_batch = term)
31    not_found = []
32    #from Products.zdb import set_trace;set_trace()
33    for item in items:
34        if item.student == '':
35            continue
36        student = getattr(students,item.student,None)
37        if student is None:
38            not_found.append(item)
39        elif not "accommodation_2006" in student.objectIds():
40            not_found.append(item)
41        else:
42            acco = student.accommodation_2006
43            acco_doc = acco.getContent()
44            pin = "".join(str(acco_doc.acco_res_sc_pin).split('-'))
45            if pin != item.pin:
46                not_found.append(item)
47    return not_found
48
49def set_used_pins(items, from_cat=False):
50    l = []
51    for i in items:
52        item = {}
53        if from_cat:
54            sno = i.student
55            prefix = i.prefix_batch
56            serial = i.serial
57            pin = i.pin
58        else:
59            sno = i['student']
60            prefix = i.get('prefix_batch')
61            serial = i.get('serial')
62            pin = i.get('pin')
63        item['student'] = sno
64        item['prefix'] = prefix
65        item['serial'] = serial
66        if len(sno) > 0:
67            item['pin'] = pin
68        else:
69            if str(member) in ('admin','isouaba'):
70                item['pin'] = "%s" % (pin,)
71            else:
72                item['pin'] = "%s%s****%s" % (i.prefix_batch,pin[-10:-7],pin[-3:])
73
74        if sno.startswith('disabled'):
75            item['student_url'] = None
76            item['student_id'] = sno
77        elif len(sno)==10 or '/' in sno:
78            #res = context.portal_catalog(SearchableText=sno,portal_type='StudentApplication')
79            res = context.students_catalog(jamb_reg_no=sno.upper())
80            if len(res) > 0:
81                item['student_url'] = "%s/campus/students/%s/application" % (context.portal_url(),res[0].id)
82                item['student_id'] = res[0].id
83            else:
84                item['student_url'] = None
85                item['student_id'] = ''
86        elif len(sno) == 7:
87            item['student_url'] = '%s/campus/students/%s' % (context.portal_url(),item['student'])
88            item['student_id'] = item['student']
89        elif sno.startswith(prefix):
90            item['student_url'] = None
91            item['student_id'] = ''       
92        else:
93            item['student_url'] = ''
94            item['student_id'] = "not used"
95        l.append(item)
96    return l
97
98lt = context.portal_layouts
99validate = request.has_key("cpsdocument_edit_button")
100default = {'search_mode': 'student',}
101rend,psm,ds = lt.renderLayout(layout_id= 'scratch_card_search',
102                      schema_id= 'student_search',
103                      context=context,
104                      mapping=validate and request,
105                      ob=default,
106                      layout_mode='edit',
107                      formaction="search_pins",
108                      commit = False,
109                      )
110info = {}
111info['used'] = []
112#from Products.zdb import set_trace;set_trace()
113if context.portal_type == "ScratchCardBatch":
114    info['batch_doc'] = context.getContent()
115    #info['used'] = set_used_pins(info['batch_doc'].getUsedPins())
116    info['used'] = []
117    info['unused'] = info['batch_doc'].getUnusedPins()
118    info['nr_used'] = info['batch_doc'].getNumberOfUsedPins()
119    view = context.batch_view
120elif context.portal_type == "ScratchCardBatchesFolder":
121
122    batches = []
123    for id, object in context.objectItems():
124        batch = {}
125        batch['id'] = id
126        batch['date'] = object.creation_date
127        batch['url'] = object.absolute_url()
128        batch['date_str'] = object.creation_date.strftime("%d/%m/%y")
129        batches.append(batch)
130    batches.sort(cmp=lambda x,y: cmp(x['id'].lower(), y['id'].lower()))
131    #batches.sort(cmp=lambda x,y: cmp(x['date'], y['date']))
132
133    #from Products.zdb import set_trace;set_trace()
134    #batches = context.objectIds()
135    #batches.sort()
136    l = []
137    cols = 4
138    rows,rest = divmod(len(batches),cols)
139    bis = 0
140    for r in range(rows):
141        von = r*cols
142        bis = von + cols
143        l += batches[von:bis],
144    if rest:
145        von = bis
146        l += batches[von:von+rest],
147    info['batches'] = l
148    view = context.pins_view
149if psm == '':
150    return view(rendered = rend,
151                psm = psm,
152                #psm = "%s, %s" % (psm,ds),
153                info = info,
154                allowed = True,
155                )
156what = ds.get('search_mode')
157term = ds.get('search_string')
158
159items=[]
160if term != "":
161    if what == 'student' :
162        items_1 = pincat(student = term.upper())
163        items_2 = pincat(student = term.lower())
164        items = items_1 + items_2
165    elif what == 'pin':
166        term = term.replace('-','').strip().upper()
167        if context.portal_type == "ScratchCardBatch" and term == "HOS1":
168            items = check_hostel_pins(term)
169        else:
170            items = pincat(pin = term)
171    elif what == 'serial':
172        try:
173            snr = int(term.strip())
174            items = pincat(serial = snr)
175        except ValueError:
176            psm = "invalid number"
177            items = []
178            pass
179    else:
180        items = []
181
182info['used'] = set_used_pins(items,from_cat=True)
183
184
185
186return view(rendered = rend,
187            psm = psm,
188            #psm = "%s, %s" % (psm,ds),
189            info = info,
190            allowed = True,
191            )
192
Note: See TracBrowser for help on using the repository browser.