def getWebistesByServiceID(self, sid, no_json=False): """ Returns a website list given an Service ID Uses :func:`~app.DAOs.WebsiteDAO.WebsiteDAO.getWebsitesByServiceID` as well as :func:`~app.handlers.WebsiteHandler._buildWebsiteResponse` :param sid: Service ID. :type sid: int :param no_json: Indicates if response is returned as JSON or list. :type no_json: bool :returns JSON Response Object: JSON Response Object containing success or error response. """ dao = WebsiteDAO() sites = dao.getWebsitesByServiceID(sid=sid) site_list = [] if not sites: site_list = None else: for row in sites: site_list.append(_buildWebsiteResponse(website_tuple=row)) response = site_list if no_json: return response return jsonify(response)
def removeServiceWebsite(self, sid, json, uid): """ Remove a list of websites (urls and desriptions) to a given service. Uses :func:`~app.DAOs.WebsiteDAO.WebsiteDAO.removeWebsitesGivenServiceID` as well as: * :func:`~app.handlers.WebsiteHandler.WebsiteHandler.unpackWebsites` * :func:`~app.handlers.WebsiteHandler.WebsiteHandler.getWebsiteByID` * :func:`~app.handlers.WebsiteHandler._buildCoreWebsiteResponse` :param sid: Service ID. :type sid: int :param json: list of websites wit the keys: * Websites [] :type json: array :returns JSON Response Object: JSON Response Object containing success or error response. """ for key in SERVICEWEBSITEKEYS: if key not in json: return jsonify(Error='Missing credentials from submission: ' + key), 400 handler = WebsiteHandler() sites = [] siteIDs = [] websiteInfo = [] sites = (handler.unpackWebsites(json['websites'])) dao = WebsiteDAO() if not sites: websiteInfo = None else: for x in sites: ID = (dao.removeWebsitesGivenServiceID(sid=sid, wid=x['wid'], uid=uid)) # print('Removed PhoneID '+str(x['phoneid']) + ' from service '+ str(sid)) if (ID == None): return jsonify( Error="Website ID not associated with Service-> sid: " + str(sid) + ' websiteid: ' + (str(x['wid']))), 403 else: siteIDs.append(int(ID)) # print('Phones deleted IDs: '+ str(phoneIDs)) for row in siteIDs: websiteInfo.append( _buildCoreWebsiteResponse( website_tuple=dao.getWebsiteByID(row))) return jsonify({"websites": (websiteInfo)})
def getWebsiteByID(self, wid): """ Return a website given it's ID. Uses :func:`~app.DAOs.WebsiteDAO.WebsiteDAO.getWebsiteByID` as well as :func:`~app.handlers.WebsiteHandler._buildWebsiteIDResponse` :param wid: Website ID. :type wid: int :returns JSON Response Object: JSON Response Object containing success or error response. """ dao = WebsiteDAO() site = dao.getWebsiteByID(wid=wid) return _buildWebsiteResponse(site)
def createWebsite(self, url, uid): """ Attempt to create a website. Uses :func:`~app.DAOs.WebsiteDAO.WebsiteDAO.createWebsite` as well as :func:`~app.handlers.WebsiteHandler._buildWebsiteIDResponse` :param uid: User ID. :type uid: int :param url: A link to a website :type url: string :returns JSON Response Object: JSON Response Object containing success or error response. """ dao = WebsiteDAO() websiteID = dao.createWebsite(url=url, uid=uid) return _buildWebsiteIDResponse(websiteID)
def insertServiceWebsite(self, sid, json, uid): """ Insert a list of websites (urls and desriptions) to a given service. Uses :func:`~app.DAOs.WebsiteDAO.WebsiteDAO.insertWebsiteToService` as well as: * :func:`~app.handlers.WebsiteHandler.WebsiteHandler.unpackWebsites` * :func:`~app.handlers.WebsiteHandler._buildWebsiteResponse` :param eid: Event ID. :type eid: int :param json: list of websites wit the keys: * Websites :type json: array :param uid: User ID :type uid: int :returns JSON Response Object: JSON Response Object containing success or error response. """ for key in SERVICEWEBSITEKEYS: if key not in json: return jsonify(Error='Missing credentials from submission: ' + key), 400 handler = WebsiteHandler() sites = [] website = [] sites = (handler.unpackWebsites(json['websites'])) dao = WebsiteDAO() if not sites: return jsonify(Error='Missing websites for submission: '), 400 website = dao.insertWebsiteToService(sites, sid, uid=uid) try: return jsonify(website), 201 except: return jsonify(Error="Service with sid: " + sid + " not found"), 401
def createService(self, uid, rid, sname, sdescription, sschedule, websites, numbers): """ Creates a new service and adds websites and phones to it. Uses :func:`~app.DAOs.AuditDAO.AuditDAO.getTableValueByIntID` & :func:`~app.DAOs.AuditDAO.AuditDAO.insertAuditEntry` :param uid: The user ID for the creator of the service :type uid: int :param rid: The ID for the room that would provide the service :type rid: int :param sname: The name of the service :type sname: string :param sdescription: A description of the service :type sdescription: string :param sschedule: The service's schedule :type sschedule: string :param websites: Websites to be asociated with the service :type websites: array :param numbers: Phone numbers to be added to the service :type numbers: array :return: results from :func:`~app.DAOs.ServiceDAO.ServiceDAO.getServiceByID` used with the new service's sid. """ cursor = self.conn.cursor() # Build the query to create an event entry. try: audit = AuditDAO() tablename = "services" pkeys = ["rid", "sname"] oldValue = audit.getTableValueByPkeyPair(table=tablename, pkeyname1=pkeys[0], pkeyname2=pkeys[1], pkeyval1=rid, pkeyval2=sname, cursor=cursor) query = sql.SQL( "insert into {table1} ({insert_fields})" "values (%s, %s, %s, %s, %s) " "ON CONFLICT (rid,sname) " "do update set sdescription=%s, sschedule=%s, isdeleted=false " "where services.isdeleted = true " "returning {keys} ").format(table1=sql.Identifier('services'), insert_fields=sql.SQL(',').join([ sql.Identifier('rid'), sql.Identifier('sname'), sql.Identifier('sdescription'), sql.Identifier('sschedule'), sql.Identifier('isdeleted'), ]), keys=sql.SQL(',').join([ sql.Identifier('sid'), sql.Identifier('rid'), sql.Identifier('sname'), sql.Identifier('sdescription'), sql.Identifier('sschedule'), sql.Identifier('isdeleted'), ])) cursor.execute( query, (int(rid), str(sname), str(sdescription), str(sschedule), False, str(sdescription), str(sschedule))) result = cursor.fetchone() try: sid = result[0] except: return jsonify(Error='Room with service already exists '), 401 newValue = audit.getTableValueByPkeyPair(table=tablename, pkeyname1=pkeys[0], pkeyname2=pkeys[1], pkeyval1=rid, pkeyval2=sname, cursor=cursor) if not oldValue: changeType = audit.INSERTVALUE else: changeType = audit.UPDATEVALUE audit.insertAuditEntry(changedTable=tablename, changeType=changeType, oldValue=oldValue, newValue=newValue, uid=uid, cursor=cursor) for site in websites: website = (WebsiteDAO.addWebsite(self, url=site['url'], cursor=cursor, uid=uid)) if website is None: return jsonify(Error='Website problem ' + site['url'] + " Not valid"), 400 else: WebsiteDAO().addWebsitesToService( sid=sid, wid=website[0], wdescription=site['wdescription'], cursor=cursor, uid=uid) for num in numbers: phone = PhoneDAO.addPhone(self, pnumber=num['pnumber'], ptype=num['ptype'], cursor=cursor, uid=uid) PhoneDAO().addPhoneToService(sid=sid, pid=phone[0], cursor=cursor, uid=uid) # Commit changes if no errors occur. self.conn.commit() return result except errors.UniqueViolation as badkey: return jsonify(Error="Room has service with the same name" + str(badkey)), 401
def createEvent(self, ecreator, roomid, etitle, edescription, estart, eend, tags, photourl, websites): """ Create an Event from the information provided. Uses: * :func:`~app.DAOs.PhotoDAO.PhotoDAO.insertPhoto` * :func:`~app.DAOs.TagDAO.TagDAO.tagEvent` * :func:`~app.DAOs.WebsiteDAO.WebsiteDAO.addWebsite` * :func:`~app.DAOs.WebsiteDAO.WebsiteDAO.addWebsitesToEvent` :param ecreator: the event creator's UID. :type ecreator: int :param roomid: the rid of the room in which the event will take place. :type roomid: int :param etitle: the event's title. :type etitle: str :param edescription: the event's description. :type edescription: str :param estart: the event's start timestamp. :type estart: str :param eend: the event's end timestamp, which must be greater than the start timestamp. :type eend: str :param tags: a list of integers corresponding to tag IDs :type tags: list of int :param photourl: url of a photo to be related to the event. Can be empty. :type photourl: str :param websites: a list of dictionaries containing website urls and wdescriptions. can be empty. :type websites: list of dicts :return Tuple: SQL result of Query as a tuple. """ cursor = self.conn.cursor() # Insert photo into table if it does not exist, then get the photoid. photoid = PhotoDAO().insertPhoto(photourl=photourl, uid=ecreator, cursor=cursor)[0] # Build the query to create an event entry. query = sql.SQL( "insert into {table1} ({insert_fields})" "values (%s, %s, %s, %s, %s, %s, CURRENT_TIMESTAMP, %s, %s, %s) " "returning {pkey1}").format(table1=sql.Identifier('events'), insert_fields=sql.SQL(',').join([ sql.Identifier('ecreator'), sql.Identifier('roomid'), sql.Identifier('etitle'), sql.Identifier('edescription'), sql.Identifier('estart'), sql.Identifier('eend'), sql.Identifier('ecreation'), sql.Identifier('estatus'), sql.Identifier('estatusdate'), sql.Identifier('photoid') ]), pkey1=sql.Identifier('eid')) # Try to insert the event into the database, catch if any event is duplicated. try: cursor.execute( query, (int(ecreator), int(roomid), str(etitle), str(edescription), str(estart), str(eend), 'active', None, photoid)) result = cursor.fetchone() eid = result[0] except errors.UniqueViolation as unique_error: self.conn.close() return errors.UniqueViolation( "An event with the same name, in the same room, " "that starts at the same time, already exists in the system.") # Once the event is created, tag it with the list of tags provided. Catch any bad tags. try: for tag in tags: TagDAO().tagEvent(eid=eid, tid=tag, cursor=cursor) except errors.ForeignKeyViolation as fk_error: self.conn.close() return fk_error # Once tagged, insert the websites, if any, that do not already exist, and relate them to the event. if websites is not None: try: for website in websites: wid = WebsiteDAO().addWebsite(url=website['url'], cursor=cursor, uid=ecreator) WebsiteDAO().addWebsitesToEvent( eid=eid, wid=wid[0], wdescription=website['wdescription'], cursor=cursor, uid=ecreator) # Do not know if this is the right error to expect. except TypeError as e: self.conn.close() return e except ValueError as e: self.conn.close() return e # Commit changes if no errors occur. self.conn.commit() self.conn.close() return result