def postRisk(data): i = impactCrud.getImpact(data['impact']) p = probabilityCrud.getProbability(data['probability']) ra = riskAreaCrud.getRiskArea(data['riskArea']) rt = riskTypeCrud.getRiskType(data['riskType']) ro = userCrud.getUser(data['owner']) row = risk(title = data['title'], desc = data['desc'], owner = data['owner'], riskOwner = ro, tenant_uuid = session['tenant_uuid'], uuid = UUID.uuid4(), impact_uuid=i.uuid, impact=i, probability_uuid=p.uuid, probability=p, riskArea_uuid=ra.uuid, riskArea=ra, riskType_uuid=rt.uuid, riskType=rt, created=datetime.now(), createdBy=session['user_uuid']) try: db.session.add(row) db.session.commit() postLog(table='risk', uuid=unicode(row.uuid)) return {'success': 'Risk added'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='risk') return {'error': 'Risk already exist'} else: errorLog(unicode(E), table='risk') return {'error': unicode(E)}
def postImpact(data): row = impact(title=data['title'], cost=data['cost'], schedule=data['schedule'], requirements=data['requirements'], legal=data['legal'], other=data['other'], value=data['value'], tenant_uuid=session['tenant_uuid'], uuid=UUID.uuid4(), created=datetime.now(), createdBy=session['user_uuid']) try: db.session.add(row) db.session.commit() postLog(table='impact', uuid=unicode(row.uuid)) return {'success': 'Impact added'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='impact') return {'error': 'Impact already exist'} else: errorLog(unicode(E), table='impact') return {'error': unicode(E)}
def postRating(data): i = impactCrud.getImpact(data['impact']) p = probabilityCrud.getProbability(data['probability']) row = rating(value = data['value'], desc = data['desc'], tenant_uuid = session['tenant_uuid'], uuid = UUID.uuid4(), impact_uuid=i.uuid, impact=i, probability_uuid=p.uuid, probability=p, created=datetime.now(), createdBy=session['user_uuid']) try: db.session.add(row) db.session.commit() postLog(table='rating', uuid=unicode(row.uuid)) return {'success': 'Rating added'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='rating') return {'error': 'Rating already exist'} else: errorLog(unicode(E), table='rating') return {'error': unicode(E)}
def putRating(data, uuid): row = getRating(uuid) i = impactCrud.getImpact(data['impact']) p = probabilityCrud.getProbability(data['probability']) changes = compareDict(row=row, data=data)['modified'] if i != row.impact: changes['impact'] = (row.impact.uuid,i.uuid) if p != row.probability: changes['probability'] = (row.probability.uuid,p.uuid) row.value = data['value'] row.desc = data['desc'] row.modified = datetime.now() row.modifiedBy = session['user_uuid'] row.impact_uuid = i.uuid row.impact = i row.probability_uuid = p.uuid row.probability = p try: db.session.commit() putLog(table='rating', uuid=unicode(uuid), changes=changes) return {'success': 'Rating updated'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='rating') return {'error': 'Rating already exist'} else: errorLog(unicode(E), table='rating') return {'error': unicode(E)}
def putSubRegion(data, uuid): row = getSubRegion(uuid) reg = regionCrud.getRegion(data['region']) changes = compareDict(row=row, data=data)['modified'] if reg != row.region: changes['region'] = (row.region.uuid,r.uuid) row.title = data['title'] row.abbr = data['abbr'] row.modified = datetime.now() row.modifiedBy = session['user_uuid'] row.region = reg row.region_uuid = reg.uuid try: db.session.commit() putLog(table='subRegion', uuid=unicode(uuid), changes=changes) return {'success': 'Sub Region updated'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='subRegion') return {'error': 'Sub Region already exist'} else: errorLog(unicode(E), table='subRegion') return {'error': unicode(E)}
def putValueChain(data, uuid): row = getValueChain(uuid) r = valueChainAreaCrud.getValueChainArea(data['valueChainArea']) changes = compareDict(row=row, data=data)['modified'] if r != row.valueChainArea: changes['valueChainArea'] = (row.valueChainArea.uuid, r.uuid) row.title = data['title'] row.desc = data['desc'] row.modified = datetime.now() row.modifiedBy = session['user_uuid'] row.valueChainArea_uuid = r.uuid row.valueChainArea = r try: db.session.commit() putLog(table='valueChain', uuid=unicode(uuid), changes=changes) return {'success': 'Value Chain updated'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='valueChain') return {'error': 'Value Chain already exist'} else: errorLog(unicode(E), table='valueChain') return {'error': unicode(E)}
def postTreatment(data): t = treatmentTypeCrud.getTreatmentType(data['treatmentType']) r = riskResponseCrud.getRiskResponse(data['riskResponse']) row = treatment(title=data['title'], desc=data['desc'], tenant_uuid=session['tenant_uuid'], uuid=UUID.uuid4(), treatmentType_uuid=t.uuid, treatmentType=t, riskResponse_uuid=r.uuid, riskResponse=r, created=datetime.now(), createdBy=session['user_uuid']) try: db.session.add(row) db.session.commit() postLog(table='treatment', uuid=unicode(row.uuid)) return {'success': 'Treatment added'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='treatment') return {'error': 'Treatment already exist'} else: errorLog(unicode(E), table='treatment') return {'error': unicode(E)}
def putZone(data, uuid): row = getZone(uuid) c = countryCrud.getCountry(data['country']) changes = compareDict(row=row, data=data)['modified'] if c != row.country: changes['country'] = (row.country.uuid, r.uuid) row.title = data['title'] row.abbr = data['abbr'] row.modified = datetime.now() row.modifiedBy = session['user_uuid'] row.country = c row.country_uuid = c.uuid try: db.session.commit() putLog(table='zone', uuid=unicode(uuid), changes=changes) return {'success': 'Zone updated'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='zone') return {'error': 'Zone already exist'} else: errorLog(unicode(E), table='zone') return {'error': unicode(E)}
def putTreatment(data, uuid): row = getTreatment(uuid) t = treatmentTypeCrud.getTreatmentType(data['treatmentType']) r = riskResponseCrud.getRiskResponse(data['riskResponse']) # Discover changes to row changes = compareDict(row=row, data=data)['modified'] if t != row.treatmentType: changes['treatmentType'] = (row.treatmentType.uuid, t.uuid) if r != row.riskResponse: changes['riskResponse'] = (row.riskResponse.uuid, r.uuid) # Assign changes to row row.title = data['title'] row.desc = data['desc'] row.modified = datetime.now() row.modifiedBy = session['user_uuid'] row.riskResponse_uuid = r.uuid row.riskResponse = r row.treatmentType_uuid = t.uuid row.treatmentType = t try: db.session.commit() putLog(table='treatment', uuid=unicode(uuid), changes=changes) return {'success': 'Treatment updated'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='treatment') return {'error': 'Treatment already exist'} else: errorLog(unicode(E), table='treatment') return {'error': unicode(E)}
def putRegion(data, uuid): row = getRegion(uuid) changes = compareDict(row=row, data=data)['modified'] row.title = data['title'] row.abbr = data['abbr'] row.modified = datetime.now() row.modifiedBy = session['user_uuid'] if data['subRegions']: for sr in data['subRegions']: row.subRegions.append(subRegionCrud.getSubRegion(unicode(sr))) else: row.subRegions[:] = [] if data['subRegions'] != row.subRegions[:]: changes['subRegions'] = (row.subRegions.uuid, r.uuid) try: db.session.commit() putLog(table='region', uuid=unicode(uuid), changes=changes) return {'success': 'Region updated'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='region') return {'error': 'Region already exist'} else: errorLog(unicode(E), table='region') return {'error': unicode(E)}
def putCausingFactor(data, uuid): row = getCausingFactor(uuid) c = causingFactorTypeCrud.getCausingFactorType(data['causingFactorType']) changes = compareDict(row=row, data=data)['modified'] if c != row.causingFactorType: changes['causingFactorType'] = (row.causingFactorType.uuid, c.uuid) row.title = data['title'] row.desc = data['desc'] row.modified = datetime.now() row.modifiedBy = session['user_uuid'] row.causingFactorType_uuid = c.uuid row.causingFactorType = c try: db.session.commit() putLog(table='causingFactor', uuid=unicode(uuid), changes=changes) return {'success': 'Causing Factor updated'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='causingFactor') return {'error': 'Causing Factor already exist'} else: errorLog(unicode(E), table='causingFactor') return {'error': unicode(E)}
def putGroup(data, uuid): try: dataDict = { 'name': data['title'], 'desc': data['desc'], 'users': data['users'] } req = authAPI(endpoint='group/' + unicode(uuid), method='put', dataDict=dataDict, token=session['token']) if 'error' in req: return {'error': req['error']} else: grp = getGroup(uuid) changes = compareDict(row=grp, data=data)['modified'] try: currentUsers = [r.uuid for r in grp.users] except: currentUsers = [] try: newUsers = [ user.query.filter_by(uuid=unicode(r)).first().uuid for r in data['users'] ] except: newUsers = [] if currentUsers != newUsers: changes['users'] = (currentUsers, newUsers) grp.title = data['title'] grp.desc = data['desc'] grp.users = [ user.query.filter_by(uuid=unicode(r)).first() for r in data['users'] ] grp.modified = datetime.now() grp.modifiedBy = session['user_uuid'] try: db.session.commit() putLog(table='group', uuid=unicode(uuid), changes=changes) return {'success': 'Group has been modified'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='group') return {'error': 'Group already exist'} else: errorLog(unicode(E), table='group') return {'error': unicode(E)} except Exception as E: return {'error': unicode(E)}
def deleteRating(uuid): row = getRating(uuid) try: db.session.delete(row) db.session.commit() deleteLog(table='rating', uuid=unicode(uuid)) return {'success': 'Rating deleted'} except Exception as E: errorLog(unicode(E), table='rating') return {'error': unicode(E)}
def deleteCausingFactor(uuid): row = getCausingFactor(uuid) try: db.session.delete(row) db.session.commit() deleteLog(table='causingFactor', uuid=unicode(uuid)) return {'success': 'Causing Factor deleted'} except Exception as E: errorLog(unicode(E), table='causingFactor') return {'error': unicode(E)}
def deleteRiskType(uuid): entry = getRiskType(uuid) try: db.session.delete(entry) db.session.commit() deleteLog(table='riskType', uuid=unicode(uuid)) return {'success': 'Risk Type deleted'} except Exception as E: errorLog(unicode(E), table='riskType') return {'error': unicode(E)}
def activateUser(uuid): try: usr = getUser(uuid) usr.active = True db.session.commit() logEntry('User activated: {}'.format(usr.uuid), table='user') return {'success': 'User has been activated'} except Exception as E: errorLog(unicode(E), table='user') return {'error':unicode(E)}
def deleteValueChainStepType(uuid): entry = getValueChainStepType(uuid) try: db.session.delete(entry) db.session.commit() deleteLog(table='valueChainStepType', uuid=unicode(uuid)) return {'success': 'Value Chain Step Type deleted'} except Exception as E: errorLog(unicode(E), table='valueChainStepType') return {'error': unicode(E)}
def deleteTreatment(uuid): row = getTreatment(uuid) try: req = deleteLog(table='treatment', uuid=unicode(uuid)) db.session.delete(row) db.session.commit() return {'success': 'Treatment deleted'} except Exception as E: errorLog(unicode(E), table='treatment') return {'error': unicode(E)}
def deleteProbability(uuid): entry = getProbability(uuid) try: db.session.delete(entry) db.session.commit() deleteLog(table='probability', uuid=unicode(uuid)) return {'success': 'Probability deleted'} except Exception as E: errorLog(unicode(E), table='probability') return {'error': unicode(E)}
def confirmUser(uuid, tenant_uuid): try: usr = user.query.filter_by(uuid=uuid, tenant_uuid=tenant_uuid).first() usr.confirmed = True db.session.commit() logEntry('User confirmed: {}'.format(usr.uuid), table='user') return {'success':'User confirmed'} except Exception as E: errorLog(unicode(E), table='user') return {'error':unicode(E)}
def deleteEventType(uuid): row = getEventType(uuid) try: db.session.delete(row) db.session.commit() deleteLog(table='eventType', uuid=unicode(uuid)) return {'success': 'Event Type deleted'} except Exception as E: errorLog(unicode(E), table='eventType') return {'error': unicode(E)}
def deleteImpact(uuid): entry = getImpact(uuid) try: db.session.delete(entry) db.session.commit() deleteLog(table='impact', uuid=unicode(uuid)) return {'success': 'Impact deleted'} except Exception as E: errorLog(unicode(E), table='impact') return {'error': unicode(E)}
def deleteStatus(uuid): row = getStatus(uuid) try: db.session.delete(row) db.session.commit() deleteLog(table='status', uuid=unicode(uuid)) return {'success': 'Status deleted'} except Exception as E: errorLog(unicode(E), table='status') return {'error': unicode(E)}
def deleteValueChain(uuid): row = getValueChain(uuid) try: db.session.delete(row) db.session.commit() deleteLog(table='valueChain', uuid=unicode(uuid)) return {'success': 'Value Chain deleted'} except Exception as E: errorLog(unicode(E), table='valueChain') return {'error': unicode(E)}
def deleteRiskResponse(uuid): entry = getRiskResponse(uuid) if entry.treatments: return {'error': 'Risk Response cannot be deleted as long as there are Treatments assigned this Risk Response'} try: db.session.delete(entry) db.session.commit() deleteLog(table='riskResponse', uuid=unicode(uuid)) return {'success': 'Risk Response deleted'} except Exception as E: errorLog(unicode(E), table='riskResponse') return {'error': unicode(E)}
def deleteRegion(uuid): row = getRegion(uuid) if not row.subRegions: try: db.session.delete(row) db.session.commit() deleteLog(table='region', uuid=unicode(uuid)) return {'success': 'Region deleted'} except Exception as E: errorLog(unicode(E), table='region') return {'error': unicode(E)} else: errorLog('Delete attempt without removing Sub Regions', table='region') return {'error': 'You must first remove Sub Regions from Region'}
def deleteCausingFactorType(uuid): entry = getCausingFactorType(uuid) if not entry.causingFactors: try: db.session.delete(entry) db.session.commit() deleteLog(table='causingFactor', uuid=unicode(uuid)) return {'success': 'Causing Factor Type deleted'} except Exception as E: errorLog(unicode(E), table='causingFactor') return {'error': unicode(E)} else: return { 'error': 'Cannot be deleted as there are Causing Factors assigned this Type' }
def deleteValueChainArea(uuid): entry = getValueChainArea(uuid) if entry.valueChains: errorLog('Delete attempt without removing Value Chains', table='valueChainArea') return { 'error': 'Value Chain Area cannot be deleted as long as there are Value Chains assigned this Value Chain Area' } try: db.session.delete(entry) db.session.commit() deleteLog(table='valueChainArea', uuid=unicode(uuid)) return {'success': 'Value Chain Area deleted'} except Exception as E: errorLog(unicode(E), table='valueChainArea') return {'error': unicode(E)}
def putRisk(data, uuid): row = getRisk(uuid) i = impactCrud.getImpact(data['impact']) p = probabilityCrud.getProbability(data['probability']) ra = riskAreaCrud.getRiskArea(data['riskArea']) rt = riskTypeCrud.getRiskType(data['riskType']) ro = userCrud.getUser(data['owner']) changes = compareDict(row=row, data=data)['modified'] if i != row.impact: changes['impact'] = (row.impact.uuid,i.uuid) if p != row.probability: changes['probability'] = (row.probability.uuid,p.uuid) if ra != row.riskArea: changes['riskArea'] = (row.riskArea.uuid,p.uuid) if rt != row.riskType: changes['riskType'] = (row.riskType.uuid,p.uuid) if ro != row.riskOwner: changes['riskOwner'] = (row.riskOwner.uuid,p.uuid) row.title = data['title'] row.desc = data['desc'] row.modified = datetime.now() row.modifiedBy = session['user_uuid'] row.impact_uuid = i.uuid row.impact = i row.probability_uuid = p.uuid row.probability = p row.riskArea_uuid = ra.uuid row.riskArea = ra row.riskType_uuid = rt.uuid row.riskType = rt row.owner = data['owner'] row.riskOwner = ro try: db.session.commit() putLog(table='risk', uuid=unicode(uuid), changes=changes) return {'success': 'Risk updated'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='risk') return {'error': 'Risk already exist'} else: errorLog(unicode(E), table='risk') return {'error': unicode(E)}
def putStatus(data, uuid): row = getStatus(uuid) changes = compareDict(row=row, data=data)['modified'] row.title = data['title'] row.modified = datetime.now() row.modifiedBy = session['user_uuid'] try: db.session.commit() putLog(table='status', uuid=unicode(uuid), changes=changes) return {'success': 'Status updated'} except Exception as E: if 'unique constraint' in unicode(E): errorLog('Unique constraint', table='status') return {'error': 'Status already exist'} else: errorLog(unicode(E), table='status') return {'error': unicode(E)}