class RecentActivityUtility(object): """ Utility for recent activities """ grok.implements(IRecentActivity) def __init__(self): self.activities = OOBTree() def add_activity(self, timestamp, action, user, obj, parent): """ Add an activity to the BTree storage """ timestamp = int(time.time()) activity = {'action': action, 'user': user, 'object': obj, 'object_url': obj.absolute_url(), 'parent': parent, 'parent_url': parent.absolute_url(), } self.activities.insert(timestamp, activity) return timestamp def get_recent_activity(self, items=None): """ Get the activities stored in the BTree """ if self.activities: if items: return sorted(self.activities.items(), reverse=True)[:items] else: return sorted(self.activities.items(), reverse=True)
class RecentActivityUtility(Persistent): """Recent Activity Utility """ implements(IRecentActivityUtility) #activities = IOBTree() activities = None def __init__(self): self.activities = OOBTree() def addActivity(self, timestamp, action, user, fullname, object, parent): """Add an activity to the BTree. """ timestamp = int(time.time()) activity = {'action': action, 'user': user, 'fullname': fullname, 'object': object, 'object_url': object.absolute_url(), 'parent': parent, 'parent_url': parent.absolute_url(), } self.activities.insert(timestamp, activity) #from zope.container.contained import ObjectAddedEvent #from zope.container.contained import notifyContainerModified #notify(ObjectAddedEvent(object, self.activities, str(uid))) #notifyContainerModified(self.activities) return timestamp def getRecentActivity(self, items=None): """Get all activities stored in the BTree. """ if self.activities: if items: # Return activities sorted by timestamp return sorted(self.activities.items(), reverse=True)[:items] else: return sorted(self.activities.items(), reverse=True) def manage_fixupOwnershipAfterAdd(self): """This is needed, otherwise we get an Attribute Error when we try to install the product. """ pass
class CanonicalStorage(SimpleItem): implements(IMultilingualStorage) id = 'portal_multilingual' def __init__(self): self.id = id self.canonicals = OOBTree() def get_canonical(self, id): """ get a canonical for a specific content-id """ canonical = None if id in self.canonicals: canonical = self.canonicals[id] return canonical def add_canonical(self, id, canonical): """ add a canonical there is a usecase where the id can already exist on the OOBTree """ if not self.canonicals.insert(id, canonical): # We are going to remove the language on a old canonical # so we need to check if the canonical has other translation active # before removing it canonical_old = self.get_canonical(id) # check if there more than one language on the canonical if len(canonical_old.get_keys()) > 1: canonical_old.remove_item_by_id(id) self.remove_canonical(id) else: self.remove_canonical(id) del canonical_old # import transaction; transaction.commit() self.canonicals.insert(id, canonical) def remove_canonical(self, id): """ remove a canonical """ self.canonicals.pop(id) def get_canonicals(self): """ get all canonicals """ return self.canonicals
def genBTree(src, attributes): """ Generates a B-Tree from the given source file and uses the attributes to generate a key. **Parameters**: src : str the path and file name to the file. attributes : list the list of attributes which define the key **Returns**: A complete B-Tree. """ json_fr = json_file_reader(src) b_tree = OOBTree() while not json_fr.isEOF(): data = json_fr.readRecord() key = keyGen(attributes, data) if not key: continue # Key was not generated, go to next b_tree.insert(key, data) return b_tree
class ZODBMutablePropertyProvider(BasePlugin): """Storage for mutable properties in the ZODB for users/groups. API sounds like it's only for users, but groups work as well. """ meta_type = 'ZODB Mutable Property Provider' security = ClassSecurityInfo() def __init__(self, id, title='', schema=None, **kw): """Create in-ZODB mutable property provider. Provide a schema either as a list of (name,type,value) tuples in the 'schema' parameter or as a series of keyword parameters 'name=value'. Types will be guessed in this case. The 'value' is meant as the default value, and will be used unless the user provides data. If no schema is provided by constructor, the properties of the portal_memberdata object will be used. Types available: string, text, boolean, int, long, float, lines, date """ self.id = id self.title = title self._storage = OOBTree() # calculate schema and default values defaultvalues = {} if not schema and not kw: schema = () elif not schema and kw: schema = _guessSchema(kw) defaultvalues = kw else: valuetuples = [(name, value) for name, type, value in schema] schema = [(name, type) for name, type, value in schema] for name, value in valuetuples: defaultvalues[name] = value self._schema = tuple(schema) self._defaultvalues = defaultvalues def _getSchema(self, isgroup=None): # this could probably stand to be cached datatool = isgroup and "portal_groupdata" or "portal_memberdata" schema = self._schema if not schema: # if no schema is provided, use portal_memberdata properties schema = () mdtool = getToolByName(self, datatool, None) # Don't fail badly if tool is not available. if mdtool is not None: mdschema = mdtool.propertyMap() schema = [(elt['id'], elt['type']) for elt in mdschema] return schema def _getDefaultValues(self, isgroup=None): """Returns a dictionary mapping of property names to default values. Defaults to portal_*data tool if necessary. """ datatool = isgroup and "portal_groupdata" or "portal_memberdata" defaultvalues = self._defaultvalues if not self._schema: # if no schema is provided, use portal_*data properties defaultvalues = {} mdtool = getToolByName(self, datatool, None) # Don't fail badly if tool is not available. if mdtool is not None: # we rely on propertyMap and propertyItems mapping mdvalues = mdtool.propertyItems() for name, value in mdvalues: # For selection types the default value is the name of a # method which returns the possible values. There is no way # to set a default value for those types. ptype = mdtool.getPropertyType(name) if ptype == "selection": defaultvalues[name] = "" elif ptype == "multiple selection": defaultvalues[name] = [] else: defaultvalues[name] = value # ALERT! if someone gives their *_data tool a title, and want a # title as a property of the user/group (and groups do by # default) we don't want them all to have this title, since # a title is used in the UI if it exists if defaultvalues.get("title"): defaultvalues["title"] = "" return defaultvalues @security.private def getPropertiesForUser(self, user, request=None): """Get property values for a user or group. Returns a dictionary of values or a PropertySheet. This implementation will always return a MutablePropertySheet. NOTE: Must always return something, or else the property sheet won't get created and this will screw up portal_memberdata. """ isGroup = getattr(user, 'isGroup', lambda: None)() data = self._storage.get(user.getId()) defaults = self._getDefaultValues(isGroup) # provide default values where missing if not data: data = {} for key, val in defaults.items(): if key not in data: data[key] = val return MutablePropertySheet(self.id, schema=self._getSchema(isGroup), **data) @security.private def setPropertiesForUser(self, user, propertysheet): """Set the properties of a user or group based on the contents of a property sheet. """ isGroup = getattr(user, 'isGroup', lambda: None)() properties = dict(propertysheet.propertyItems()) for name, property_type in self._getSchema(isGroup) or (): if (name in properties and not validateValue(property_type, properties[name])): raise ValueError('Invalid value: %s does not conform to %s' % (name, property_type)) allowed_prop_keys = [pn for pn, pt in self._getSchema(isGroup) or ()] if allowed_prop_keys: prop_names = set(properties.keys()) - set(allowed_prop_keys) if prop_names: raise ValueError('Unknown Properties: %r' % prop_names) userid = user.getId() userprops = self._storage.get(userid) properties.update({'isGroup': isGroup}) if userprops is not None: userprops.update(properties) # notify persistence machinery of change self._storage[userid] = self._storage[userid] else: self._storage.insert(user.getId(), properties) @security.private def deleteUser(self, user_id): """Delete all user properties """ # Do nothing if an unknown user_id is given try: del self._storage[user_id] except KeyError: pass @security.private def testMemberData(self, memberdata, criteria, exact_match=False): """Test if a memberdata matches the search criteria. """ for (key, value) in criteria.items(): testvalue = memberdata.get(key, None) if testvalue is None: return False if isStringType(testvalue): testvalue = safe_unicode(testvalue.lower()) if isStringType(value): value = safe_unicode(value.lower()) if exact_match: if value != testvalue: return False else: try: if value not in testvalue: return False except TypeError: # Fall back to exact match if we can check for # sub-component if value != testvalue: return False return True @security.private def enumerateUsers(self, id=None, login=None, exact_match=False, **kw): """ See IUserEnumerationPlugin. """ plugin_id = self.getId() # This plugin can't search for a user by id or login, because there is # no such keys in the storage (data dict in the comprehensive list) # If kw is empty or not, we continue the search. if id is not None or login is not None: return () criteria = copy.copy(kw) users = [(user, data) for (user, data) in self._storage.items() if self.testMemberData(data, criteria, exact_match) and not data.get('isGroup', False)] user_info = [{ 'id': self.prefix + user_id, 'login': user_id, 'title': data.get('fullname', user_id), 'description': data.get('fullname', user_id), 'email': data.get('email', ''), 'pluginid': plugin_id } for (user_id, data) in users] return tuple(user_info) def updateUser(self, user_id, login_name): """ Update the login name of the user with id user_id. This is a new part of the IUserEnumerationPlugin interface, but not interesting for us. """ pass def updateEveryLoginName(self, quit_on_first_error=True): """Update login names of all users to their canonical value. This is a new part of the IUserEnumerationPlugin interface, but not interesting for us. """ pass
class LookupTable(base.Base): "LookupTable class" meta_type = "LookupTable" security = ClassSecurityInfo() records = None recordsLength = None drawDict = base.Base.drawDict.copy() drawDict['drawTable'] = 'drawTable' security.declareProtected('View management screens', 'edit') def edit(self, *args, **kw): "Inline edit short object" format = "<p>Currently there are %s records</p><div>%s</div>" if self.records is None: self.records = OOBTree() lenRecords = self.recordsLength() if self.recordsLength is not None else 0 return format % (lenRecords, self.create_button('clear', "Clear")) security.declarePrivate('processRecorderChanges') def processRecorderChanges(self, form): "process the recorder changes" clear = form.pop('clear', None) if clear is not None: self.clear() security.declarePrivate('after_manage_edit') def before_manage_edit(self, form): "process the edits" self.processRecorderChanges(form) security.declareProtected('View management screens', "drawTable") def drawTable(self): "Render page" temp = [] format = '<p>%s:%s</p>' if self.records is not None: for key,value in self.records.items(): temp.append(format % (repr(key), repr(value))) return ''.join(temp) security.declareProtected('Python Record Modification', 'insert') def insert(self, key, value): "modify this key and value into the OOBTree" if self.records is None: self.records = OOBTree() if self.recordsLength is None: self.setObject('recordsLength' ,BTrees.Length.Length()) if key not in self.records: self.recordsLength.change(1) self.records.insert(key,value) security.declareProtected('Python Record Modification', 'add') def add(self, key, value): "this this key and value into the OOBTree" if self.records is None: self.records = OOBTree() if self.recordsLength is None: self.setObject('recordsLength' ,BTrees.Length.Length()) if key not in self.records: self.recordsLength.change(1) self.records[key] = value security.declareProtected('Python Record Access', 'items') def items(self, min=None, max=None): "return the items in this OOBTree" if self.records is None: return [] return self.records.items(min, max) security.declareProtected('Python Record Access', 'values') def values(self, min=None, max=None): "return the values of this OOBTree" if self.records is None: return [] return self.records.values(min, max) security.declareProtected('Python Record Modification', 'update') def update(self, collection): "update our OOBTree with the data in collection" if self.records is None: self.records = OOBTree() if self.recordsLength is None: self.setObject('recordsLength' ,BTrees.Length.Length()) records = self.records change = self.recordsLength.change for key,value in collection.items(): if key not in records: change(1) records[key] = value security.declareProtected('Python Record Access', 'keys') def keys(self, min=None, max=None): "return the keys of this OOBTree" if self.records is None: return [] return self.records.keys(min,max) security.declareProtected('Python Record Modification', '__delitem__') def __delitem__(self, key): "delete this key from the OOBTree" if self.records is not None: del self.records[key] self.recordsLength.change(-1) security.declareProtected('Python Record Modification', 'remove') def remove(self, key): "delete this key from the OOBTree" if self.records is not None: del self.records[key] self.recordsLength.change(-1) security.declareProtected('Python Record Modification', '__setitem__') def __setitem__(self, key, value): "set this key and value in the OOBTree" if self.records is None: self.records = OOBTree() self.records[key] = value security.declareProtected('Python Record Access', '__getitem__') def __getitem__(self, index): "get this item from the OOBTree" if self.records is not None: return self.records[index] raise KeyError, index security.declareProtected('Python Record Access', 'get') def get(self, key, default=None): "get this item from the OOBTree" if self.records is not None: return self.records.get(key,default) return default security.declareProtected('Python Record Access', 'has_key') def has_key(self, key): "see if we have this key in the OOBTree" if self.records is not None: return self.records.has_key(key) return False security.declareProtected('Python Record Modification', 'clear') def clear(self): "clear the OOBTree" self.setObject('records', None) self.setObject('recordsLength', None) security.declarePrivate("PrincipiaSearchSource") def PrincipiaSearchSource(self): "This is the basic search function" return '' security.declarePrivate('classUpgrader') def classUpgrader(self): "upgrade this class" self.createBTreeLength() security.declarePrivate('createBTreeLength') def createBTreeLength(self): "remove Filters that are not being used" if self.records is not None: length = BTrees.Length.Length() length.set(len(self.records)) self.setObject('recordsLength', length) createBTreeLength = utility.upgradeLimit(createBTreeLength, 165)
class ZODBMutablePropertyProvider(BasePlugin): """Storage for mutable properties in the ZODB for users/groups. API sounds like it's only for users, but groups work as well. """ meta_type = 'ZODB Mutable Property Provider' implements(IPropertiesPlugin, IUserEnumerationPlugin, IMutablePropertiesPlugin) security = ClassSecurityInfo() def __init__(self, id, title='', schema=None, **kw): """Create in-ZODB mutable property provider. Provide a schema either as a list of (name,type,value) tuples in the 'schema' parameter or as a series of keyword parameters 'name=value'. Types will be guessed in this case. The 'value' is meant as the default value, and will be used unless the user provides data. If no schema is provided by constructor, the properties of the portal_memberdata object will be used. Types available: string, text, boolean, int, long, float, lines, date """ self.id = id self.title = title self._storage = OOBTree() # calculate schema and default values defaultvalues = {} if not schema and not kw: schema = () elif not schema and kw: schema = _guessSchema(kw) defaultvalues = kw else: valuetuples = [(name, value) for name, type, value in schema] schema = [(name, type) for name, type, value in schema] for name, value in valuetuples: defaultvalues[name] = value self._schema = tuple(schema) self._defaultvalues = defaultvalues def _getSchema(self, isgroup=None): # this could probably stand to be cached datatool = isgroup and "portal_groupdata" or "portal_memberdata" schema = self._schema if not schema: # if no schema is provided, use portal_memberdata properties schema = () mdtool = getToolByName(self, datatool, None) # Don't fail badly if tool is not available. if mdtool is not None: mdschema = mdtool.propertyMap() schema = [(elt['id'], elt['type']) for elt in mdschema] return schema def _getDefaultValues(self, isgroup=None): """Returns a dictionary mapping of property names to default values. Defaults to portal_*data tool if necessary. """ datatool = isgroup and "portal_groupdata" or "portal_memberdata" defaultvalues = self._defaultvalues if not self._schema: # if no schema is provided, use portal_*data properties defaultvalues = {} mdtool = getToolByName(self, datatool, None) # Don't fail badly if tool is not available. if mdtool is not None: # we rely on propertyMap and propertyItems mapping mdvalues = mdtool.propertyItems() for name, value in mdvalues: # For selection types the default value is the name of a # method which returns the possible values. There is no way # to set a default value for those types. ptype = mdtool.getPropertyType(name) if ptype == "selection": defaultvalues[name] = "" elif ptype == "multiple selection": defaultvalues[name] = [] else: defaultvalues[name] = value # ALERT! if someone gives their *_data tool a title, and want a # title as a property of the user/group (and groups do by # default) we don't want them all to have this title, since # a title is used in the UI if it exists if defaultvalues.get("title"): defaultvalues["title"] = "" return defaultvalues security.declarePrivate('getPropertiesForUser') def getPropertiesForUser(self, user, request=None): """Get property values for a user or group. Returns a dictionary of values or a PropertySheet. This implementation will always return a MutablePropertySheet. NOTE: Must always return something, or else the property sheet won't get created and this will screw up portal_memberdata. """ isGroup = getattr(user, 'isGroup', lambda: None)() data = self._storage.get(user.getId()) defaults = self._getDefaultValues(isGroup) # provide default values where missing if not data: data = {} for key, val in defaults.items(): if not key in data: data[key] = val return MutablePropertySheet(self.id, schema=self._getSchema(isGroup), **data) security.declarePrivate('setPropertiesForUser') def setPropertiesForUser(self, user, propertysheet): """Set the properties of a user or group based on the contents of a property sheet. """ isGroup = getattr(user, 'isGroup', lambda: None)() properties = dict(propertysheet.propertyItems()) for name, property_type in self._getSchema(isGroup) or (): if (name in properties and not validateValue(property_type, properties[name])): raise ValueError('Invalid value: %s does not conform ' 'to %s' % (name, property_type)) allowed_prop_keys = [pn for pn, pt in self._getSchema(isGroup) or ()] if allowed_prop_keys: prop_names = set(properties.keys()) - set(allowed_prop_keys) if prop_names: raise ValueError('Unknown Properties: %r' % prop_names) userid = user.getId() userprops = self._storage.get(userid) properties.update({'isGroup': isGroup}) if userprops is not None: userprops.update(properties) # notify persistence machinery of change self._storage[userid] = self._storage[userid] else: self._storage.insert(user.getId(), properties) security.declarePrivate('deleteUser') def deleteUser(self, user_id): """Delete all user properties """ # Do nothing if an unknown user_id is given try: del self._storage[user_id] except KeyError: pass security.declarePrivate('testMemberData') def testMemberData(self, memberdata, criteria, exact_match=False): """Test if a memberdata matches the search criteria. """ for (key, value) in criteria.items(): testvalue = memberdata.get(key, None) if testvalue is None: return False if isStringType(testvalue): testvalue = safe_unicode(testvalue.lower()) if isStringType(value): value = safe_unicode(value.lower()) if exact_match: if value != testvalue: return False else: try: if value not in testvalue: return False except TypeError: # Fall back to exact match if we can check for # sub-component if value != testvalue: return False return True security.declarePrivate('enumerateUsers') def enumerateUsers(self, id=None, login=None, exact_match=False, **kw): """ See IUserEnumerationPlugin. """ plugin_id = self.getId() # This plugin can't search for a user by id or login, because there is # no such keys in the storage (data dict in the comprehensive list) # If kw is empty or not, we continue the search. if id is not None or login is not None: return () criteria = copy.copy(kw) users = [(user, data) for (user, data) in self._storage.items() if self.testMemberData(data, criteria, exact_match) and not data.get('isGroup', False)] user_info = [{'id': self.prefix + user_id, 'login': user_id, 'title': data.get('fullname', user_id), 'description': data.get('fullname', user_id), 'email': data.get('email', ''), 'pluginid': plugin_id} for (user_id, data) in users] return tuple(user_info) def updateUser(self, user_id, login_name): """ Update the login name of the user with id user_id. This is a new part of the IUserEnumerationPlugin interface, but not interesting for us. """ pass def updateEveryLoginName(self, quit_on_first_error=True): """Update login names of all users to their canonical value. This is a new part of the IUserEnumerationPlugin interface, but not interesting for us. """ pass
class Subscriptions(SimpleItem): security = ClassSecurityInfo() title = "Meeting registrations" def __init__(self, id): """ """ super(SimpleItem, self).__init__(id) self.id = id self._signups = OOBTree() self._account_subscriptions = OOBTree() security.declarePublic('getMeeting') def getMeeting(self): return self.aq_parent.aq_parent def _validate_signup(self, form): """ """ formdata = {} formerrors = {} keys = ('first_name', 'last_name', 'email', 'organization', 'phone') formdata = dict((key, form.get(key, '')) for key in keys) for key in formdata: if formdata[key] == '': formerrors[key] = 'This field is mandatory' if formerrors == {}: if formdata['email'].count('@') != 1: formerrors['email'] = ('An email address must contain ' 'a single @') if formerrors == {}: formerrors = None return formdata, formerrors def _add_signup(self, formdata): """ """ meeting = self.getMeeting() key = random_key() name = formdata['first_name'] + ' ' + formdata['last_name'] email = formdata['email'] organization = formdata['organization'] phone = formdata['phone'] signup = SignUp(key, name, email, organization, phone) self._signups.insert(key, signup) if meeting.auto_register: self._accept_signup(key) email_sender = self.getMeeting().getEmailSender() email_sender.send_signup_email(signup) if self.REQUEST.AUTHENTICATED_USER.getUserName() == 'Anonymous User': self.REQUEST.SESSION['nymt-current-key'] = key security.declareProtected(view, 'signup') def signup(self, REQUEST): """ """ meeting = self.getMeeting() if not meeting.allow_register: return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscription_not_allowed') if REQUEST.get('add_users'): return self.subscribe_accounts(REQUEST) if REQUEST.get('add_signup'): formdata, formerrors = self._validate_signup(REQUEST.form) # check Captcha/reCaptcha if not (self.checkPermissionSkipCaptcha() or REQUEST.SESSION.get('captcha_passed')): recaptcha_response = REQUEST.form.get('g-recaptcha-response', '') captcha_validator = self.validateCaptcha(recaptcha_response, REQUEST) if captcha_validator: if formerrors is None: formerrors = {} formerrors['captcha'] = captcha_validator else: REQUEST.SESSION['captcha_passed'] = True if formerrors is not None: return self.getFormsTool().getContent( {'here': self, 'formdata': formdata, 'formerrors': formerrors}, 'naaya.content.meeting.subscription_signup') else: self._add_signup(formdata) if self.getMeeting().survey_required: REQUEST.RESPONSE.redirect( self.getMeeting().absolute_url()) else: REQUEST.RESPONSE.redirect(self.absolute_url() + '/signup_successful') # check Captcha/reCaptcha also for searching users captcha_validator = None if (REQUEST.get('search_user') or REQUEST.get('search_user_with_role')): if not (self.checkPermissionSkipCaptcha() or REQUEST.SESSION.get('captcha_passed')): recaptcha_response = REQUEST.form.get('g-recaptcha-response', '') captcha_validator = self.validateCaptcha(recaptcha_response, REQUEST) if not captcha_validator: REQUEST.SESSION['captcha_passed'] = True return self.getFormsTool().getContent( {'here': self, 'captcha_errors': captcha_validator}, 'naaya.content.meeting.subscription_signup') security.declareProtected(view, 'signup_successful') def signup_successful(self, REQUEST): """ """ return self.getFormsTool().getContent( {'here': self}, 'naaya.content.meeting.subscription_signup_successful') security.declareProtected(view, 'subscribe') def subscribe(self, REQUEST): """ """ meeting = self.getMeeting() if not meeting.allow_register: return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscription_not_allowed') return self.getFormsTool().getContent( {'here': self}, 'naaya.content.meeting.subscription_subscribe') def getSignups(self): """ """ if not self.checkPermissionParticipateInMeeting(): raise Unauthorized return self._signups.itervalues() security.declareProtected(PERMISSION_ADMIN_MEETING, 'getSignup') def getSignup(self, key): """ """ return self._signups.get(key, None) def index_html(self, REQUEST): """ """ if not self.checkPermissionParticipateInMeeting(): raise Unauthorized return self.getFormsTool().getContent( {'here': self}, 'naaya.content.meeting.subscription_index') def _accept_signup(self, key): """ """ meeting = self.getMeeting() meeting.getParticipants()._set_attendee(key, PARTICIPANT_ROLE) signup = self._signups[key] signup.accepted = 'accepted' email_sender = meeting.getEmailSender() email_sender.send_signup_accepted_email(signup) def _reject_signup(self, key): """ """ meeting = self.getMeeting() signup = self._signups[key] signup.accepted = 'rejected' participants = meeting.getParticipants() # delete the 'reimbursed' status participants.setAttendeeInfo([key], 'reimbursed', False) if key in participants._get_attendees(): participants._del_attendee(key) email_sender = meeting.getEmailSender() email_sender.send_signup_rejected_email(signup) def _delete_signup(self, key): """ """ meeting = self.getMeeting() signup = self._signups.pop(key, None) if signup is None: return participants = meeting.getParticipants() if key in participants._get_attendees(): participants._del_attendee(key) email_sender = meeting.getEmailSender() email_sender.send_signup_rejected_email(signup) def _is_signup(self, key): """ """ return key in self._signups def _is_accepted_signup(self, key): """ """ return self._is_signup(key) and \ self._signups[key].accepted == 'accepted' def _is_pending_signup(self, key): """ """ return self._is_signup(key) and \ self._signups[key].accepted == 'new' def manageSubscriptions(self, REQUEST): """ """ if not (self.checkPermissionAdminMeeting() or self.nfp_for_country()): raise Unauthorized uids = REQUEST.form.get('uids', []) assert isinstance(uids, list) for uid in uids: if 'accept' in REQUEST.form: if self._is_signup(uid): self._accept_signup(uid) else: self._accept_account_subscription(uid) elif 'reject' in REQUEST.form: if self._is_signup(uid): self._reject_signup(uid) else: self._reject_account_subscription(uid) elif 'delete' in REQUEST.form: if not self.checkPermissionAdminMeeting(): raise Unauthorized if self._is_signup(uid): self._delete_signup(uid) else: self._delete_account_subscription(uid) if 'set_representative' in REQUEST.form: self.setRepresentatives(REQUEST) elif 'unset_representative' in REQUEST.form: self.setRepresentatives(REQUEST, remove=True) elif 'set_reimbursement' in REQUEST.form: self.setReimbursement(REQUEST) elif 'unset_reimbursement' in REQUEST.form: self.setReimbursement(REQUEST, remove=True) elif 'save_changes' in REQUEST.form: self.save_changes(REQUEST) return REQUEST.RESPONSE.redirect(self.absolute_url()) security.declarePublic('welcome') def welcome(self, REQUEST, came_from=None): """ """ if 'logout' in REQUEST.form: REQUEST.SESSION['nymt-current-key'] = None return REQUEST.RESPONSE.redirect(self.getMeeting().absolute_url()) key = REQUEST.get('key', None) signup = self.getSignup(key) if self._is_signup(key): REQUEST.SESSION['nymt-current-key'] = key if came_from: return REQUEST.RESPONSE.redirect(came_from) else: return REQUEST.RESPONSE.redirect( self.getMeeting().absolute_url()) return self.getFormsTool().getContent( {'here': self, 'signup': signup}, 'naaya.content.meeting.subscription_welcome') def _add_account_subscription(self, uid, accept=False): """ """ # If the subscription already exists or the user is alread signed up # skip the whole thing if self._is_account_subscription(uid): return key = uid.replace('signup:', '') if self._is_signup(key): return site = self.getSite() meeting = self.getMeeting() name = getUserFullName(site, uid) # If for any reason we still don't have a name, at least use UID if not name: name = uid email = getUserEmail(site, uid) organization = getUserOrganization(site, uid) if not organization: organization = self.get_survey_answer(uid, 'w_organization') if not organization: organization = self.get_survey_answer(uid, 'w_organisation') phone = getUserPhoneNumber(site, uid) if not phone: phone = self.get_survey_answer(uid, 'w_telephone') if not phone: phone = self.get_survey_answer(uid, 'w_phone') account_subscription = AccountSubscription(uid, name, email, organization, phone) self._account_subscriptions.insert(uid, account_subscription) if meeting.auto_register or accept: self._accept_account_subscription(uid) email_sender = self.getMeeting().getEmailSender() email_sender.send_account_subscription_email(account_subscription) security.declareProtected(PERMISSION_ADMIN_MEETING, 'update_account_subscription') def update_account_subscription(self, uid): """ """ site = self.getSite() name = getUserFullName(site, uid) email = getUserEmail(site, uid) organization = getUserOrganization(site, uid) phone = getUserPhoneNumber(site, uid) account_subscription = AccountSubscription(uid, name, email, organization, phone) self._account_subscriptions.update({uid: account_subscription}) security.declareProtected(view, 'subscribe_accounts') def subscribe_accounts(self, REQUEST): """ """ meeting = self.getMeeting() if not meeting.allow_register: return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscription_not_allowed') # check Captcha/reCaptcha also for searching users if not (self.checkPermissionSkipCaptcha() or REQUEST.SESSION.get('captcha_passed')): recaptcha_response = REQUEST.form.get('g-recaptcha-response', '') captcha_validator = self.validateCaptcha(recaptcha_response, REQUEST) if captcha_validator: return self.getFormsTool().getContent( {'here': self, 'captcha_errors': captcha_validator}, 'naaya.content.meeting.subscription_signup') else: REQUEST.SESSION['captcha_passed'] = True uids = REQUEST.form.get('uids', []) assert isinstance(uids, list) for uid in uids: self._add_account_subscription(uid) return REQUEST.RESPONSE.redirect( self.absolute_url() + '/subscribe_account_successful?uids='+','.join(uids)) security.declareProtected(view, 'subscribe_my_account') def subscribe_my_account(self, REQUEST): """ """ meeting = self.getMeeting() if not meeting.allow_register: return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscription_not_allowed') self._add_account_subscription(REQUEST.AUTHENTICATED_USER.getId()) if self.survey_required: site = self.getSite() path = str(self.survey_pointer) survey_ob = site.unrestrictedTraverse(path, None) if survey_ob is not None and \ survey_ob.meta_type == 'Naaya Mega Survey': answers = survey_ob.getAnswers() respondents = [a.respondent for a in answers] current_user = REQUEST.AUTHENTICATED_USER.getUserName() if current_user not in respondents: self.setSessionInfoTrans( 'Registration successfully sent for approval. ' 'Please also respond to the following questionaire.') return REQUEST.RESPONSE.redirect( '%s/%s' % (self.getSite().absolute_url(), self.survey_pointer)) return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscribe_account_successful') security.declareProtected(view, 'subscribe_account_successful') def subscribe_account_successful(self, REQUEST): """ """ return self.getFormsTool().getContent( {'here': self}, 'naaya.content.meeting.subscription_subscribe_successful') def getAccountSubscriptions(self): """ """ if not self.checkPermissionParticipateInMeeting(): raise Unauthorized return self._account_subscriptions.itervalues() def getSubscriptions(self): """ """ if not self.checkPermissionParticipateInMeeting(): raise Unauthorized subscriptions = (list(self._signups.itervalues()) + list(self._account_subscriptions.itervalues())) statuses = {'new': 0, 'accepted': 1, 'rejected': 2 } return sorted(subscriptions, key=lambda x: statuses.get(x.accepted)) security.declareProtected(PERMISSION_ADMIN_MEETING, 'getAccountSubscription') def getAccountSubscription(self, uid): """ """ return self._account_subscriptions.get(uid, None) def _is_account_subscription(self, uid): """ """ return uid in self._account_subscriptions and \ self._account_subscriptions[uid].accepted == 'accepted' def _accept_account_subscription(self, uid): """ """ meeting = self.getMeeting() meeting.getParticipants()._set_attendee(uid, PARTICIPANT_ROLE) account_subscription = self._account_subscriptions[uid] account_subscription.accepted = 'accepted' email_sender = meeting.getEmailSender() email_sender.send_account_subscription_accepted_email( account_subscription) def _reject_account_subscription(self, uid): """ """ meeting = self.getMeeting() account_subscription = self._account_subscriptions[uid] account_subscription.accepted = 'rejected' participants = meeting.getParticipants() # remove the 'reimbursed' status participants.setAttendeeInfo([uid], 'reimbursed', False) if uid in participants._get_attendees(): participants._del_attendee(uid) email_sender = meeting.getEmailSender() email_sender.send_account_subscription_rejected_email( account_subscription) def _delete_account_subscription(self, uid): """ """ meeting = self.getMeeting() account_subscription = self._account_subscriptions.pop(uid, None) if account_subscription is None: return participants = meeting.getParticipants() if uid in participants._get_attendees(): participants._del_attendee(uid) email_sender = meeting.getEmailSender() email_sender.send_account_subscription_rejected_email( account_subscription) security.declareProtected(view, 'subscription_not_allowed') def subscription_not_allowed(self, REQUEST): """ """ return self.getFormsTool().getContent( {'here': self}, 'naaya.content.meeting.subscription_not_allowed')
class Subscriptions(SimpleItem): security = ClassSecurityInfo() title = "Meeting registrations" def __init__(self, id): """ """ super(SimpleItem, self).__init__(id) self.id = id self._signups = OOBTree() self._account_subscriptions = OOBTree() security.declarePublic('getMeeting') def getMeeting(self): return self.aq_parent.aq_parent def _validate_signup(self, form): """ """ formdata = {} formerrors = {} keys = ('first_name', 'last_name', 'email', 'organization', 'phone') formdata = dict((key, form.get(key, '')) for key in keys) for key in formdata: if formdata[key] == '': formerrors[key] = 'This field is mandatory' if formerrors == {}: if formdata['email'].count('@') != 1: formerrors['email'] = ('An email address must contain ' 'a single @') if formerrors == {}: formerrors = None return formdata, formerrors def _add_signup(self, formdata): """ """ meeting = self.getMeeting() key = random_key() name = formdata['first_name'] + ' ' + formdata['last_name'] email = formdata['email'] organization = formdata['organization'] phone = formdata['phone'] signup = SignUp(key, name, email, organization, phone) self._signups.insert(key, signup) if meeting.auto_register: self._accept_signup(key) email_sender = self.getMeeting().getEmailSender() email_sender.send_signup_email(signup) if self.REQUEST.AUTHENTICATED_USER.getUserName() == 'Anonymous User': self.REQUEST.SESSION['nymt-current-key'] = key security.declareProtected(view, 'signup') def signup(self, REQUEST): """ """ meeting = self.getMeeting() if not meeting.allow_register: return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscription_not_allowed') if REQUEST.get('add_users'): return self.subscribe_accounts(REQUEST) if REQUEST.get('add_signup'): formdata, formerrors = self._validate_signup(REQUEST.form) # check Captcha/reCaptcha if not (self.checkPermissionSkipCaptcha() or REQUEST.SESSION.get('captcha_passed')): recaptcha_response = REQUEST.form.get('g-recaptcha-response', '') captcha_validator = self.validateCaptcha( recaptcha_response, REQUEST) if captcha_validator: if formerrors is None: formerrors = {} formerrors['captcha'] = captcha_validator else: REQUEST.SESSION['captcha_passed'] = True if formerrors is not None: return self.getFormsTool().getContent( { 'here': self, 'formdata': formdata, 'formerrors': formerrors }, 'naaya.content.meeting.subscription_signup') else: self._add_signup(formdata) if self.getMeeting().survey_required: REQUEST.RESPONSE.redirect(self.getMeeting().absolute_url()) else: REQUEST.RESPONSE.redirect(self.absolute_url() + '/signup_successful') # check Captcha/reCaptcha also for searching users captcha_validator = None if (REQUEST.get('search_user') or REQUEST.get('search_user_with_role')): if not (self.checkPermissionSkipCaptcha() or REQUEST.SESSION.get('captcha_passed')): recaptcha_response = REQUEST.form.get('g-recaptcha-response', '') captcha_validator = self.validateCaptcha( recaptcha_response, REQUEST) if not captcha_validator: REQUEST.SESSION['captcha_passed'] = True return self.getFormsTool().getContent( { 'here': self, 'captcha_errors': captcha_validator }, 'naaya.content.meeting.subscription_signup') security.declareProtected(view, 'signup_successful') def signup_successful(self, REQUEST): """ """ return self.getFormsTool().getContent( {'here': self}, 'naaya.content.meeting.subscription_signup_successful') security.declareProtected(view, 'subscribe') def subscribe(self, REQUEST): """ """ meeting = self.getMeeting() if not meeting.allow_register: return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscription_not_allowed') return self.getFormsTool().getContent( {'here': self}, 'naaya.content.meeting.subscription_subscribe') def getSignups(self): """ """ if not self.checkPermissionParticipateInMeeting(): raise Unauthorized return self._signups.itervalues() security.declareProtected(PERMISSION_ADMIN_MEETING, 'getSignup') def getSignup(self, key): """ """ return self._signups.get(key, None) def index_html(self, REQUEST): """ """ if not self.checkPermissionParticipateInMeeting(): raise Unauthorized return self.getFormsTool().getContent( {'here': self}, 'naaya.content.meeting.subscription_index') def _accept_signup(self, key): """ """ meeting = self.getMeeting() meeting.getParticipants()._set_attendee(key, PARTICIPANT_ROLE) signup = self._signups[key] signup.accepted = 'accepted' email_sender = meeting.getEmailSender() email_sender.send_signup_accepted_email(signup) def _reject_signup(self, key): """ """ meeting = self.getMeeting() signup = self._signups[key] signup.accepted = 'rejected' participants = meeting.getParticipants() # delete the 'reimbursed' status participants.setAttendeeInfo([key], 'reimbursed', False) if key in participants._get_attendees(): participants._del_attendee(key) email_sender = meeting.getEmailSender() email_sender.send_signup_rejected_email(signup) def _delete_signup(self, key): """ """ meeting = self.getMeeting() signup = self._signups.pop(key, None) if signup is None: return participants = meeting.getParticipants() if key in participants._get_attendees(): participants._del_attendee(key) email_sender = meeting.getEmailSender() email_sender.send_signup_rejected_email(signup) def _is_signup(self, key): """ """ return key in self._signups and \ self._signups[key].accepted == 'accepted' def _is_pending_signup(self, key): """ """ return key in self._signups and \ self._signups[key].accepted == 'new' def manageSubscriptions(self, REQUEST): """ """ if not (self.checkPermissionAdminMeeting() or self.nfp_for_country()): raise Unauthorized uids = REQUEST.form.get('uids', []) assert isinstance(uids, list) for uid in uids: if 'accept' in REQUEST.form: if self._is_signup(uid): self._accept_signup(uid) else: self._accept_account_subscription(uid) elif 'reject' in REQUEST.form: if self._is_signup(uid): self._reject_signup(uid) else: self._reject_account_subscription(uid) elif 'delete' in REQUEST.form: if not self.checkPermissionAdminMeeting(): raise Unauthorized if self._is_signup(uid): self._delete_signup(uid) else: self._delete_account_subscription(uid) if 'set_representative' in REQUEST.form: self.setRepresentatives(REQUEST) elif 'unset_representative' in REQUEST.form: self.setRepresentatives(REQUEST, remove=True) elif 'set_reimbursement' in REQUEST.form: self.setReimbursement(REQUEST) elif 'unset_reimbursement' in REQUEST.form: self.setReimbursement(REQUEST, remove=True) elif 'save_changes' in REQUEST.form: self.save_changes(REQUEST) return REQUEST.RESPONSE.redirect(self.absolute_url()) security.declarePublic('welcome') def welcome(self, REQUEST, came_from=None): """ """ if 'logout' in REQUEST.form: REQUEST.SESSION['nymt-current-key'] = None return REQUEST.RESPONSE.redirect(self.getMeeting().absolute_url()) key = REQUEST.get('key', None) signup = self.getSignup(key) if self._is_signup(key) or self._is_pending_signup(key): REQUEST.SESSION['nymt-current-key'] = key if came_from: return REQUEST.RESPONSE.redirect(came_from) else: return REQUEST.RESPONSE.redirect( self.getMeeting().absolute_url()) return self.getFormsTool().getContent({ 'here': self, 'signup': signup }, 'naaya.content.meeting.subscription_welcome') def _add_account_subscription(self, uid, accept=False): """ """ # If the subscription already exists or the user is alread signed up # skip the whole thing if self._is_account_subscription(uid): return key = uid.replace('signup:', '') if self._is_signup(key): return site = self.getSite() meeting = self.getMeeting() name = getUserFullName(site, uid) # If for any reason we still don't have a name, at least use UID if not name: name = uid email = getUserEmail(site, uid) organization = getUserOrganization(site, uid) if not organization: organization = self.get_survey_answer(uid, 'w_organization') if not organization: organization = self.get_survey_answer(uid, 'w_organisation') phone = getUserPhoneNumber(site, uid) if not phone: phone = self.get_survey_answer(uid, 'w_telephone') if not phone: phone = self.get_survey_answer(uid, 'w_phone') account_subscription = AccountSubscription(uid, name, email, organization, phone) self._account_subscriptions.insert(uid, account_subscription) if meeting.auto_register or accept: self._accept_account_subscription(uid) email_sender = self.getMeeting().getEmailSender() email_sender.send_account_subscription_email(account_subscription) security.declareProtected(PERMISSION_ADMIN_MEETING, 'update_account_subscription') def update_account_subscription(self, uid): """ """ site = self.getSite() name = getUserFullName(site, uid) email = getUserEmail(site, uid) organization = getUserOrganization(site, uid) phone = getUserPhoneNumber(site, uid) account_subscription = AccountSubscription(uid, name, email, organization, phone) self._account_subscriptions.update({uid: account_subscription}) security.declareProtected(view, 'subscribe_accounts') def subscribe_accounts(self, REQUEST): """ """ meeting = self.getMeeting() if not meeting.allow_register: return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscription_not_allowed') # check Captcha/reCaptcha also for searching users if not (self.checkPermissionSkipCaptcha() or REQUEST.SESSION.get('captcha_passed')): recaptcha_response = REQUEST.form.get('g-recaptcha-response', '') captcha_validator = self.validateCaptcha(recaptcha_response, REQUEST) if captcha_validator: return self.getFormsTool().getContent( { 'here': self, 'captcha_errors': captcha_validator }, 'naaya.content.meeting.subscription_signup') else: REQUEST.SESSION['captcha_passed'] = True uids = REQUEST.form.get('uids', []) assert isinstance(uids, list) for uid in uids: self._add_account_subscription(uid) return REQUEST.RESPONSE.redirect( self.absolute_url() + '/subscribe_account_successful?uids=' + ','.join(uids)) security.declareProtected(view, 'subscribe_my_account') def subscribe_my_account(self, REQUEST): """ """ meeting = self.getMeeting() if not meeting.allow_register: return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscription_not_allowed') self._add_account_subscription(REQUEST.AUTHENTICATED_USER.getId()) if self.survey_required: site = self.getSite() path = str(self.survey_pointer) survey_ob = site.unrestrictedTraverse(path, None) if survey_ob is not None and \ survey_ob.meta_type == 'Naaya Mega Survey': answers = survey_ob.getAnswers() respondents = [a.respondent for a in answers] current_user = REQUEST.AUTHENTICATED_USER.getUserName() if current_user not in respondents: self.setSessionInfoTrans( 'Registration successfully sent for approval. ' 'Please also respond to the following questionaire.') return REQUEST.RESPONSE.redirect( '%s/%s' % (self.getSite().absolute_url(), self.survey_pointer)) return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscribe_account_successful') security.declareProtected(view, 'subscribe_account_successful') def subscribe_account_successful(self, REQUEST): """ """ return self.getFormsTool().getContent( {'here': self}, 'naaya.content.meeting.subscription_subscribe_successful') def getAccountSubscriptions(self): """ """ if not self.checkPermissionParticipateInMeeting(): raise Unauthorized return self._account_subscriptions.itervalues() def getSubscriptions(self): """ """ if not self.checkPermissionParticipateInMeeting(): raise Unauthorized subscriptions = (list(self._signups.itervalues()) + list(self._account_subscriptions.itervalues())) statuses = {'new': 0, 'accepted': 1, 'rejected': 2} return sorted(subscriptions, key=lambda x: statuses.get(x.accepted)) security.declareProtected(PERMISSION_ADMIN_MEETING, 'getAccountSubscription') def getAccountSubscription(self, uid): """ """ return self._account_subscriptions.get(uid, None) def _is_account_subscription(self, uid): """ """ return uid in self._account_subscriptions and \ self._account_subscriptions[uid].accepted == 'accepted' def _accept_account_subscription(self, uid): """ """ meeting = self.getMeeting() meeting.getParticipants()._set_attendee(uid, PARTICIPANT_ROLE) account_subscription = self._account_subscriptions[uid] account_subscription.accepted = 'accepted' email_sender = meeting.getEmailSender() email_sender.send_account_subscription_accepted_email( account_subscription) def _reject_account_subscription(self, uid): """ """ meeting = self.getMeeting() account_subscription = self._account_subscriptions[uid] account_subscription.accepted = 'rejected' participants = meeting.getParticipants() # remove the 'reimbursed' status participants.setAttendeeInfo([uid], 'reimbursed', False) if uid in participants._get_attendees(): participants._del_attendee(uid) email_sender = meeting.getEmailSender() email_sender.send_account_subscription_rejected_email( account_subscription) def _delete_account_subscription(self, uid): """ """ meeting = self.getMeeting() account_subscription = self._account_subscriptions.pop(uid, None) if account_subscription is None: return participants = meeting.getParticipants() if uid in participants._get_attendees(): participants._del_attendee(uid) email_sender = meeting.getEmailSender() email_sender.send_account_subscription_rejected_email( account_subscription) security.declareProtected(view, 'subscription_not_allowed') def subscription_not_allowed(self, REQUEST): """ """ return self.getFormsTool().getContent( {'here': self}, 'naaya.content.meeting.subscription_not_allowed')
list_rid.append(list2[i]) t.insert(list1[i], list_rid) list_rid = [] ''' ''' else: #print("in else list_rid: ", list_rid) t.insert(list1[i], list2[i]) list_rid = [] ''' for i in range(len(list1)): list10 = [] list10.append(list2[i]) if t.insert(list1[i], list10) == 0: value = t.pop(list1[i]) print("in the loop: ", value) # print("len: ", len(value[0])) ''' if len(str(value)) == 1: list_rid.append(value) elif len(str(value)) > 1: for j in range(len(value)): list_rid.append(value[j]) ''' value.append(list2[i]) #list_rid.append(list2[i]) t.insert(list1[i], value) # list_rid = []
class Search: def __init__(self, output_path): self._output = output_path self._alphabet = 'abcdefghijklmnñopqrstuvwxyz' self._btree = OOBTree() self._reverse_btree = OOBTree() def search_in_ii(self, terms): term_dict = pickle.load(open(self._output + '/' + 'term.dict', 'rb')) document_dict = pickle.load( open(self._output + '/' + 'document.dict', 'rb')) ii_dict = pickle.load(open(self._output + '/' + 'ii.dict', 'rb')) # Invierto document_dict para trabajar con los ID id_to_document_list = {y: x for x, y in document_dict.items()} id_to_term = {y: x for x, y in term_dict.items()} # Inserto los términos en el árbol for key in ii_dict: self._btree.insert(id_to_term.get(key), ii_dict.get(key)) self.reverse_btree() # Ya no se necesita esto ii_dict = None term_dict = None document_dict = None results = {} try: postings = CompressedPostings(self._output + '/' + 'index.cii') except FileNotFoundError: postings = UncompressedPostings(self._output + '/' + 'index.ii') for term in terms: metadata_list = self.wildcard_search(term) postings_list = [] if metadata_list: for metadata in metadata_list: postings_list += postings.retrieve_postings_list( metadata[0], metadata[2]) results[term] = [ id_to_document_list.get(x) for x in postings_list ] else: results[term] = None postings.close_postings_file() return results def reverse_btree(self): for key in self._btree: self._reverse_btree.insert(key[::-1], self._btree[key]) def wildcard_search(self, word): if word.find("*") == -1: # No wildcard try: stemmer = Stemmer.Stemmer('spanish') stemmed_word = stemmer.stemWord(word) return list([self._btree[stemmed_word]]) except KeyError: return None elif word[-1] == "*": return list( self._btree.values(min=word[:-1] + self._alphabet[0], max=word[:-1] + self._alphabet[26])) elif word[0] == "*": print("Desde %s hasta %s" % (word[::-1][:-1] + self._alphabet[0], word[::-1][:-1] + self._alphabet[26])) return list( self._reverse_btree.values( min=word[::-1][:-1] + self._alphabet[0], max=word[::-1][:-1] + self._alphabet[26])) else: return None
class Subscriptions(SimpleItem): security = ClassSecurityInfo() title = "Meeting registrations" def __init__(self, id): """ """ super(SimpleItem, self).__init__(id) self.id = id self._signups = OOBTree() self._account_subscriptions = OOBTree() security.declarePublic('getMeeting') def getMeeting(self): return self.aq_parent.aq_parent def _validate_signup(self, form): """ """ formdata = {} formerrors = {} keys = ('first_name', 'last_name', 'email', 'organization', 'phone') formdata = dict( (key, form.get(key, '')) for key in keys ) for key in formdata: if formdata[key] == '': formerrors[key] = 'This field is mandatory' if formerrors == {}: if formdata['email'].count('@') != 1: formerrors['email'] = 'An email address must contain a single @' if formerrors == {}: formerrors = None return formdata, formerrors def _add_signup(self, formdata): """ """ meeting = self.getMeeting() key = random_key() name = formdata['first_name'] + ' ' + formdata['last_name'] email = formdata['email'] organization = formdata['organization'] phone = formdata['phone'] signup = SignUp(key, name, email, organization, phone) self._signups.insert(key, signup) if meeting.auto_register: self._accept_signup(key) email_sender = self.getMeeting().getEmailSender() email_sender.send_signup_email(signup) security.declareProtected(view, 'signup') def signup(self, REQUEST): """ """ meeting = self.getMeeting() if not meeting.allow_register: return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscription_not_allowed') if REQUEST.REQUEST_METHOD == 'GET': return self.getFormsTool().getContent({'here': self}, 'naaya.content.meeting.subscription_signup') if REQUEST.REQUEST_METHOD == 'POST': formdata, formerrors = self._validate_signup(REQUEST.form) #check Captcha/reCaptcha if not self.checkPermissionSkipCaptcha(): contact_word = REQUEST.form.get('contact_word', '') captcha_validator = self.validateCaptcha(contact_word, REQUEST) if captcha_validator: if formerrors is None: formerrors = {} formerrors['captcha'] = captcha_validator if formerrors is not None: return self.getFormsTool().getContent({'here': self, 'formdata': formdata, 'formerrors': formerrors}, 'naaya.content.meeting.subscription_signup') else: self._add_signup(formdata) REQUEST.RESPONSE.redirect(self.absolute_url() + '/signup_successful') security.declareProtected(view, 'signup_successful') def signup_successful(self, REQUEST): """ """ return self.getFormsTool().getContent({'here': self}, 'naaya.content.meeting.subscription_signup_successful') security.declareProtected(view, 'subscribe') def subscribe(self, REQUEST): """ """ meeting = self.getMeeting() if not meeting.allow_register: return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscription_not_allowed') return self.getFormsTool().getContent({'here': self}, 'naaya.content.meeting.subscription_subscribe') security.declareProtected(PERMISSION_ADMIN_MEETING, 'getSignups') def getSignups(self): """ """ return self._signups.itervalues() security.declareProtected(PERMISSION_ADMIN_MEETING, 'getSignup') def getSignup(self, key): """ """ return self._signups.get(key, None) security.declareProtected(PERMISSION_ADMIN_MEETING, 'index_html') def index_html(self, REQUEST): """ """ return self.getFormsTool().getContent({'here': self}, 'naaya.content.meeting.subscription_index') def _accept_signup(self, key): """ """ meeting = self.getMeeting() meeting.getParticipants()._set_attendee(key, PARTICIPANT_ROLE) signup = self._signups[key] signup.accepted = 'accepted' email_sender = meeting.getEmailSender() result = email_sender.send_signup_accepted_email(signup) def _reject_signup(self, key): """ """ meeting = self.getMeeting() signup = self._signups[key] signup.accepted = 'rejected' participants = meeting.getParticipants() if key in participants._get_attendees(): participants._del_attendee(key) email_sender = meeting.getEmailSender() result = email_sender.send_signup_rejected_email(signup) def _delete_signup(self, key): """ """ meeting = self.getMeeting() signup = self._signups.pop(key, None) if signup is None: return participants = meeting.getParticipants() if key in participants._get_attendees(): participants._del_attendee(key) email_sender = meeting.getEmailSender() result = email_sender.send_signup_rejected_email(signup) def _is_signup(self, key): """ """ return self._signups.has_key(key) and self._signups[key].accepted == 'accepted' security.declareProtected(PERMISSION_ADMIN_MEETING, 'manageSignups') def manageSignups(self, REQUEST): """ """ keys = REQUEST.form.get('keys', []) assert isinstance(keys, list) if 'accept' in REQUEST.form: for key in keys: self._accept_signup(key) elif 'reject' in REQUEST.form: for key in keys: self._reject_signup(key) elif 'delete' in REQUEST.form: for key in keys: self._delete_signup(key) return REQUEST.RESPONSE.redirect(self.absolute_url()) security.declarePublic('welcome') def welcome(self, REQUEST): """ """ if 'logout' in REQUEST.form: REQUEST.SESSION['nymt-current-key'] = None return REQUEST.RESPONSE.redirect(self.getMeeting().absolute_url()) key = REQUEST.get('key', None) signup = self.getSignup(key) if self._is_signup(key): REQUEST.SESSION['nymt-current-key'] = key return self.getFormsTool().getContent({'here': self, 'signup': signup}, 'naaya.content.meeting.subscription_welcome') def _add_account_subscription(self, uid): """ """ site = self.getSite() meeting = self.getMeeting() name = getUserFullName(site, uid) email = getUserEmail(site, uid) organization = getUserOrganization(site, uid) if not organization: organization = self.get_survey_answer(uid, 'w_organization') if not organization: organization = self.get_survey_answer(uid, 'w_organisation') phone = getUserPhoneNumber(site, uid) if not phone: phone = self.get_survey_answer(uid, 'w_telephone') if not phone: phone = self.get_survey_answer(uid, 'w_phone') account_subscription = AccountSubscription(uid, name, email, organization, phone) self._account_subscriptions.insert(uid, account_subscription) if meeting.auto_register: self._accept_account_subscription(uid) email_sender = self.getMeeting().getEmailSender() email_sender.send_account_subscription_email(account_subscription) security.declareProtected(PERMISSION_ADMIN_MEETING, 'update_account_subscription') def update_account_subscription(self, uid): """ """ site = self.getSite() name = getUserFullName(site, uid) email = getUserEmail(site, uid) organization = getUserOrganization(site, uid) phone = getUserPhoneNumber(site, uid) account_subscription = AccountSubscription(uid, name, email, organization, phone) self._account_subscriptions.update({uid: account_subscription}) security.declareProtected(view, 'subscribe_account') def subscribe_account(self, REQUEST): """ """ meeting = self.getMeeting() if not meeting.allow_register: return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscription_not_allowed') self._add_account_subscription(REQUEST.AUTHENTICATED_USER.getId()) if self.survey_required: site = self.getSite() path = str(self.survey_pointer) survey_ob = site.unrestrictedTraverse(path, None) if survey_ob is not None and survey_ob.meta_type == 'Naaya Mega Survey': answers = survey_ob.getAnswers() respondents = [a.respondent for a in answers] current_user = REQUEST.AUTHENTICATED_USER.getUserName() if current_user not in respondents: self.setSessionInfoTrans( 'Registration successfully sent for approval. ' 'Please also respond to the following questionaire.') return REQUEST.RESPONSE.redirect('%s/%s' % (self.getSite().absolute_url(), self.survey_pointer)) return REQUEST.RESPONSE.redirect(self.absolute_url() + '/subscribe_account_successful') security.declareProtected(view, 'subscribe_account_successful') def subscribe_account_successful(self, REQUEST): """ """ return self.getFormsTool().getContent({'here': self}, 'naaya.content.meeting.subscription_subscribe_successful') security.declareProtected(PERMISSION_ADMIN_MEETING, 'getAccountSubscriptions') def getAccountSubscriptions(self): """ """ return self._account_subscriptions.itervalues() security.declareProtected(PERMISSION_ADMIN_MEETING, 'getAccountSubscription') def getAccountSubscription(self, uid): """ """ return self._account_subscriptions.get(uid, None) def _is_account_subscription(self, uid): """ """ return self._account_subscriptions.has_key(uid) and self._account_subscriptions[uid].accepted == 'accepted' security.declareProtected(PERMISSION_ADMIN_MEETING, 'manageSignups') def manageAccountSubscriptions(self, REQUEST): """ """ uids = REQUEST.form.get('uids', []) assert isinstance(uids, list) if 'accept' in REQUEST.form: for uid in uids: self._accept_account_subscription(uid) elif 'reject' in REQUEST.form: for uid in uids: self._reject_account_subscription(uid) elif 'delete' in REQUEST.form: for uid in uids: self._delete_account_subscription(uid) return REQUEST.RESPONSE.redirect(self.absolute_url()) def _accept_account_subscription(self, uid): """ """ meeting = self.getMeeting() meeting.getParticipants()._set_attendee(uid, PARTICIPANT_ROLE) account_subscription = self._account_subscriptions[uid] account_subscription.accepted = 'accepted' email_sender = meeting.getEmailSender() result = email_sender.send_account_subscription_accepted_email(account_subscription) def _reject_account_subscription(self, uid): """ """ meeting = self.getMeeting() account_subscription = self._account_subscriptions[uid] account_subscription.accepted = 'rejected' participants = meeting.getParticipants() if uid in participants._get_attendees(): participants._del_attendee(uid) email_sender = meeting.getEmailSender() result = email_sender.send_account_subscription_rejected_email(account_subscription) def _delete_account_subscription(self, uid): """ """ meeting = self.getMeeting() account_subscription = self._account_subscriptions.pop(uid, None) if account_subscription is None: return participants = meeting.getParticipants() if uid in participants._get_attendees(): participants._del_attendee(uid) email_sender = meeting.getEmailSender() result = email_sender.send_account_subscription_rejected_email(account_subscription) security.declareProtected(view, 'subscription_not_allowed') def subscription_not_allowed(self, REQUEST): """ """ return self.getFormsTool().getContent({'here': self}, 'naaya.content.meeting.subscription_not_allowed')
def import_all(context): site = context.getSite() if 'pageDictionary' not in site.objectIds(): pageDictionary = OOBTree() pageDictionary.insert('page','users')##make users a list?
class LookupTable(base.Base): "LookupTable class" meta_type = "LookupTable" security = ClassSecurityInfo() records = None recordsLength = None drawDict = base.Base.drawDict.copy() drawDict['drawTable'] = 'drawTable' security.declareProtected('View management screens', 'edit') def edit(self, *args, **kw): "Inline edit short object" format = "<p>Currently there are %s records</p><div>%s</div>" if self.records is None: self.records = OOBTree() lenRecords = self.recordsLength( ) if self.recordsLength is not None else 0 return format % (lenRecords, self.create_button('clear', "Clear")) security.declarePrivate('processRecorderChanges') def processRecorderChanges(self, form): "process the recorder changes" clear = form.pop('clear', None) if clear is not None: self.clear() security.declarePrivate('after_manage_edit') def before_manage_edit(self, form): "process the edits" self.processRecorderChanges(form) security.declareProtected('View management screens', "drawTable") def drawTable(self): "Render page" temp = [] format = '<p>%s:%s</p>' if self.records is not None: for key, value in self.records.items(): temp.append(format % (repr(key), repr(value))) return ''.join(temp) security.declareProtected('Python Record Modification', 'insert') def insert(self, key, value): "modify this key and value into the OOBTree" if self.records is None: self.records = OOBTree() if self.recordsLength is None: self.setObject('recordsLength', BTrees.Length.Length()) if key not in self.records: self.recordsLength.change(1) self.records.insert(key, value) security.declareProtected('Python Record Modification', 'add') def add(self, key, value): "this this key and value into the OOBTree" if self.records is None: self.records = OOBTree() if self.recordsLength is None: self.setObject('recordsLength', BTrees.Length.Length()) if key not in self.records: self.recordsLength.change(1) self.records[key] = value security.declareProtected('Python Record Access', 'items') def items(self, min=None, max=None): "return the items in this OOBTree" if self.records is None: return [] return self.records.items(min, max) security.declareProtected('Python Record Access', 'values') def values(self, min=None, max=None): "return the values of this OOBTree" if self.records is None: return [] return self.records.values(min, max) security.declareProtected('Python Record Modification', 'update') def update(self, collection): "update our OOBTree with the data in collection" if self.records is None: self.records = OOBTree() if self.recordsLength is None: self.setObject('recordsLength', BTrees.Length.Length()) records = self.records change = self.recordsLength.change for key, value in collection.items(): if key not in records: change(1) records[key] = value security.declareProtected('Python Record Access', 'keys') def keys(self, min=None, max=None): "return the keys of this OOBTree" if self.records is None: return [] return self.records.keys(min, max) security.declareProtected('Python Record Modification', '__delitem__') def __delitem__(self, key): "delete this key from the OOBTree" if self.records is not None: del self.records[key] self.recordsLength.change(-1) security.declareProtected('Python Record Modification', 'remove') def remove(self, key): "delete this key from the OOBTree" if self.records is not None: del self.records[key] self.recordsLength.change(-1) security.declareProtected('Python Record Modification', '__setitem__') def __setitem__(self, key, value): "set this key and value in the OOBTree" if self.records is None: self.records = OOBTree() self.records[key] = value security.declareProtected('Python Record Access', '__getitem__') def __getitem__(self, index): "get this item from the OOBTree" if self.records is not None: return self.records[index] raise KeyError, index security.declareProtected('Python Record Access', 'get') def get(self, key, default=None): "get this item from the OOBTree" if self.records is not None: return self.records.get(key, default) return default security.declareProtected('Python Record Access', 'has_key') def has_key(self, key): "see if we have this key in the OOBTree" if self.records is not None: return self.records.has_key(key) return False security.declareProtected('Python Record Modification', 'clear') def clear(self): "clear the OOBTree" self.setObject('records', None) self.setObject('recordsLength', None) security.declarePrivate("PrincipiaSearchSource") def PrincipiaSearchSource(self): "This is the basic search function" return '' security.declarePrivate('classUpgrader') def classUpgrader(self): "upgrade this class" self.createBTreeLength() security.declarePrivate('createBTreeLength') def createBTreeLength(self): "remove Filters that are not being used" if self.records is not None: length = BTrees.Length.Length() length.set(len(self.records)) self.setObject('recordsLength', length) createBTreeLength = utility.upgradeLimit(createBTreeLength, 165)
class InvertedIndex: def __init__(self): super().__init__() self._btree = BTree() self.build() def build(self): """ Reads files one-by-one and builds the index Arguments: None Returns: None """ if os.path.isfile("InvertedIndex"): print("Loading the Inverted Index from file") with open("InvertedIndex", "rb") as file: self._btree = pickle.load(file) else: print("Building the Inverted Index") dirPath = os.path.dirname(os.path.realpath(__file__)) dataPath = os.path.realpath(os.path.join(dirPath, "..", "data")) files = [ os.path.join(dataPath, file) for file in sorted(os.listdir(dataPath)) ] for file in files: snippets = getSnippets(file) for index, snippet in enumerate(snippets): filename = int(os.path.split(file)[1].split(".csv")[0]) docId = (filename, index + 2) tokens, snippetMetadata = preProcess(snippet) self.updateIndex(tokens, docId) self.sortPostingLists() sys.setrecursionlimit(10000) with open("InvertedIndex", "wb") as file: print("Saving the Inverted Index to file") pickle.dump(self._btree, file) def sortPostingLists(self): words = list(self.getKeys()) for word in words: postingList = self._btree.get(word) postingList.sort(key=lambda x: ((10000 * x[0][0]) + x[0][1])) def getKeys(self): return self._btree.keys() def getValues(self): return self._btree.values() def getPostingListCollection(self, termList): result = [] for term in termList: postingList = self._btree.get(term) if (postingList != None): result.append(postingList) else: result.append([]) return result def documentUnion(self, postingListCollection): docList = set() for postingList in postingListCollection: for doc in postingList: docList.add(doc[0]) return docList def getDocuments(self, termList, queryType=0, queryMetadata=defaultdict(int)): """ Finds the documents containing the terms Arguments: termList - List of query terms Returns: List of document numbers """ docList = [] # Union if (queryType == 0): postingListCollection = self.getPostingListCollection(termList) docList = self.documentUnion(postingListCollection) # intersection/positional elif (queryType == 1): postingListCollection = self.getPostingListCollection(termList) docList = self.documentIntersection(postingListCollection, queryMetadata) return docList def documentIntersection(self, documents, queryMetadata): while (len(documents) > 1): isPositional = False diff = queryMetadata[(len(documents) - 2, len(documents) - 1)] if (diff != 0): isPositional = True list1 = documents.pop() list2 = documents.pop() ptr1 = 0 ptr2 = 0 intersection = [] while (ptr1 < len(list1) and ptr2 < len(list2)): fileNo1 = list1[ptr1][0][0] rowNo1 = list1[ptr1][0][1] fileNo2 = list2[ptr2][0][0] rowNo2 = list2[ptr2][0][1] if (fileNo1 == fileNo2 and rowNo1 == rowNo2): if (isPositional): for postion1 in list1[ptr1][1]: for postion2 in list2[ptr2][1]: if ((postion1 - postion2 + 1) == diff): if (len(intersection) == 0 or intersection[-1][0] != list1[ptr1][0]): intersection.append( (list1[ptr1][0], [postion2])) elif (intersection[-1][0] == list1[ptr1][0] ): intersection[-1][1].append(postion2) else: intersection.append((list1[ptr1][0], list2[ptr2][1])) ptr1 += 1 ptr2 += 1 elif (fileNo1 == fileNo2 and rowNo1 < rowNo2): ptr1 += 1 elif (fileNo1 == fileNo2 and rowNo1 > rowNo2): ptr2 += 1 elif (fileNo1 < fileNo2): ptr1 += 1 elif (fileNo1 > fileNo2): ptr2 += 1 documents.append(intersection) docNolist = [x[0] for x in documents[0]] return docNolist def updateIndex(self, docTokens, docId): """ Updates the inverted index Arguments: docText - Document to be added to the index docId - ID of the document Returns: None """ for word, wordIndex in docTokens: postingList = self._btree.get(word) if postingList is not None: lastdocId = postingList[-1][0] if docId == lastdocId: postingList[-1][1].append(wordIndex) else: postingList.append((docId, [wordIndex])) else: postingList = [(docId, [wordIndex])] self._btree.insert(word, postingList)
class DataBucketStream(Document): """ Represents data stored in many small files inside a "stream". Each file is "addressed" by its key similar to dict. """ meta_type = 'ERP5 Data Bucket Stream' portal_type = 'Data Bucket Stream' add_permission = Permissions.AddPortalContent # Declarative security security = ClassSecurityInfo() security.declareObjectProtected(Permissions.AccessContentsInformation) # Declarative properties property_sheets = (PropertySheet.CategoryCore, PropertySheet.SortIndex) def __init__(self, id, **kw): self.initBucketTree() self.initIndexTree() Document.__init__(self, id, **kw) def __len__(self): return len(self._tree) def initBucketTree(self): """ Initialize the Bucket Tree """ self._tree = OOBTree() def initIndexTree(self): """ Initialize the Index Tree """ self._long_index_tree = LOBTree() def getMaxKey(self, key=None): """ Return the maximum key """ try: return self._tree.maxKey(key) except ValueError: return None def getMaxIndex(self, index=None): """ Return the maximum index """ try: return self._long_index_tree.maxKey(index) except ValueError: return None def getMinKey(self, key=None): """ Return the minimum key """ try: return self._tree.minKey(key) except ValueError: return None def getMinIndex(self, index=None): """ Return the minimum key """ try: return self._long_index_tree.minKey(index) except ValueError: return None def _getOb(self, id, *args, **kw): return None def getBucketByKey(self, key=None): """ Get one bucket """ return self._tree[key].value def getBucketByIndex(self, index=None): """ Get one bucket """ key = self._long_index_tree[index] return self.getBucketByKey(key).value def getBucket(self, key): log('DeprecationWarning: Please use getBucketByKey') return self.getBucketByKey(key) def hasBucketKey(self, key): """ Wether bucket with such key exists """ return key in self._tree def hasBucketIndex(self, index): """ Wether bucket with such index exists """ return self._long_index_tree.has_key(index) def insertBucket(self, key, value): """ Insert one bucket """ try: count = self._long_index_tree.maxKey() + 1 except ValueError: count = 0 except AttributeError: pass try: self._long_index_tree.insert(count, key) except AttributeError: pass value = PersistentString(value) is_new_key = self._tree.insert(key, value) if not is_new_key: self.log("Reingestion of same key") self._tree[key] = value def getBucketKeySequenceByKey(self, start_key=None, stop_key=None, count=None, exclude_start_key=False, exclude_stop_key=False): """ Get a lazy sequence of bucket keys """ sequence = self._tree.keys(min=start_key, max=stop_key, excludemin=exclude_start_key, excludemax=exclude_stop_key) if count is None: return sequence return sequence[:count] def getBucketKeySequenceByIndex(self, start_index=None, stop_index=None, count=None, exclude_start_index=False, exclude_stop_index=False): """ Get a lazy sequence of bucket keys """ sequence = self._long_index_tree.values(min=start_index, max=stop_index, excludemin=exclude_start_index, excludemax=exclude_stop_index) if count is None: return sequence return sequence[:count] def getBucketKeySequence(self, start_key=None, count=None): log('DeprecationWarning: Please use getBucketKeySequenceByKey') return self.getBucketKeySequenceByKey(start_key=start_key, count=count) def getBucketIndexKeySequenceByIndex(self, start_index=None, stop_index=None, count=None, exclude_start_index=False, exclude_stop_index=False): """ Get a lazy sequence of bucket keys """ sequence = self._long_index_tree.items(min=start_index, max=stop_index, excludemin=exclude_start_index, excludemax=exclude_stop_index) if count is not None: sequence = sequence[:count] return IndexKeySequence(self, sequence) def getBucketIndexSequenceByIndex(self, start_index=None, stop_index=None, count=None, exclude_start_index=False, exclude_stop_index=False): """ Get a lazy sequence of bucket keys """ sequence = self._long_index_tree.keys(min=start_index, max=stop_index, excludemin=exclude_start_index, excludemax=exclude_stop_index) if count is None: return sequence return sequence[:count] def getBucketValueSequenceByKey(self, start_key=None, stop_key=None, count=None, exclude_start_key=False, exclude_stop_key=False): """ Get a lazy sequence of bucket values """ sequence = self._tree.values(min=start_key, max=stop_key, excludemin=exclude_start_key, excludemax=exclude_stop_key) if count is None: return sequence return sequence[:count] def getBucketValueSequenceByIndex(self, start_index=None, stop_index=None, count=None, exclude_start_index=False, exclude_stop_index=False): """ Get a lazy sequence of bucket values """ sequence = self._long_index_tree.values(min=start_index, max=stop_index, excludemin=exclude_start_index, excludemax=exclude_stop_index) if count is not None: sequence = sequence[:count] return IndexValueSequence(self, sequence) def getBucketValueSequence(self, start_key=None, count=None): log('DeprecationWarning: Please use getBucketValueSequenceByKey') return self.getBucketValueSequenceByKey(start_key=start_key, count=count) def getBucketKeyItemSequenceByKey(self, start_key=None, stop_key=None, count=None, exclude_start_key=False, exclude_stop_key=False): """ Get a lazy sequence of bucket items """ sequence = self._tree.items(min=start_key, max=stop_key, excludemin=exclude_start_key, excludemax=exclude_stop_key) if count is None: return sequence return sequence[:count] def getBucketItemSequence(self, start_key=None, count=None, exclude_start_key=False): log('DeprecationWarning: Please use getBucketKeyItemSequenceByKey') return self.getBucketKeyItemSequenceByKey( start_key=start_key, count=count, exclude_start_key=exclude_start_key) def getBucketIndexItemSequenceByIndex(self, start_index=None, stop_index=None, count=None, exclude_start_index=False, exclude_stop_index=False): """ Get a lazy sequence of bucket items """ sequence = self._long_index_tree.items(min=start_index, max=stop_index, excludemin=exclude_start_index, excludemax=exclude_stop_index) if count is not None: sequence = sequence[:count] return IndexItemSequence(self, sequence) def getBucketIndexKeyItemSequenceByIndex(self, start_index=None, stop_index=None, count=None, exclude_start_index=False, exclude_stop_index=False): """ Get a lazy sequence of bucket items """ sequence = self._long_index_tree.items(min=start_index, max=stop_index, excludemin=exclude_start_index, excludemax=exclude_stop_index) if count is not None: sequence = sequence[:count] return IndexKeyItemSequence(self, sequence) def getItemList(self): """ Return a list of all key, value pairs """ return [item for item in self._tree.items()] def getKeyList(self): """ Return a list of all keys """ return [key for key in self._tree.keys()] def getIndexList(self): """ Return a list of all indexes """ return [key for key in self._long_index_tree.keys()] def getIndexKeyTupleList(self): """ Return a list of all indexes """ return [key for key in self._long_index_tree.items()] def getMd5sum(self, key): """ Get hexdigest of bucket. """ h = hashlib.md5() h.update(self.getBucketByKey(key)) return h.hexdigest() def delBucketByKey(self, key): """ Remove the bucket. """ del self._tree[key] for index, my_key in list(self.getBucketIndexKeySequenceByIndex()): if my_key == key: del self._long_index_tree[index] def delBucketByIndex(self, index): """ Remove the bucket. """ key = self._long_index_tree[index] del self._tree[key] del self._long_index_tree[index] def rebuildIndexTreeByKeyOrder(self): """ Clear and rebuild the index tree by order of keys """ self.initIndexTree() for count, key in enumerate(self.getBucketKeySequenceByKey()): self._long_index_tree.insert(count, key)
class channel_state: def __init__(self): self.id = 0 self.mix_options = OOBTree() self.song_queue = OOBTree() self.song_queue_pos = 0 self.player: StreamPlayer def set_id(self, id: int): self.id = id def add_option(self, option: mix_option): try: option_index = self.mix_options.maxKey() + 1 except ValueError: option_index = 0 mix_option.id = option_index print('inserting option: ' + str(option_index)) self.mix_options.insert(option_index, option) return option_index def clear_options(self): self.mix_options.clear() def empty_queue(self): self.song_queue.clear() self.reset_queue_pos() def add_queue(self, song: Song): try: song_index = self.song_queue.maxKey() + 1 except ValueError: song_index = 0 print('inserting song: ' + str(song_index)) self.song_queue.insert(song_index, song) return song_index def current_queue_item(self): return self.song_queue[self.song_queue_pos] def increment_queue_pos(self): self.song_queue_pos = self.song_queue_pos + 1 def reset_queue_pos(self): self.song_queue_pos = 0 def has_next_song(self): if self.song_queue.maxKey() > self.song_queue_pos: return True else: return False def play_next(self): if self.current_queue_item().player.error: print("bad stuff happened.") print(self.current_queue_item().player.error) if self.has_next_song(): self.increment_queue_pos() next_song = self.current_queue_item() next_player: StreamPlayer = next_song.player next_player.start() else: self.empty_queue() def play_now(self): current_player: StreamPlayer = self.current_queue_item().player print('playing : ' + str(self.song_queue_pos) + ' ' + str(current_player.is_alive())) if current_player.is_alive() != True: print('starting : ' + str(self.song_queue_pos)) current_player.start() def pause(self): current_player: StreamPlayer = self.current_queue_item().player current_player.pause() def resume(self): current_player: StreamPlayer = self.current_queue_item().player current_player.resume() def stop(self): current_player: StreamPlayer = self.current_queue_item().player current_player.stop()
class PermutermIndex: def __init__(self, invertedIndex): self._invertedIndex = invertedIndex self._btree = BTree() self.build() def build(self): """ Reads words in invertedIndex and builds and permutermIndex Arguments: None Returns: None """ if os.path.isfile("PermutermIndex"): print("Loading the Permuterm Index from file") with open("PermutermIndex", "rb") as file: self._btree = pickle.load(file) else: print("Building the Permuterm Index") words = self._invertedIndex.getKeys() for word in words: wordsRotated = rotateWord(word) for wordR in wordsRotated: self._btree.insert(wordR, word) sys.setrecursionlimit(100000) with open("PermutermIndex", "wb") as file: print("Saving the Permuterm Index to file") pickle.dump(self._btree, file) def search(self, query): """ Searches the permuterm index for the wildcard query Arguments: query - wildcard query to search the permuterm index Returns: List of words matching the query """ queryMin, queryMax = rotateQuery(query) if queryMin == "": return [] matches = set() for word in self._btree.keys(queryMin, queryMax): if word == queryMax: break actualWord = self._btree.get(word) matches.add(actualWord) matches = list(matches) return matches def getDocuments(self, termList, queryType=0, queryMetadata=defaultdict(int)): """ Finds the documents containing the terms Arguments: termList - List of query terms Returns: List of document numbers """ if queryType == 2: queryType = 0 elif queryType == 3: queryType = 1 allTerms = [] for term in termList: if "*" in term: res = self.search(term) else: res = [term] if res != []: allTerms.append(res) termList = list(itertools.product(*allTerms)) docList = [(self._invertedIndex.getDocuments(doc, queryType, queryMetadata), doc) for doc in termList] docList = [doc for doc in docList if doc[0] != []] return docList
class UdbBtreeMultivaluedIndex(UdbIndex): is_ranged = True is_multivalued = True is_prefixed = True is_sorted_asc = True type = 'btree_multivalued' def __init__(self, schema, name=None): from BTrees.OOBTree import OOBTree UdbIndex.__init__(self, schema, name) self._btree = OOBTree() def __len__(self): return len(self._btree) def clear(self): self._btree.clear() return self def delete(self, key, uid): old_existing = self._btree.get(key, EMPTY) if old_existing != EMPTY and uid in old_existing: if len(old_existing) == 1: self._btree.pop(key) else: old_existing.remove(uid) return self def insert(self, key, uid): old_existing = self._btree.get(key, EMPTY) if old_existing == EMPTY: self._btree.insert(key, {uid}) else: old_existing.add(uid) return self def search_by_key(self, key): val = self._btree.get(key, EMPTY) if val != EMPTY: for _ in val: yield _ def search_by_key_in(self, keys): for key in keys: val = self._btree.get(key, EMPTY) if val != EMPTY: for _ in val: yield _ def search_by_key_prefix(self, key): for val in self._btree.values(key, key + CHAR255): for _ in val: yield _ def search_by_key_prefix_in(self, keys): for key in keys: for val in self._btree.values(key, key + CHAR255): for _ in val: yield _ def search_by_key_range(self, gte=None, lte=None, gte_excluded=False, lte_excluded=False): for val in self._btree.values(gte, lte, gte_excluded, lte_excluded): for _ in val: yield _ def upsert(self, old, new, uid): if old != new: old_existing = self._btree.get(old, EMPTY) if old_existing != EMPTY and uid in old_existing: if len(old_existing) == 1: self._btree.pop(old) else: old_existing.remove(uid) new_existing = self._btree.get(new, EMPTY) if new_existing == EMPTY: self._btree.insert(new, {uid}) else: new_existing.add(uid) return self