def get_person_by_surrogate_id(self, surrogate_id): #Validate input self._validate_subscriber_id(surrogate_id) url = "/notification/v1/person/%s" % (surrogate_id) dao = NWS_DAO() response = dao.getURL(url, {"Accept": "application/json"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) person = Person() Serializer().deserialize(person, response.data) return person
def get_channels_by_sln_year_quarter(self, channel_type, sln, year, quarter): """ Search for all channels by sln, year and quarter """ url = "/notification/v1/channel?type=%s&tag_sln=%s&tag_year=%s&tag_quarter=%s" % (channel_type, sln, year, quarter) dao = NWS_DAO() response = dao.getURL(url, {"Accept": "application/json"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) channel_list = ChannelList() Serializer().deserialize(channel_list, response.data) return channel_list.view_models
def get_channels(self, first_result = 1, max_results = 10): """ Search for all channels """ url = "/notification/v1/channel?first_result=%s&max_results=%s" % (first_result, max_results) dao = NWS_DAO() response = dao.getURL(url, {"Accept": "application/json"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) channel_list = ChannelList() Serializer().deserialize(channel_list, response.data) return channel_list.view_models
def resend_sms_endpoint_verification(self, endpoint_id): """ Calls NWS function to resend verification message to endpoint's phone number """ #Validate input self._validate_uuid(endpoint_id) url = "/notification/v1/endpoint/%s/verification" % (endpoint_id) dao = NWS_DAO() post_response = dao.postURL(url, None, None) if post_response.status != 202: raise DataFailureException(url, post_response.status, post_response.data) return post_response.status
def get_channel_by_surrogate_id(self, channel_type, surrogate_id): """ Get a channel by surrogate id """ key = "%s|%s" % (channel_type, surrogate_id) url = "/notification/v1/channel/%s" % (quote(key)) dao = NWS_DAO() response = dao.getURL(url, {"Accept": "application/json"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) channel = Channel() Serializer().deserialize(channel, response.data) return channel
def update_channel(self, channel): """ Update an existing channel :param channel: is the updated channel that the client wants to update """ #Update the channel dao = NWS_DAO() url = "/notification/v1/channel/%s" % (channel.channel_id) put_response = dao.putURL(url, {"Content-Type": "application/json"}, Serializer().serialize(channel)) #Http response code 204 No Content: #The server has fulfilled the request but does not need to return an entity-body if put_response.status != 204: raise DataFailureException(url, put_response.status, put_response.data) return put_response.status
def create_new_channel(self, channel): """ Create a new channel :param channel: is the new channel that the client wants to create """ #Create new channel dao = NWS_DAO() url = "/notification/v1/channel" post_response = dao.postURL(url, {"Content-Type": "application/json"}, Serializer().serialize(channel)) #HTTP Status Code 201 Created: The request has been fulfilled and resulted #in a new resource being created if post_response.status != 201: raise DataFailureException(url, post_response.status, post_response.data) return post_response.status
def get_endpoint_by_endpoint_id(self, endpoint_id): """ Get an endpoint by endpoint id """ #Validate the channel_id self._validate_uuid(endpoint_id) url = "/notification/v1/endpoint/%s" % (endpoint_id) dao = NWS_DAO() response = dao.getURL(url, {"Accept": "application/json"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) endpoint = Endpoint() Serializer().deserialize(endpoint, response.data) return endpoint
def get_channel_by_channel_id(self, channel_id): """ Get a channel by channel id """ #Validate the channel_id self._validate_uuid(channel_id) url = "/notification/v1/channel/%s" % (channel_id) dao = NWS_DAO() response = dao.getURL(url, {"Accept": "application/json"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) channel = Channel() Serializer().deserialize(channel, response.data) return channel
def get_endpoints_by_subscriber_id(self, subscriber_id): """ Search for all endpoints by a given subscriber """ #Validate input self._validate_subscriber_id(subscriber_id) url = "/notification/v1/endpoint?subscriber_id=%s" % (subscriber_id) dao = NWS_DAO() response = dao.getURL(url, {"Accept": "application/json"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) endpoint_list = EndpointList() Serializer().deserialize(endpoint_list, response.data) return endpoint_list.view_models
def term_has_active_channel(self, channel_type, term): """ Checks to see if there exists a channel for the given sws.Term object's year and quarter. """ #Sets now to midnight of current day to allow for caching now = datetime.combine(datetime.utcnow().date(), time.min).isoformat() dao = NWS_DAO() url = "/notification/v1/channel?tag_year=%s&tag_quarter=%s&max_results=1&expires_after=%s" % (term.year, term.quarter, now) response = dao.getURL(url, {"Accept": "application/json"}) if response.status != 200: return False data = json.loads(response.data) if "TotalCount" in data: if data["TotalCount"] > 0: return True return False
def create_new_message(self, dispatch): """ Create a new dispatch :param dispatch: is the new dispatch that the client wants to create """ #Create new dispatch dao = NWS_DAO() url = "/notification/v1/dispatch" data = Serializer().serialize(dispatch) post_response = dao.postURL(url, {"Content-Type": "application/json"}, data) if post_response.status != 200: raise DataFailureException(url, post_response.status, post_response.data) return post_response.status
def delete_channel(self, channel_id): """ Deleting an existing channel :param channel_id: is the channel that the client wants to delete """ #Validate the subscription_id self._validate_uuid(channel_id) #Delete the subscription url = "/notification/v1/channel/%s" % (channel_id) dao = NWS_DAO() delete_response = dao.deleteURL(url, None) #Http response code 204 No Content: #The server has fulfilled the request but does not need to return an entity-body if delete_response.status != 204: raise DataFailureException(url, delete_response.status, delete_response.data) return delete_response.status
def update_person(self, person): """ Update an existing person :param person: is the updated person that the client wants to update """ #Validate self._validate_regid(person.person_id) self._validate_subscriber_id(person.surrogate_id) attributes = person.get_attributes() person.attributes = None for attribute in attributes: if attribute.name in MANAGED_ATTRIBUTES: continue person.add_attribute(attribute.name, attribute.value, None, None) # ATTRIBUTE_TYPE_EMAIL_DISPATCHED_COUNT = 'DispatchedEmailCount' # ATTRIBUTE_TYPE_SMS_DISPATCHED_COUNT = 'DispatchedTextMessageCount' # ATTRIBUTE_TYPE_SMS_SENT_COUNT = 'SentTextMessageCount' # ATTRIBUTE_TYPE_SUBSCRIPTION_COUNT = 'SubscriptionCount' dao = NWS_DAO() url = "/notification/v1/person/%s" % (person.person_id) headers = {"Content-Type": "application/json"} if self.override_user is not None: headers['X_UW_ACT_AS'] = self.override_user put_response = dao.putURL(url, headers, Serializer().serialize(person)) #Http response code 204 No Content: #The server has fulfilled the request but does not need to return an entity-body if put_response.status != 204: raise DataFailureException(url, put_response.status, put_response.data) return put_response.status
def get_endpoint_by_address(self, endpoint_address): """ Get an endpoint by address """ url = "/notification/v1/endpoint?endpoint_address=%s" % endpoint_address dao = NWS_DAO() response = dao.getURL(url, {"Accept": "application/json"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) endpoint_list = EndpointList() Serializer().deserialize(endpoint_list, response.data) endpoint_vms = endpoint_list.view_models if len(endpoint_vms) == 0: raise DataFailureException(url, 404, {"Message": "No endpoint found"}) endpoint = endpoint_vms[0] return endpoint
def get_endpoint_by_subscriber_id_and_protocol(self, subscriber_id, protocol): """ Get an endpoint by subscriber_id and protocol """ self._validate_subscriber_id(subscriber_id) url = "/notification/v1/endpoint?subscriber_id=%s&protocol=%s" % (subscriber_id, protocol) dao = NWS_DAO() response = dao.getURL(url, {"Accept": "application/json"}) if response.status != 200: raise DataFailureException(url, response.status, response.data) endpoint_list = EndpointList() Serializer().deserialize(endpoint_list, response.data) endpoint_vms = endpoint_list.view_models if len(endpoint_vms) == 0: raise DataFailureException(url, 404, {"Message": "No SMS endpoint found"}) endpoint = endpoint_vms[0] return endpoint
def create_new_subscription(self, subscription): """ Create a new subscription :param subscription: is the new subscription that the client wants to create """ #Validate input if subscription.get_subscription_id() is not None: self._validate_uuid(subscription.get_subscription_id()) if subscription.get_endpoint() is not None: if subscription.get_endpoint().user: self._validate_subscriber_id(subscription.endpoint.user) if subscription.get_endpoint().get_endpoint_id() is not None: self._validate_uuid(subscription.endpoint.get_endpoint_id()) if subscription.get_channel() is not None: self._validate_uuid(subscription.channel.get_channel_id()) #Create new subscription dao = NWS_DAO() url = "/notification/v1/subscription" headers = {"Content-Type": "application/json"} if self.override_user is not None: headers['X_UW_ACT_AS'] = self.override_user post_response = dao.postURL(url, headers, Serializer().serialize(subscription)) #HTTP Status Code 201 Created: The request has been fulfilled and resulted #in a new resource being created if post_response.status != 201: raise DataFailureException(url, post_response.status, post_response.data) return post_response.status
def proxy(request, service, url): if not hasattr(settings, "RESTCLIENTS_ADMIN_GROUP"): print "You must have a group defined as your admin group." print 'Configure that using RESTCLIENTS_ADMIN_GROUP="u_foo_bar"' raise Exception("Missing RESTCLIENTS_ADMIN_GROUP in settings") user_service = UserService() actual_user = user_service.get_original_user() g = Group() is_admin = g.is_member_of_group(actual_user, settings.RESTCLIENTS_ADMIN_GROUP) if not is_admin: return HttpResponseRedirect("/") use_pre = False headers = {} if service == "sws": dao = SWS_DAO() headers["X-UW-Act-as"] = actual_user elif service == "pws": dao = PWS_DAO() elif service == "gws": dao = GWS_DAO() elif service == "nws": dao = NWS_DAO() elif service == "hfs": dao = Hfs_DAO() elif service == "book": dao = Book_DAO() elif service == "canvas": dao = Canvas_DAO() elif service == "grad": dao = Grad_DAO() elif service == "uwnetid": dao = Uwnetid_DAO() elif service == "libraries": dao = MyLibInfo_DAO() elif service == "libcurrics": dao = LibCurrics_DAO() elif service == "myplan": dao = MyPlan_DAO() elif service == "iasystem": dao = IASYSTEM_DAO() headers = {"Accept": "application/vnd.collection+json"} subdomain = None if url.endswith('/evaluation'): if url.startswith('uwb/') or url.startswith('uwt/'): subdomain = url[:3] url = url[4:] else: subdomain = url[:2] url = url[3:] elif service == "calendar": dao = TrumbaCalendar_DAO() use_pre = True else: return HttpResponseNotFound("Unknown service: %s" % service) url = "/%s" % quote(url) if request.GET: try: url = "%s?%s" % (url, urlencode(request.GET)) except UnicodeEncodeError: err = "Bad URL param given to the restclients browser" return HttpResponse(err) start = time() try: if service == "iasystem" and subdomain is not None: response = dao.getURL(url, headers, subdomain) else: response = dao.getURL(url, headers) except Exception as ex: response = MockHTTP() response.status = 500 response.data = str(ex) end = time() # Assume json, and try to format it. try: if not use_pre: content = format_json(service, response.data) json_data = response.data else: content = response.data json_data = None except Exception as e: content = format_html(service, response.data) json_data = None context = { "url": unquote(url), "content": content, "json_data": json_data, "response_code": response.status, "time_taken": "%f seconds" % (end - start), "headers": response.headers, "override_user": user_service.get_override_user(), "use_pre": use_pre, } try: loader.get_template("restclients/extra_info.html") context["has_extra_template"] = True context["extra_template"] = "restclients/extra_info.html" except TemplateDoesNotExist: pass try: loader.get_template("restclients/proxy_wrapper.html") context["wrapper_template"] = "restclients/proxy_wrapper.html" except TemplateDoesNotExist: context["wrapper_template"] = "proxy_wrapper.html" try: search_template_path = re.sub(r"\..*$", "", url) search_template = "proxy/%s%s.html" % (service, search_template_path) loader.get_template(search_template) context["search_template"] = search_template context["search"] = format_search_params(url) except TemplateDoesNotExist: context["search_template"] = None return render_to_response("proxy.html", context, context_instance=RequestContext(request))