def get_events(**kwargs): """ Return a list of events matching the passed filter. Supported kwargs are listed at http://knowledge25.collegenet.com/display/WSW/events.xml """ url = "events.xml" if len(kwargs): url += "?%s" % urlencode(kwargs) return events_from_xml(get_resource(url))
def get_reservations(**kwargs): """ Return a list of reservations matching the passed filter. Supported kwargs are listed at http://knowledge25.collegenet.com/display/WSW/reservations.xml """ kwargs["scope"] = "extended" url = "reservations.xml" if len(kwargs): url += "?%s" % urlencode(kwargs) return reservations_from_xml(get_resource(url))
def update_get_space_ids(ems_rooms): """ Get R25 space_ids for EMS Rooms. :param ems_rooms: A collection of ems_client.models.Room :return: A dictionary of Room.id: space_id """ space_ids = {} for room in ems_rooms: if room.active and room.external_reference is not None: space_ids[room.id] = room.external_reference # while we're here, update the R25 saved search that we'll use query_url = "space_search.xml?query_id=%s" % settings.EMS_R25_SPACE_QUERY try: r25_query_tree = get_resource(query_url) except (R25ErrorException, MaxRetryError, XMLSyntaxError) as ex: logger.warning("R25 error while retrieving space search: %s" % ex) return space_ids snode = r25_query_tree.xpath("r25:search", namespaces=nsmap)[0] snode.attrib["status"] = "mod" step = snode.xpath("r25:step", namespaces=nsmap)[0] step.attrib["status"] = "del" query_modified = False found_space_ids = [] for pnode in step.xpath("r25:step_param", namespaces=nsmap): space_id = pnode.xpath("r25:space_id", namespaces=nsmap)[0].text found_space_ids.append(space_id) pnode.attrib["status"] = "del" if space_id not in space_ids.values(): query_modified = True # replace with new search step step = etree.Element("{%s}step" % nsmap["r25"], attrib={"status": "new"}) node = etree.Element("{%s}step_number" % nsmap["r25"]) node.text = "1" step.append(node) node = etree.Element("{%s}step_type_id" % nsmap["r25"]) node.text = "407" # Spaces step.append(node) node = etree.Element("{%s}qualifier" % nsmap["r25"]) node.text = "1" # Include Any step.append(node) snode.append(step) step_param_nbr = 0 for space_id in space_ids.values(): step_param_nbr += 1 param = etree.Element("{%s}step_param" % nsmap["r25"]) node = etree.Element("{%s}step_param_nbr" % nsmap["r25"]) node.text = str(step_param_nbr) param.append(node) node = etree.Element("{%s}space_id" % nsmap["r25"]) node.text = space_id param.append(node) step.append(param) if space_id not in found_space_ids: query_modified = True if query_modified: try: put_resource(query_url, etree.tostring(r25_query_tree)) except (R25ErrorException, MaxRetryError, XMLSyntaxError) as ex: logger.warning("R25 error while updating space search: %s" % ex) return space_ids
def get_event_by_id(event_id): url = "event.xml?event_id=%s" % event_id return events_from_xml(get_resource(url))[0]
def get_event_by_alien_id(alien_id): url = "event.xml?alien_uid=%s" % quote(alien_id) event = events_from_xml(get_resource(url)) return event[0] if event else None
def get_space_by_id(space_id): url = "space.xml?space_id=%s" % space_id return spaces_from_xml(get_resource(url))[0]
def update_event(event): """ Create or update the given event in R25 Only the features supported by uw_r25 are supported here. Some unsupported fields are initialized with hard-coded values suitable for events from ASA's EMS. :param event: a uw_r25.models.event :return: the new or updated event from R25, as a uw_r25.models.event """ # Start with xml from R25, by requesting a new blank event, or getting the # editable version of our existing event if event.event_id is None: # Create a new event url = "events.xml" event_tree = post_resource(url) enode = event_tree.xpath("r25:event", namespaces=nsmap)[0] event.event_id = enode.xpath("r25:event_id", namespaces=nsmap)[0].text logger.debug("created new event %s" % event.event_id) # initialize some things that aren't kept in the uw_r25 model update_value(enode, "node_type", "E") onode = enode.xpath("r25:organization", namespaces=nsmap)[0] update_value( onode, "organization_id", R25_DAO().get_service_setting("ORGANIZATION"), ) update_value(onode, "primary", "T") # delete the blank profile pnode = enode.xpath("r25:profile", namespaces=nsmap)[0] enode.remove(pnode) else: url = "event.xml?event_id=%s&mode=edit" % event.event_id event_tree = get_resource(url) enode = event_tree.xpath("r25:event", namespaces=nsmap)[0] update_value(enode, "alien_uid", event.alien_uid) update_value(enode, "event_name", event.name) update_value(enode, "event_title", event.title) update_value(enode, "start_date", event.start_date) update_value(enode, "end_date", event.end_date) update_value(enode, "state", event.state) update_value(enode, "event_type_id", event.event_type_id) update_value(enode, "parent_id", event.parent_id) update_value(enode, "cabinet_id", event.cabinet_id) update_value(enode, "cabinet_name", event.cabinet_name) # add or update each reservation/profile # only one reservation per profile is supported for res in event.reservations: if res.reservation_id: # find existing profile xpath = ( "r25:profile[./r25:reservation/r25:reservation_id = '%s']" % res.reservation_id) pnode = enode.xpath(xpath, namespaces=nsmap)[0] rnode = pnode.xpath("r25:reservation", namespaces=nsmap)[0] else: # add new profile and reservation pnode = add_node(enode, "profile") rnode = add_node(pnode, "reservation") update_value(pnode, "profile_name", res.profile_name) update_value(pnode, "init_start_dt", res.start_datetime) update_value(pnode, "init_end_dt", res.end_datetime) update_value(rnode, "reservation_start_dt", res.start_datetime) update_value(rnode, "reservation_end_dt", res.end_datetime) update_value(rnode, "event_start_dt", res.start_datetime) update_value(rnode, "event_end_dt", res.end_datetime) update_value(rnode, "pre_event_start_dt", res.start_datetime) update_value(rnode, "post_event_end_dt", res.end_datetime) update_value(rnode, "reservation_state", res.state) # add or update space_reservation # only one space_reservation per reservation is supported srnode = None try: srnode = rnode.xpath("r25:space_reservation", namespaces=nsmap)[0] except IndexError: pass if srnode is not None: if (res.space_reservation is None or srnode.xpath("r25:space_id", namespaces=nsmap)[0].text != res.space_reservation.space_id): # outdated space reservation. delete it delete_node(srnode) srnode = None if res.space_reservation is not None: if srnode is None: # Add space reservation srnode = add_node(rnode, "space_reservation") update_value(srnode, "space_id", res.space_reservation.space_id) # Make sure event dates encompass all reservations # for res in r25_event.reservations: # res_start_date = res.start_datetime.split('T')[0] # res_end_date = res.end_datetime.split('T')[0] # if res_start_date < r25_event.start_date: # r25_event.start_date = res_start_date.isoformat() # if res_end_date > r25_event.end_date: # r25_event.end_date = res_end_date if enode.attrib["status"] == "est": logger.debug("Event unchanged") return event url = "event.xml?event_id=%s&return_doc=T" % event.event_id return events_from_xml(put_resource(url, etree.tostring(event_tree)))[0]
def get_reservation_by_id(reservation_id): url = "reservation.xml?rsrv_id=%s" % reservation_id return reservations_from_xml(get_resource(url))[0]