def get_suggested_categories(user): """Gets the suggested categories of a user for the dashboard""" from MaKaC.conference import CategoryManager if not redis_write_client: return [] related = user.favorite_categories | user.get_linked_objects('category', 'manager') res = [] for id_, score in suggestions.get_suggestions(user, 'category').iteritems(): try: categ = CategoryManager().getById(id_) except KeyError: suggestions.unsuggest(user, 'category', id_) continue if not categ or categ.isSuggestionsDisabled() or categ in related: continue if any(p.isSuggestionsDisabled() for p in categ.iterParents()): continue if not categ.canAccess(AccessWrapper(user.as_avatar)): continue res.append({ 'score': score, 'categ': categ, 'path': truncate_path(categ.getCategoryPathTitles()[:-1], chars=50) }) return res
def category_cleanup(): cfg = Config.getInstance() janitor_user = User.get_one(cfg.getJanitorUserId()) logger.debug("Checking whether any categories should be cleaned up") for categ_id, days in cfg.getCategoryCleanup().iteritems(): try: category = CategoryManager().getById(categ_id) except KeyError: logger.warning("Category %s does not exist!", categ_id) continue now = now_utc() to_delete = [ev for ev in category.conferences if (now - ev._creationDS) > timedelta(days=days)] if not to_delete: continue logger.info("Category %s: %s events were created more than %s days ago and will be deleted", categ_id, len(to_delete), days) for i, event in enumerate(to_delete, 1): logger.info("Deleting %s", event) event.delete(user=janitor_user) if i % 100 == 0: db.session.commit() DBMgr.getInstance().commit() db.session.commit() DBMgr.getInstance().commit()
def _getAnswer(self): import MaKaC.common.indexes as indexes nameIdx = indexes.IndexesHolder().getIndex('conferenceTitle') query = ' '.join(map(lambda y: "*%s*" % y, self._searchString.split())) foundEntries = nameIdx.search(query) number = len(foundEntries) # get only the first 10 results foundEntries = foundEntries[:7] entryTitles = [] for (confId, value) in foundEntries: conference = CategoryManager().getById(confId) entryTitles.append({ 'title': conference.getTitle(), 'url': str(urlHandlers.UHConferenceDisplay.getURL(conference)) }) return {"list": entryTitles, "number": number}
def main(): """This script deletes existing category indexes and recreates them.""" db.DBMgr.getInstance().startRequest() im = indexes.IndexesHolder() im.removeById('categoryDate') catIdx = im.getIndex('categoryDate') ch = CategoryManager() totnum = len(ch.getList()) curnum = 0 curper = 0 for cat in ch.getList(): while 1: try: for conf in cat.getConferenceList(): catIdx.indexConf(conf) transaction.commit() break except: db.DBMgr.getInstance().sync() curnum += 1 per = int(float(curnum)/float(totnum)*100) if per != curper: curper = per print "%s%%" % per db.DBMgr.getInstance().endRequest()
def __init__(self, targetCategoryId): self._target = targetCategoryId self._homeId = CategoryManager().getRoot().getId() self._goodConfs = set() self._badConfs = set() self._goodCategs = set() self._badCategs = set()
def _checkParams(self, params): base.RHProtected._checkParams(self, params) self._statusValue = "OK" self._message = "" try: self._title = params.get("title", "").strip() self._startdate = params.get("startdate", "").strip() self._enddate = params.get("enddate", "").strip() self._place = params.get("place", "").strip() self._room = params.get("room", "").strip() self._accessPassword = params.get("an", "").strip() self._modifyPassword = params.get("mp", "").strip() self._style = params.get("style", "").strip() self._fid = params.get("fid", "").strip() self._description = params.get("description", "").strip() self._stime = params.get("stime", "").strip() self._etime = params.get("etime", "").strip() self._speaker = params.get("speaker", "").strip() self._speakeremail = params.get("email", "").strip() self._speakeraffiliation = params.get("affiliation", "").strip() if not self._title or not self._startdate or not self._enddate or not self._fid or not self._stime or not self._etime: raise MaKaCError(_("mandatory parameter missing")) ch = CategoryManager() try: self._cat = ch.getById(self._fid) except: raise MaKaCError(_("unknown category")) except MaKaCError, e: self._statusValue = "ERROR" self._message = e.getMsg()
def cleanup_categories(dbi, logger): config = Config.getInstance() janitor_user = AvatarHolder().getById(config.getJanitorUserId()) logger.info("Checking whether any categories should be cleaned up") for categ_id, days in config.getCategoryCleanup().items(): try: category = CategoryManager().getById(categ_id) except KeyError: logger.warning("Category '{0}' does not exist!".format(categ_id)) continue now = now_utc() to_delete = [ ev for ev in category.conferences if (now - ev._creationDS) > timedelta(days=days) ] if to_delete: logger.info( "Category '{0}': {1} events were created more than {2} days ago and will be deleted" .format(categ_id, len(to_delete), days)) for i, event in enumerate(to_delete, 1): logger.info("Deleting {0}".format(repr(event))) event.delete(user=janitor_user) if i % 100 == 0: dbi.commit() dbi.commit()
def main(): DBMgr.getInstance().startRequest() im = indexes.IndexesHolder() im.removeById('calendar') DBMgr.getInstance().commit() ch = CategoryManager() list = ch.getList() totnum = len(list) curnum = 0 curper = 0 for cat in list: committed = False DBMgr.getInstance().sync() calindex = im.getIndex('calendar') while not committed: try: del cat._calIdx except: pass for conf in cat.getConferenceList(): calindex.indexConf(conf) try: DBMgr.getInstance().commit() committed = True except: DBMgr.getInstance().abort() print "retry %s" % cat.getId() curnum += 1 per = int(float(curnum) / float(totnum) * 100) if per != curper: curper = per if per in [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]: print "done %s%%" % per DBMgr.getInstance().endRequest()
def __init__(self, conference): if conference.getId() == "default": #Best values for CERN printing service self.__topMargin = 1.6 self.__bottomMargin = 1.1 self.__leftMargin = 1.6 self.__rightMargin = 1.4 self.__marginColumns = 1.0 self.__marginRows = 0.0 self._pageSize = "A4" self._landscape = False self._drawDashedRectangles = True else: from MaKaC.conference import CategoryManager defaultConferencePDFOptions = CategoryManager( ).getDefaultConference().getBadgeTemplateManager().getPDFOptions() self.__topMargin = defaultConferencePDFOptions.getTopMargin() self.__bottomMargin = defaultConferencePDFOptions.getBottomMargin() self.__leftMargin = defaultConferencePDFOptions.getLeftMargin() self.__rightMargin = defaultConferencePDFOptions.getRightMargin() self.__marginColumns = defaultConferencePDFOptions.getMarginColumns( ) self.__marginRows = defaultConferencePDFOptions.getMarginRows() self._pageSize = defaultConferencePDFOptions.getPagesize() self._landscape = defaultConferencePDFOptions.getLandscape() self._drawDashedRectangles = defaultConferencePDFOptions.getDrawDashedRectangles( )
def initialize(self, dbi=None): from MaKaC.conference import CategoryManager for cid, categ in CategoryManager()._getIdx().iteritems(): self[cid] = self._initializeSubIndex(categ.conferences) if dbi: dbi.commit()
def _getAnswer(self): import MaKaC.common.indexes as indexes nameIdx = indexes.IndexesHolder().getIndex('categoryName') try: query = ' AND '.join( map( lambda y: "*%s*" % y, filter(lambda x: len(x) > 0, self._searchString.split(' ')))) foundEntries = nameIdx.search(query) except parsetree.ParseError: foundEntries = [] number = len(foundEntries) # get only the first 10 results foundEntries = foundEntries[:7] entryNames = [] for (categId, value) in foundEntries: categ = CategoryManager().getById(categId) entryNames.append({ 'title': categ.getTitle(), 'path': self._getPath(categ), 'url': str(urlHandlers.UHCategoryDisplay.getURL(categ)) }) return {"list": entryNames, "number": number}
def categoryACMigration(dbi, withRBDB, prevVersion): """ Fixing AccessController for categories """ for categ in CategoryManager()._getIdx().itervalues(): _fixAccessController(categ) dbi.commit()
def _check(self, dbi=None): from MaKaC.conference import CategoryManager, ConferenceHolder confIdx = ConferenceHolder()._getIdx() categIdx = CategoryManager()._getIdx() i = 0 for cid, index in self._container.iteritems(): # simple data structure check for problem in index._check(): yield problem # consistency with CategoryManager if cid not in categIdx: yield "Category '%s' not in CategoryManager" % cid # consistency with ConferenceHolder for ts, conf in index.iteritems(): if conf.getId() not in confIdx: yield "[%s] Conference '%s'(%s) not in ConferenceHolder" \ % (cid, conf.getId(), ts) if dbi and i % 100 == 99: dbi.abort() i += 1
def taskMigration(dbi, withRBDB, prevVersion): """ Migrating database tasks from the old format to the new one """ c = Client() for t in HelperTaskList().getTaskListInstance().getTasks(): for obj in t.listObj.values(): print console.colored(" * %s" % obj.__class__.__name__, 'blue') if obj.__class__.__name__ == 'FoundationSync': c.enqueue( FoundationSyncTask(rrule.DAILY, byhour=0, byminute=0)) elif obj.__class__.__name__ == 'StatisticsUpdater': c.enqueue(CategoryStatisticsUpdaterTask( CategoryManager().getById('0'), rrule.DAILY, byhour=0, byminute=0)) elif obj.__class__.__name__ == 'sendMail': # they have to be somewhere in the conference alarm = t.conf.alarmList[t.id] c.enqueue(alarm) else: raise Exception("Unknown task type!") if withRBDB: DALManager.commit() dbi.commit()
def buildCatalog(): #rc.init_catalog() # Forces index rebuild cm = CategoryManager() ch = ConferenceHolder() totnum = len(ch.getValuesToList()) curnum = 0 curper = 0 startFrom = 0 print "Events found:", totnum for c in ch.getValuesToList(): if curnum >= startFrom: print "___________", curnum, ".......confid=", c.getId() rc.fullIndex(c) transaction.commit() curnum += 1 per = int(float(curnum) / float(totnum) * 100) if per != curper: curper = per print str(per) + "%" # Pack it when finished print "Packing...." db.DBMgr.getInstance().pack() print "Done."
def _getTarget(self): try: self._target = self._conf = ConferenceHolder().getById( self._params.get("confId", "")) except: self._target = CategoryManager().getById( self._params.get('categId', 0))
def _process_DELETE(self): category = CategoryManager().getById(request.view_args['category_id']) if category in self.user.favorite_categories: self.user.favorite_categories.remove(category) if redis_write_client: suggestions.unsuggest(self.user, 'category', category.getId()) return jsonify(success=True)
def main(): """ This script deletes existing category indexes and recreates them. """ dbi = DBMgr.getInstance() dbi.startRequest() im = indexes.IndexesHolder() im.removeById('categoryDate') catIdx = im.getIndex('categoryDate') cm = CategoryManager() num_categs = len(cm._getIdx()) cur_num = cur_percent = 0 for cat in cm._getIdx().itervalues(): for conf in cat.conferences.itervalues(): catIdx.indexConf(conf) dbi.commit() cur_num += 1 percent = int(float(cur_num) / num_categs * 100) if percent != cur_percent: cur_percent = percent print "{0}%".format(percent) dbi.endRequest()
def __init__(self, cal, day): self._calendar = cal self._day = day self._categories = set() for __, event in self._calendar._data['events'][self._day.date()]: for categ_id in event.category_chain: categ = CategoryManager().getById(categ_id) if categ in self._calendar._categList: self._categories.add(categ)
def get_icons(detailed_data): icons = defaultdict(set) for __, day in detailed_data['events'].viewitems(): for __, event in day: for categ_id in event.category_chain: if CategoryManager().getById(categ_id).getIcon(): icons[categ_id].add(event.id) return icons
def main(argv): category = -1 meeting = -1 show = 0 try: opts, args = getopt.getopt(argv, "hm:c:s", ["help", "meeting=", "category=", "show"]) except getopt.GetoptError: usage() sys.exit(2) for opt, arg in opts: if opt in ("-h", "--help"): usage() sys.exit() elif opt in ("-s", "--show"): show = 1 elif opt in ("-m", "--meeting"): meeting = arg elif opt in ("-c", "--category"): category = arg #Create database instance and open trashcan manager object DBMgr.getInstance().startRequest() t = TrashCanManager() conf = None if (show): for i in t.getList(): if isinstance(i, Conference): if meeting != -1 and i.getId() == meeting: print "[%s]%s" % (i.getId(), i.getTitle()) elif meeting == -1: print "[%s]%s" % (i.getId(), i.getTitle()) sys.exit() if (meeting != -1 and category != -1): print "Meeting:%s" % meeting print "Category:%s" % category for i in t.getList(): if isinstance(i, Conference): if i.getId() == meeting: conf = i break if conf: #Remove the meeting from conference t.remove(conf) #Attach meeting to desired category cat = CategoryManager().getById(category) ConferenceHolder().add(conf) cat._addConference(conf) #Add Evaluation c = ConferenceHolder().getById(meeting) from MaKaC.evaluation import Evaluation c.setEvaluations([Evaluation(c)]) DBMgr.getInstance().endRequest()
def categoryConfDictToTreeSet(dbi, withRBDB, prevVersion): """ Replacing the conference dictionary in the Category objects by a OOTreeSet. """ for categ in CategoryManager()._getIdx().itervalues(): categ.conferencesBackup = categ.conferences.values() categ.conferences = OOTreeSet(categ.conferences.itervalues()) if len(categ.conferences) != len(categ.conferencesBackup): print "Problem migrating conf dict to tree set: %s" % categ.getId()
def _getCategoryPath(id, aw): path = [] firstCat = cat = CategoryManager().getById(id) visibility = cat.getVisibility() while cat: # the first category (containing the event) is always shown, others only with access iface = ICategoryMetadataFossil if firstCat or cat.canAccess(aw) else ICategoryProtectedMetadataFossil path.append(fossilize(cat, iface)) cat = cat.getOwner() if visibility > len(path): visibilityName = "Everywhere" elif visibility == 0: visibilityName = "Nowhere" else: categId = path[visibility-1]["id"] visibilityName = CategoryManager().getById(categId).getName() path.reverse() path.append({"visibility": {"name": visibilityName}}) return path
def _process_PUT(self): category = CategoryManager().getById(request.view_args['category_id']) if category not in self.user.favorite_categories: if not category.canAccess(AccessWrapper(self.user.as_avatar)): raise Forbidden() self.user.favorite_categories.add(category) if redis_write_client: suggestions.unignore(self.user, 'category', category.getId()) suggestions.unsuggest(self.user, 'category', category.getId()) return jsonify(success=True)
def object(self): from MaKaC.conference import CategoryManager if self.link_type == LinkType.category: return CategoryManager().getById(self.category_id, True) elif self.link_type == LinkType.event: return self.event_new elif self.link_type == LinkType.session: return self.session elif self.link_type == LinkType.contribution: return self.contribution elif self.link_type == LinkType.subcontribution: return self.subcontribution
def main(): """This script deletes existing category indexes and recreates them.""" db.DBMgr.getInstance().startRequest() ch = CategoryManager() for cat in ch.getList(): for conf in cat.getConferenceList(): chconf(conf) get_transaction().commit() # Tasks htl = timerExec.HelperTaskList.getTaskListInstance() for task in htl.getTasks(): chtask(task) db.DBMgr.getInstance().endRequest()
def reindexCategoryNameAndConferenceTitle(dbi, withRBDB, prevVersion): """ Indexing Conference Title with new WhooshTextIndex """ IndexesHolder().removeById('conferenceTitle') IndexesHolder().removeById('categoryName') confTitleIdx = IndexesHolder().getIndex('conferenceTitle') categNameIdx = IndexesHolder().getIndex('categoryName') dbi.commit() confTitleIdx.clear() confTitleIdx.initialize(dbi, ConferenceHolder().itervalues()) categNameIdx.clear() categNameIdx.initialize(dbi, CategoryManager().itervalues())
def _process(self): self._req.content_type = "text/xml" cm = CategoryManager() try: XG = xmlGen.XMLGen() cat = cm.getById(self._id) self._getHeader(XG) self._getCategXML(cat, XG, self._fp) self._getFooter(XG) return XG.getXml() except: value = "ERROR" message = "Category does not exist" if value != "OK": return self._createResponse(value, message)
def reindexCategoryNameAndConferenceTitle(dbi, prevVersion): """ Indexing Conference Title with new WhooshTextIndex """ IndexesHolder().removeById('conferenceTitle') IndexesHolder().removeById('categoryName') confTitleIdx = IndexesHolder().getIndex('conferenceTitle') categNameIdx = IndexesHolder().getIndex('categoryName') dbi.commit() iterator = (x[1] for x in console.conferenceHolderIterator(ConferenceHolder(), deepness='event')) confTitleIdx.clear() confTitleIdx.initialize(dbi, iterator) categNameIdx.clear() categNameIdx.initialize(dbi, CategoryManager().itervalues())
def _export(self, args): logger = _basicStreamHandler() if console.yesno("This will export all the data to the remote service " "using the agent (takes LONG). Are you sure?"): try: agent = self._sm.getAllAgents()[args.agent] except KeyError: raise Exception("Agent '%s' was not found!" % args.agent) root = CategoryManager().getById(args.cat or 0) if args.monitor: monitor = open(args.monitor, 'w') else: monitor = None if args.fast: iterator = console.conferenceHolderIterator( ConferenceHolder(), verbose=args.verbose) else: iterator = categoryIterator(root, 0, verbose=args.verbose) if args.output: nbatch = 0 batch = [] for record, rid, operation in _wrapper(_only_second(iterator), agent._creationState, self._dbi): if len(batch) > SIZE_BATCH_PER_FILE: self._writeFile(agent, args.output, nbatch, batch, logger) nbatch += 1 batch = [] batch.append((record, rid, operation)) if batch: self._writeFile(agent, args.output, nbatch, batch, logger) else: agent._run(_wrapper(_only_second(iterator), agent._creationState, self._dbi), logger=logger, monitor=monitor) if monitor: monitor.close()