from zope.formlib import form from zope import interface, schema from zope.interface import implements from zope.component import adapts from Products.iva.interfaces import INewCourse, ICourse, IUserManager from Products.iva.common import convert_date, translate, perm_view, perm_manage from Products.Five import BrowserView from AccessControl.SecurityInfo import ClassSecurityInfo from AccessControl import getSecurityManager from zope.component import getUtility from Globals import InitializeClass import time class NewCourseView(BrowserView): """ new course request view """ security = ClassSecurityInfo() security.declarePublic('handler') def handler(self, **kw): form = self.request.form if self.request.REQUEST_METHOD.lower() != 'post': return self.request.response.redirect('organizer_index') if "course_req_button" in form: empty_fields = False _marker = object() for x in ('my_name', 'cCat', 'desc', 'methods', 'credits', 'courseID', 'start_date', 'end_date', 'phone'): val = self.request.get(x, _marker) if not val: empty_fields = True break if empty_fields: return self.context.message_dialog(action='courses/organizer_new_course', message='All fields my be filled') nc = INewCourse(self.context) res = nc.register_new_course_request(self.request) if not res: return self.context.message_dialog(action='organizer_index', message='Request for a new course submitted!') if "addcourse_button" in form: nc = INewCourse(self.context) res = nc._add_new_course(self.request) if res == 0: raise 'creation of course failed!' return self.context.change_course(self.request, res) if "deletefromlist_button" in form: nc = INewCourse(self.context) res = nc._delete_course_req(self.request) return self.request.response.redirect('manage_pending_courses') return self.request.response.redirect('organizer_index') security.declareProtected(perm_manage, 'getPendingCourses') def getPendingCourses(self): """ return a list of pending courses """ nc = INewCourse(self.context) return nc._getPendingCourses() InitializeClass(NewCourseView) class NewCourse: #adapts(ICourse) implements(INewCourse) def __init__(self, context): self.context = context def register_new_course_request(self, req): """ register new course request """ if not hasattr(self.context.aq_base, 'course_reqs'): self.context.course_reqs = [] user = str(getSecurityManager().getUser()) c = {'user': user} c['time'] = str(time.time()) for x in ('my_name', 'cCat', 'desc', 'methods', 'credits', 'courseID', 'start_date', 'end_date', 'phone'): c[x] = req.get(x) self.context.course_reqs.append(c) self.context._p_changed = True return 0 def _getPendingCourses(self): if not hasattr(self.context.aq_base, 'course_reqs'): self.context.course_reqs = [] return self.context.course_reqs def _add_new_course(self, req): from Products.iva.Course import Course suffix = '_'+str(int(time.time())) uniq_id = self.context.generateId(prefix='course', suffix=suffix, rand_ceiling=99999999999) cr = self._get_req_by_time(req.get("course_to_add")) obj = Course( self.context, '', (cr['user'],), '', '', '', '', '', uniq_id) from Products.iva import strptime time_tuple = strptime.strptime(cr['start_date'], translate(self.context, 'short_date_format', default="%Y-%m-%d", target=self.context.giveLanguage(req))) starting_date = convert_date(str(time_tuple[2]), # day str(time_tuple[1]), # month str(time_tuple[0])) # year time_tuple = strptime.strptime(cr['end_date'], translate(self.context, 'short_date_format', default="%Y-%m-%d", target=self.context.giveLanguage(req))) ending_date = convert_date(str(time_tuple[2]), # day str(time_tuple[1]), # month str(time_tuple[0])) # year obj.update( cr['my_name'], cr['desc'], '', cr['methods'], starting_date, ending_date, '', cCat = cr['cCat'], credits = cr['credits'], courseID = cr['courseID']) self.context._setObject(obj.id, obj) c = getattr(self.context, obj.id) c.add_folder('CourseFolder') # send an inner message to user. msg = req.get('message') if msg: mail_to = cr['user'] um = getUtility(IUserManager) ui = um.get_user_info(mail_to) title = translate(self.context, 'About new course request', default='About new course request', target=self.context.giveLanguage(req)) ui.kirjad.saadaKiri(req, mail_to, mail_to, title, msg, 999999) self._delete_course(req.get("course_to_add")) return obj.id def _delete_course_req(self, req): cr = self._get_req_by_time(req.get("course_to_add")) msg = req.get('message') if msg: mail_to = cr['user'] um = getUtility(IUserManager) ui = um.get_user_info(mail_to) title = translate(self.context, 'About new course request', default='About new course request', target=self.context.giveLanguage(req)) ui.kirjad.saadaKiri(req, mail_to, mail_to, title, msg, 999999) self._delete_course(req.get("course_to_add")) def _delete_course(self, iid): tmp = [] for x in self.context.course_reqs: if x.get('time') == iid: continue tmp.append(x) self.context.course_reqs = tmp def _get_req_by_time(self, tim): for x in self.context.course_reqs: if x['time'] == tim: return x raise 'course request not found'