def evolve(context): # add default category and layer to all calendars # Prevent 'set_created' event handler from being called since it will, # in turn, set the content_modified attribute of community which is used # as the "Last Activity" in the user interface. We don't want this tweak # to impact a community's last activity. This means we need to set created # and modified on the new layers and categories ourselves. registry = getSiteManager() registry.adapters.unsubscribe((IContent, IObjectWillBeAddedEvent), None, set_created) try: search = ICatalogSearch(context) default_category_name = ICalendarCategory.getTaggedValue( 'default_name') default_layer_name = ICalendarLayer.getTaggedValue('default_name') now = datetime.now() cnt, docids, resolver = search(interfaces=[ICalendar]) for docid in docids: calendar = resolver(docid) default_category = create_content(ICalendarCategory, 'Default') default_category.created = default_category.modified = now if not default_category_name in calendar: calendar[default_category_name] = default_category local_layer = create_content(ICalendarLayer, "This Calendar's Events Only", 'blue', [resource_path(default_category)]) local_layer.created = local_layer.modified = now if not default_layer_name in calendar: calendar[default_layer_name] = local_layer finally: registry.adapters.subscribe((IContent, IObjectWillBeAddedEvent), None, set_created)
def calendar_setup_view(context, request): default_category_name = ICalendarCategory.getTaggedValue('default_name') categories = filter(lambda x: x.__name__ != default_category_name, _get_calendar_categories(context)) default_layer_name = ICalendarLayer.getTaggedValue('default_name') layers = filter(lambda x: x.__name__ != default_layer_name, _get_calendar_layers(context)) fielderrors = {} fielderrors_target = None page_title = 'Calendar Setup' api = TemplateAPI(context, request, page_title) return render_template_to_response( 'templates/calendar_setup.pt', back_to_calendar_url=model_url(context, request), categories_url=model_url(context, request, 'categories.html'), layers_url=model_url(context, request, 'layers.html'), formfields=api.formfields, fielderrors=fielderrors, fielderrors_target = fielderrors_target, api=api, editable_categories = categories, editable_layers = layers, all_categories = _get_all_calendar_categories(context, request), colors = _COLORS, )
def evolve(context): # add default category and layer to all calendars # Prevent 'set_created' event handler from being called since it will, # in turn, set the content_modified attribute of community which is used # as the "Last Activity" in the user interface. We don't want this tweak # to impact a community's last activity. This means we need to set created # and modified on the new layers and categories ourselves. registry = getSiteManager() registry.adapters.unsubscribe( (IContent, IObjectWillBeAddedEvent), None, set_created) try: search = ICatalogSearch(context) default_category_name = ICalendarCategory.getTaggedValue('default_name') default_layer_name = ICalendarLayer.getTaggedValue('default_name') now = datetime.now() cnt, docids, resolver = search(interfaces=[ICalendar]) for docid in docids: calendar = resolver(docid) default_category = create_content(ICalendarCategory, 'Default') default_category.created = default_category.modified = now if not default_category_name in calendar: calendar[default_category_name] = default_category local_layer = create_content(ICalendarLayer, "This Calendar's Events Only", 'blue', [resource_path(default_category)]) local_layer.created = local_layer.modified = now if not default_layer_name in calendar: calendar[default_layer_name] = local_layer finally: registry.adapters.subscribe( (IContent, IObjectWillBeAddedEvent), None, set_created)
def calendar_setup_layers_view(context, request): form = CalendarLayersForm() default_layer_name = ICalendarLayer.getTaggedValue('default_name') layers = filter(lambda x: x.__name__ != default_layer_name, _get_calendar_layers(context)) layer_titles = [ x.title for x in layers] layer_names = [ x.__name__ for x in layers ] default_category_name = ICalendarCategory.getTaggedValue('default_name') categories = filter(lambda x: x.__name__ != default_category_name, _get_calendar_categories(context)) if 'form.delete' in request.POST: layer_name = request.POST['form.delete'] if layer_name == default_layer_name: message = 'Cannot delete default layer' elif layer_name and layer_name in layer_names: title = context[layer_name].title del context[layer_name] message = '%s layer removed' % title else: message = 'Layer is invalid' location = model_url(context, request, 'layers.html', query={'status_message': message}) return HTTPFound(location=location) fielderrors_target = None fielderrors = {} if 'form.submitted' in request.POST: try: converted = form.validate(request.POST) category_paths = list(set(request.POST.getall('category_paths'))) layer_name = generate_name(context) layer_title = converted['layer_title'] layer_color = converted['layer_color'] if layer_title in layer_titles: msg = "Name is already used" raise Invalid(value=layer_title, state=None, msg=msg, error_list=None, error_dict={'layer_title': msg}) layer = create_content(ICalendarLayer, layer_title, layer_color, category_paths) context[layer_name] = layer location = model_url( context, request, 'layers.html', query={'status_message':'Calendar layer added'}) return HTTPFound(location=location) except Invalid, e: fielderrors_target = '__add_layer__' fielderrors = e.error_dict
def add(self, context, request): default_category_name = ICalendarCategory.getTaggedValue('default_name') default_layer_name = ICalendarLayer.getTaggedValue('default_name') calendar = create_content(ICalendar) context['calendar'] = calendar calendar = context['calendar'] default_category = create_content(ICalendarCategory, 'Default') calendar[default_category_name] = default_category local_layer = create_content(ICalendarLayer, "This Calendar's Events Only",' blue', [resource_path(default_category)]) calendar[default_layer_name] = local_layer
def _get_texts(object, default): if IPhoto.providedBy(object): return default adapter = queryAdapter(object, ITextIndexData) if adapter is None: if not IContent.providedBy(object) or ICalendarLayer.providedBy(object) or ICalendarCategory.providedBy(object): return default adapter = FlexibleTextIndexData(object) texts = adapter() if not texts: return default return texts
def add(self, context, request): default_category_name = ICalendarCategory.getTaggedValue( 'default_name') default_layer_name = ICalendarLayer.getTaggedValue('default_name') calendar = create_content(ICalendar) context['calendar'] = calendar calendar = context['calendar'] default_category = create_content(ICalendarCategory, 'Default') calendar[default_category_name] = default_category local_layer = create_content(ICalendarLayer, "This Calendar's Events Only", ' blue', [resource_path(default_category)]) calendar[default_layer_name] = local_layer
def _get_texts(object, default): if IPhoto.providedBy(object): return default adapter = queryAdapter(object, ITextIndexData) if adapter is None: if (not IContent.providedBy(object) or ICalendarLayer.providedBy(object) or ICalendarCategory.providedBy(object)): return default adapter = FlexibleTextIndexData(object) texts = adapter() if not texts: return default return texts
def get_textrepr(object, default): adapter = queryAdapter(object, ITextIndexData) if adapter is not None: text = adapter() return text elif (IContent.providedBy(object) and not (ICalendarLayer.providedBy(object) or ICalendarCategory.providedBy(object))): fmt = "%s %s" tr = fmt % ( getattr(object, 'title', ''), getattr(object, 'description', ''), ) return tr return default
def evolve(context): # fix up calendar events with an empty calendar_category (events # which were added before there was a physical _default_category_ # category object). search = ICatalogSearch(context) default_category_name = ICalendarCategory.getTaggedValue('default_name') catalog = find_catalog(context) virtual = catalog['virtual'] cnt, docids, resolver = search(interfaces=[ICalendarEvent]) for docid in docids: event = resolver(docid) if event is None: continue if not getattr(event, 'calendar_category', ''): calendar = event.__parent__ default_path = resource_path(calendar) + '/' + default_category_name event.calendar_category = default_path virtual.reindex_doc(docid, event)
def evolve(context): catalog = find_catalog(context) index = catalog['texts'] for docid in index.index._docweight.keys(): try: path = catalog.document_map.address_for_docid(docid) context = find_resource(context, path) if (ICalendarLayer.providedBy(context) or ICalendarCategory.providedBy(context) or not IContent.providedBy(context)): index.unindex_doc(docid) if hasattr(context, '_p_deactivate'): context._p_deactivate() except KeyError: # Work around some bad data in the db--some calendar categories # got added with '/' characters in their ids. Skip them for now # until we can figure out what to do with them. print "Bad path in catalog: ", path
def evolve(context): catalog = find_catalog(context) index = catalog['texts'] for docid in index.index._docweight.keys(): try: path = catalog.document_map.address_for_docid(docid) context = find_model(context, path) if (ICalendarLayer.providedBy(context) or ICalendarCategory.providedBy(context) or not IContent.providedBy(context)): index.unindex_doc(docid) if hasattr(context, '_p_deactivate'): context._p_deactivate() except KeyError: # Work around some bad data in the db--some calendar categories # got added with '/' characters in their ids. Skip them for now # until we can figure out what to do with them. print "Bad path in catalog: ", path
def evolve(context): # fix up calendar events with an empty calendar_category (events # which were added before there was a physical _default_category_ # category object). search = ICatalogSearch(context) default_category_name = ICalendarCategory.getTaggedValue('default_name') catalog = find_catalog(context) virtual = catalog['virtual'] cnt, docids, resolver = search(interfaces=[ICalendarEvent]) for docid in docids: event = resolver(docid) if event is None: continue if not getattr(event, 'calendar_category', ''): calendar = event.__parent__ default_path = resource_path( calendar) + '/' + default_category_name event.calendar_category = default_path virtual.reindex_doc(docid, event)
def form_widgets(self, fields): # compute category values calendar_categories = [] default_category = None calendar = find_interface(self.context, ICalendar) if calendar: default_category_name = ICalendarCategory.getTaggedValue('default_name') for category in _get_calendar_categories(calendar): category_tuple = (model_path(category), category.title) if category.__name__ == default_category_name: default_category = category_tuple else: calendar_categories.append(category_tuple) calendar_categories.sort(key=lambda x: x[1]) category_widget = formish.SelectChoice(calendar_categories) if default_category: category_widget.none_option = default_category widgets = { 'title': formish.Input(empty=''), 'category': category_widget, 'all_day': formish.Hidden(), 'start_date': karlwidgets.DateTime(), 'end_date': karlwidgets.DateTime(), 'location': formish.Input(empty=''), 'text': karlwidgets.RichTextWidget(empty=''), 'attendees': formish.TextArea(rows=5, cols=60), 'contact_name': formish.Input(empty=''), 'contact_email': formish.Input(empty=''), 'attachments': karlwidgets.AttachmentsSequence(sortable=False, min_start_fields=0), 'attachments.*': karlwidgets.FileUpload2(filestore=self.filestore), } schema = dict(fields) if 'security_state' in schema: security_states = self._get_security_states() widgets['security_state'] = formish.RadioChoice( options=[(s['name'], s['title']) for s in security_states], none_option=None) return widgets
def evolve(context): root = find_root(context) searcher = ICatalogSearch(root) categories_and_layers_query = dict(interfaces={ 'query': [ICalendarCategory, ICalendarLayer], 'operator': 'or' }, ) total, docids, resolver = searcher(**categories_and_layers_query) changed_categories = {} for docid in docids: ob = resolver(docid) if ob is None: # slash in path, likely path = root.catalog.document_map.docid_to_address.get(docid) if path is not None: container_path, ob_name = path.rsplit('/', 1) container = find_resource(root, container_path) name = urllib.unquote(ob_name) ob = container.get(name) if ob is not None: ob_name = ob.__name__ if not (ob_name.startswith('_default_') or is_opaque_id(ob_name)): old_path = resource_path(ob) container = ob.__parent__ new_name = generate_name(container) del container[ob_name] ob.__name__ = new_name # XXX required? container.add(new_name, ob, send_events=False) new_path = resource_path(ob) index_content(ob, None) print 'path fixed: %s -> %s' % (old_path, new_path) if ICalendarCategory.providedBy(ob): changed_categories[old_path] = new_path if changed_categories: # fix layer.paths layers_query = dict(interfaces=[ICalendarLayer]) total, docids, resolver = searcher(**layers_query) for docid in docids: layer = resolver(docid) if layer is not None: new_paths = [] changed = [] for path in layer.paths: if path in changed_categories: new_paths.append(changed_categories[path]) changed.append((path, changed_categories[path])) else: new_paths.append(path) if changed: layer.paths = new_paths reindex_content(layer, None) print 'layer fixed: %s, %s' % (resource_path(layer), [ '%s -> %s' % x for x in changed ]) # fix the category of events events_query = dict(interfaces=[ICalendarEvent], ) total, docids, resolver = searcher(**events_query) for docid in docids: event = resolver(docid) if event is not None: category = event.calendar_category if category in changed_categories: old_category = event.calendar_category new_category = changed_categories[category] event.calendar_category = new_category reindex_content(event, None) print 'event fixed: %s, %s -> %s' % ( resource_path(event), old_category, new_category)
def evolve(context): root = find_root(context) searcher = ICatalogSearch(root) categories_and_layers_query = dict( interfaces={'query':[ICalendarCategory, ICalendarLayer], 'operator':'or'}, ) total, docids, resolver = searcher(**categories_and_layers_query) changed_categories = {} for docid in docids: ob = resolver(docid) if ob is None: # slash in path, likely path = root.catalog.document_map.docid_to_address.get(docid) if path is not None: container_path, ob_name = path.rsplit('/', 1) container = find_model(root, container_path) name = urllib.unquote(ob_name) ob = container.get(name) if ob is not None: ob_name = ob.__name__ if not (ob_name.startswith('_default_') or is_opaque_id(ob_name)): old_path = model_path(ob) container = ob.__parent__ new_name = generate_name(container) del container[ob_name] ob.__name__ = new_name # XXX required? container.add(new_name, ob, send_events=False) new_path = model_path(ob) index_content(ob, None) print 'path fixed: %s -> %s' % (old_path, new_path) if ICalendarCategory.providedBy(ob): changed_categories[old_path] = new_path if changed_categories: # fix layer.paths layers_query = dict(interfaces=[ICalendarLayer]) total, docids, resolver = searcher(**layers_query) for docid in docids: layer = resolver(docid) if layer is not None: new_paths = [] changed = [] for path in layer.paths: if path in changed_categories: new_paths.append(changed_categories[path]) changed.append((path, changed_categories[path])) else: new_paths.append(path) if changed: layer.paths = new_paths reindex_content(layer, None) print 'layer fixed: %s, %s' % ( model_path(layer), [ '%s -> %s' % x for x in changed ]) # fix the category of events events_query = dict( interfaces=[ICalendarEvent], ) total, docids, resolver = searcher(**events_query) for docid in docids: event = resolver(docid) if event is not None: category = event.calendar_category if category in changed_categories: old_category = event.calendar_category new_category = changed_categories[category] event.calendar_category = new_category reindex_content(event, None) print 'event fixed: %s, %s -> %s' % ( model_path(event), old_category, new_category)
tags_field = dict(records=[]) # Render the form and shove some default values in page_title = 'Add Calendar Event' api = TemplateAPI(context, request, page_title) # Get a little policy. Should we suppress alerts? show_sendalert_field = get_show_sendalert(context, request) # Get a layout layout_provider = get_layout_provider(context, request) layout = layout_provider('community') calendar = find_interface(context, ICalendar) if calendar: default_category_name = ICalendarCategory.getTaggedValue('default_name') calendar_categories = [ {'title':x.title, 'path':model_path(x), 'name':x.__name__} for x in _get_calendar_categories(calendar) ] # default name always first calendar_categories.sort( key=lambda x: x['name'] == default_category_name and True or x['title']) else: calendar_categories = [] return render_form_to_response( 'templates/add_calendarevent.pt', form, fill_values, head_data=convert_to_script(dict(
def calendar_setup_categories_view(context, request): default_category_name = ICalendarCategory.getTaggedValue('default_name') default_category = context[default_category_name] default_category_path = model_path(default_category) categories = _get_calendar_categories(context) editable_categories = filter(lambda x: x.__name__ != default_category_name, categories) category_names = [ x.__name__ for x in categories ] default_layer_name = ICalendarLayer.getTaggedValue('default_name') default_layer = context[default_layer_name] layers = _get_calendar_layers(context) editable_layers = filter(lambda x: x.__name__ != default_layer_name, layers) if 'form.delete' in request.POST: category_name = request.POST['form.delete'] if category_name == default_category_name: message = 'Cannot delete default category' elif category_name and category_name in category_names: categ = context[category_name] title = categ.title categ_path = model_path(categ) if categ_path in default_layer.paths: default_layer.paths.remove(categ_path) default_layer._p_changed = True # uncategorize events that were previously in this # category (put them in the default category) query = dict( interfaces=[ICalendarEvent], virtual = categ_path, ) searcher = ICatalogSearch(context) total, docids, resolver = searcher(**query) for event in [ resolver(x) for x in docids ]: event.calendar_category = default_category_path objectEventNotify(ObjectModifiedEvent(event)) del context[category_name] message = '%s category removed' % title else: message = 'Category is invalid' location = model_url(context, request, 'categories.html', query={'status_message': message}) return HTTPFound(location=location) fielderrors = {} fielderrors_target = None if 'form.edit' in request.POST: category_name = request.POST['category__name__'] if category_name == default_category_name: location = model_url( context, request, 'categories.html', query={'status_message':'Cannot edit default category'}) return HTTPFound(location=location) if not category_name or not category_name in category_names: location = model_url( context, request, 'categories.html', query={'status_message':'Could not find category to edit'}) return HTTPFound(location=location) category = context[category_name] try: title = request.POST['category_title'].strip() if not title: msg = 'Please enter a value' raise Invalid(msg, error_dict={'category_title': msg}) title = convert_to_unicode(title, 'category_title') if title in [ x.title for x in categories]: msg = "Name is already used" raise Invalid(msg=msg, error_dict={'category_title': msg}) else: category.title = title location = model_url( context, request, 'categories.html', query={'status_message':'Calendar category updated'}) return HTTPFound(location=location) except Invalid, e: fielderrors_target = ("%s_category" % category_name) fielderrors = e.error_dict
def calendar_setup_layers_view(context, request): default_layer_name = ICalendarLayer.getTaggedValue('default_name') layers = filter(lambda x: x.__name__ != default_layer_name, _get_calendar_layers(context)) layer_titles = [ x.title for x in layers] layer_names = [ x.__name__ for x in layers ] default_category_name = ICalendarCategory.getTaggedValue('default_name') categories = filter(lambda x: x.__name__ != default_category_name, _get_calendar_categories(context)) if 'form.delete' in request.POST: layer_name = request.POST['form.delete'] if layer_name == default_layer_name: message = 'Cannot delete default layer' elif layer_name and layer_name in layer_names: title = context[layer_name].title del context[layer_name] message = '%s layer removed' % title else: message = 'Layer is invalid' location = model_url(context, request, 'layers.html', query={'status_message': message}) return HTTPFound(location=location) fielderrors_target = None fielderrors = {} if 'form.submitted' in request.POST: try: error_dict = {} category_paths = list(set(request.POST.getall('category_paths'))) category_paths = [path for path in category_paths if path] if not category_paths: error_dict['category_paths'] = 'Please enter a value' layer_title = request.POST['layer_title'] if not layer_title: error_dict['layer_title'] = 'Please enter a value' layer_color = request.POST.get('layer_color') if not layer_color: error_dict['layer_color'] = 'Please enter a value' if error_dict: raise Invalid(msg='Please correct the following errors', error_dict=error_dict) layer_title = convert_to_unicode(layer_title, 'layer_title') layer_color = convert_to_unicode(layer_color, 'layer_color') layer_name = generate_name(context) if layer_title in layer_titles: msg = "Name is already used" raise Invalid(msg=msg, error_dict={'layer_title': msg}) layer = create_content(ICalendarLayer, layer_title, layer_color, category_paths) context[layer_name] = layer location = model_url( context, request, 'layers.html', query={'status_message':'Calendar layer added'}) return HTTPFound(location=location) except Invalid, e: fielderrors_target = '__add_layer__' fielderrors = e.error_dict
def _get_calendar_categories(context): return [ x for x in context.values() if ICalendarCategory.providedBy(x) ]