def temp(): try: data = request.json if ProjectTable.select().where( (ProjectTable.name == data['name']) & (ProjectTable.date_start == data['date_start']) & (ProjectTable.date_end == data['date_end'])).exists(): message = ResponseClass.project_existing.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: proj_data = { 'name': data['name'], 'description': data['description'], 'date_start': data['date_start'], 'date_end': data['date_end'], 'project_manager': data['project_manager'] } proj_object = ProjectTable(**proj_data) proj_object.save() team_member_data = data['team_member'] project_lead_data = data['project_lead'] for member in team_member_data: team_member_object = {} team_member_object['project'] = proj_object.id team_member_object['team_type'] = member.team_type team_member_array = member.members_id for members in team_member_array: team_member_array_object = {} team_member_array_object['team_member'] = members team_member_object.append(team_member_array_object) save_team_member_object = ProjectUser(**team_member_object) save_team_member_object.save() for lead in project_lead_data: team_lead_object = {} team_lead_object['project'] = lead.id team_lead_object['team_type'] = lead.team_type team_lead_object['team_lead'] = lead.lead_id save_project_lead_object = ProjectLead(**team_lead_object) save_project_lead_object.save() message = { 'name': data['name'], 'description': data['description'] } obj = ResponseClass.creationsuccess_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_201_CREATED except Exception as e: print(e)
def createProject(): return_response = validate_user() return_data = json.loads(return_response.text) if return_response.status_code == 200: authority = authorisation(return_data['User_id']) if authority: data = request.json if ProjectTable.select().where( (ProjectTable.name == data['name']) & (ProjectTable.date_start == data['date_start']) & (ProjectTable.date_end == data['date_end'])).exists(): message = ResponseClass.project_existing.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: proj_data = { 'name': data['name'], 'description': data['description'], 'date_start': data['date_start'], 'date_end': data['date_end'] } proj_object = ProjectTable(**proj_data) proj_object.save() team_member_data = data['team_member'] for member in team_member_data: team_member_object = {} team_member_object['project'] = proj_object.id team_member_object['team_member'] = member team_member_object['team_lead'] = 1 save_object = ProjectUser(**team_member_object) save_object.save() message = { 'name': data['name'], 'description': data['description'] } obj = ResponseClass.creationsuccess_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_201_CREATED else: client_message = ResponseClass.authority_exception.value obj = ResponseClass.creationerror_message(client_message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: obj = json.dumps(return_data) ret_status = return_response.status_code client_response = Response(obj, mimetype='application/json') return client_response, ret_status
def getProjects(): return_response = validate_user() data = json.loads(return_response.text) if return_response.status_code == 200: proj_query = ProjectTable.select().join(ProjectUser).order_by( -ProjectTable.created_at).where( ProjectUser.team_member == data['User_id']) if proj_query.exists(): project_list = [] for proj in proj_query: projobj = {} projobj['title'] = proj.name projobj['description'] = proj.description projobj['created time'] = proj.created_at projobj['proj_id'] = proj.id project_list.append(projobj) obj = convToJson(project_list) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_200_OK else: client_message = ResponseClass.notes_absent.value obj = ResponseClass.creationerror_message(client_message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_404_NOT_FOUND else: obj = json.dumps(data) ret_status = return_response.status_code client_response = Response(obj, mimetype='application/json') return client_response, ret_status
def getAllProjects(): return_response = validate_user() data = json.loads(return_response.text) if return_response.status_code == 200: authority = authorisation(data['User_id']) if authority: proj_query = ProjectTable.select() project_list = [] for proj in proj_query: projobj = {} projobj['title'] = proj.name projobj['description'] = proj.description projobj['created time'] = proj.created_at projobj['proj_id'] = proj.id project_list.append(projobj) obj = convToJson(project_list) resp = Response(obj, mimetype='application/json') return resp, status.HTTP_200_OK else: client_message = ResponseClass.authority_exception.value obj = ResponseClass.creationerror_message(client_message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: obj = json.dumps(data) ret_status = return_response.status_code client_response = Response(obj, mimetype='application/json') return client_response, ret_status
def getTeamMembers(): return_response = validate_user() data = json.loads(return_response.text) if return_response.status_code == 200: authority = authorisation(data['User_id']) if authority: user_list = [] team_type_query = TeamType.select() for team in team_type_query: user_obj = {} user_obj['team_type'] = team.team_type user_obj['team_id'] = team.id user_query = User.select().where(User.team_type == team.id) team_list = [] for user in user_query: team_obj = {} team_obj['name'] = user.username team_obj['user_id'] = user.id team_obj['designation'] = user.designation team_list.append(team_obj) user_obj['members'] = team_list user_list.append(user_obj) obj = convToJson(user_list) resp = Response(obj, mimetype='application/json') return resp, status.HTTP_200_OK else: client_message = ResponseClass.authority_exception.value objecto = ResponseClass.creationerror_message(client_message) client_response = Response(objecto, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: obj = json.dumps(data) ret_status = return_response.status_code client_response = Response(obj, mimetype='application/json') return client_response, ret_status
def getUsers(): return_response = validate_user() data = json.loads(return_response.text) if return_response.status_code == 200: authority = authorisation(data['User_id']) if authority: user_list = [] for user in User.select(): userobj = {} userobj['username'] = user.username userobj['designation'] = user.designation userobj['user_id'] = user.id user_list.append(userobj) obj = convToJson(user_list) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_200_OK else: client_message = ResponseClass.authority_exception.value objecto = ResponseClass.creationerror_message(client_message) client_response = Response(objecto, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: obj = json.dumps(data) ret_status = return_response.status_code client_response = Response(obj, mimetype='application/json') return client_response, ret_status
def validate_user(): try: reqheader = request.headers.get('User_token') if (reqheader == None): message = ResponseClass.invalid_token.value response = ResponseClass.creationerror_message(message) return response, status.HTTP_400_BAD_REQUEST else: url = config.EXT_API_AUTH headers = {'Session_token': reqheader} delegate_data = {'Content-Type': 'application/json'} return_response = requests.get(url, headers=headers) return return_response except Exception as e: print(e) message = ResponseClass.invalid_token.value response = ResponseClass.creationerror_message(message) return response, status.HTTP_400_BAD_REQUEST
def wrapper(*args, **kw): try: reqheader = request.headers.get('User_token') if (reqheader == None): message = ResponseClass.invalid_token.value response = ResponseClass.creationerror_message(message) return response, status.HTTP_400_BAD_REQUEST else: squery = SessionTable.get(SessionTable.session_ID == reqheader) user_data = User.get(User.id == squery.user_id) data = { 'username': user_data.username, 'email-ID': user_data.email } except Exception as e: print(e) message = ResponseClass.invalid_token.value response = ResponseClass.creationerror_message(message) return response, status.HTTP_400_BAD_REQUEST return f(user_data.id, *args, **kw)
def wrapper(*args, **kw): try: data = request.json validate(request.json, schema_name) except jsonschema.ValidationError as ex: print(ex.relative_schema_path) try: err = ex.absolute_path[0] + " is invalid " except Exception as e: err = ex.message return (ResponseClass.creationerror_message(err), status.HTTP_400_BAD_REQUEST) except jsonschema.SchemaError as e: errmessage = e.message return (ResponseClass.creationerror_message(errmessage), status.HTTP_400_BAD_REQUEST) except Exception as e: print(e) message = ResponseClass.signup_exception.value return (ResponseClass.creationerror_message(message), status.HTTP_400_BAD_REQUEST) return f(*args, **kw)
def logout(): reqheader = request.headers.get('User_token') if (reqheader == None): message = ResponseClass.invalid_token.value response = ResponseClass.creationerror_message(message) return response, status.HTTP_400_BAD_REQUEST else: data = request.json url = config.EXT_API_LOGOUT headers = {'Session_token': reqheader} return_response = requests.delete(url, headers=headers) message = json.loads(return_response.text) status = return_response.status_code obj = json.dumps(message) client_response = Response(obj, mimetype='application/json') return client_response, status
def getHistory(): return_response = validate_user() return_data = json.loads(return_response.text) if return_response.status_code == 200: log_query = LogTable.select().where( LogTable.user == return_data['User_id']).paginate(1, 50) if log_query.exists(): log_hour_query = LogTable.select().order_by( LogTable.logged_time.desc()).where( LogTable.user == return_data['User_id']).paginate(1, 50) hour_list = [] cummulative_hour_list = [] member_list = [] hour = 0 for cummulative_hour in log_hour_query: hour = hour + cummulative_hour.log_hours cummulative_hour_list.append(hour) for hour in log_hour_query: proj_query = ProjectTable.get( ProjectTable.id == hour.project_id) hour_array = {} member_data_array = {} hour_array['hour'] = hour.log_hours hour_array['jobtype_ID'] = hour.job_type_id hour_array['timestamp'] = hour.logged_time hour_array['datestamp'] = hour.logged_date hour_array['description'] = hour.description hour_array['proj_id'] = hour.project_id hour_array['proj_name'] = proj_query.name hour_list.append(hour_array) member_data_array['job_type_hours'] = hour_list member_data_array['total_hours'] = cummulative_hour_list member_list.append(member_data_array) return_object = projectservice.convToJson(member_list) return return_object else: message = ResponseClass.user_query_failure.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: obj = json.dumps(return_data) ret_status = return_response.status_code client_response = Response(obj, mimetype='application/json') return client_response, ret_status
def projectDetails(): return_response = validate_user() return_data = json.loads(return_response.text) if return_response.status_code == 200: authority = authorisation(return_data['User_id']) if authority: data = request.json project_query = LogTable.select().where( LogTable.project == data['proj_id']) if project_query.exists(): user_query = ProjectUser.select().where( ProjectUser.project == data['proj_id']) if user_query.exists(): job_type_list = [] user_list = [] member_list = [] for user in user_query: user_object = {} user_object = user.team_member_id user_list.append(user_object) for member_data in user_list: member_data_query = User.select().where( User.id == member_data).paginate(1, 15) log_hour_query = LogTable.select().where( (LogTable.user == member_data) & (LogTable.project == data['proj_id'])).paginate( 1, 15) hour_list = [] cummulative_hour_list = [] hour_job_list = [] for hour in log_hour_query: hour_array = {} hour_array['hour'] = hour.logged_hours hour_array['jobtype_ID'] = hour.job_type_id hour_array['timestamp'] = hour.logged_time hour_list.append(hour_array) hour = 0 for cummulative_hour in log_hour_query: hour = hour + cummulative_hour.log_hours cummulative_hour_list.append(hour) for member in member_data_query: member_data_array = {} member_data_array['name'] = member.username member_data_array[ 'designation'] = member.designation member_data_array['user_id'] = member.id member_data_array['job_type_hours'] = hour_list member_data_array[ 'total_hours'] = cummulative_hour_list member_list.append(member_data_array) return_object = projectservice.convToJson(member_list) client_response = Response(return_object, mimetype='application/json') return client_response, status.HTTP_200_OK else: message = ResponseClass.user_query_failure.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: message = ResponseClass.project_query_failure.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: client_message = ResponseClass.authority_exception.value obj = ResponseClass.creationerror_message(client_message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: obj = json.dumps(return_data) ret_status = return_response.status_code client_response = Response(obj, mimetype='application/json') return client_response, ret_status
def editLog(): return_response = validate_user() return_data = json.loads(return_response.text) if return_response.status_code == 200: data = request.json if ProjectUser.select().where( (ProjectUser.team_member == return_data['User_id']) & (ProjectUser.project == data['proj_id'])).exists(): proj_query = ProjectTable.get(ProjectTable.id == data['proj_id']) log_query = LogTable.select().where( (LogTable.user == return_data['User_id']) & (LogTable.logged_date == data['log_date'])) total_hours = 0 for hours in log_query: total_hours = total_hours + hours.log_hours if total_hours < 24: if data['log_hours'] < 24: time_now = datetime.now() project_expiry = proj_query.date_end date = datetime.strptime(project_expiry, '%Y-%m-%d %H:%M:%S') if date >= time_now: check_log_query = LogTable.select().where( (LogTable.logged_date == data['log_date']) & (LogTable.user == return_data['User_id']) & (LogTable.project == data['proj_id']) & (LogTable.job_type == data['jobtype_id'])) if check_log_query.exists(): update_log_query = LogTable.select().where( (LogTable.logged_date == data['log_date']) & (LogTable.user == return_data['User_id']) & (LogTable.project == data['proj_id']) & (LogTable.job_type == data['jobtype_id']) ).get() update_log = { 'description': data['description'], 'log_hours': data['log_hours'] } update_log_object = LogTable.update( **update_log).where( LogTable.id == update_log_query.id) update_log_object.execute() last_update = LogTable.get( LogTable.id == update_log_query.id) last_update.modified_date = datetime.now() last_update.save() message = ResponseClass.re_edit.value obj = ResponseClass.creationsuccess_message( message) client_response = Response( obj, mimetype='application/json') return client_response, status.HTTP_200_OK else: log_data = { 'project': data['proj_id'], 'user': return_data['User_id'], 'job_type': data['jobtype_id'], 'description': data['description'], 'logged_date': data['log_date'], 'logged_hours': data['log_hours'] } log_object = LogTable(**log_data) log_object.save() message = ResponseClass.editsuccess.value obj = ResponseClass.creationsuccess_message( message) client_response = Response( obj, mimetype='application/json') return client_response, status.HTTP_200_OK else: message = ResponseClass.log_expiry.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: message = ResponseClass.day_threshold_error.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: message = ResponseClass.log_threshold_error.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: message = ResponseClass.log_failure.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: obj = json.dumps(return_data) ret_status = return_response.status_code client_response = Response(obj, mimetype='application/json') return client_response, ret_status
def logHours(): try: return_response = validate_user() return_data = json.loads(return_response.text) if return_response.status_code == 200: data = request.json print(data) if ProjectUser.select().where( (ProjectUser.team_member == return_data['User_id']) & (ProjectUser.project == data['proj_id'])).exists(): proj_query = ProjectTable.get( ProjectTable.id == data['proj_id']) log_query = LogTable.select().where( (LogTable.user == return_data['User_id']) & (LogTable.logged_date == data['log_date'])) total_hours = 0 for hours in log_query: total_hours = total_hours + hours.log_hours if total_hours < 24: if data['log_hours'] < 24: time_now = datetime.now() project_expiry = proj_query.ext_date_end date = datetime.strptime(project_expiry, '%Y-%m-%d') if date >= time_now: log_data = { 'project': data['proj_id'], 'user': return_data['User_id'], 'job_type': data['jobtype_id'], 'description': data['description'], 'logged_date': data['log_date'], 'logged_hours': data['log_hours'], 'team_type': "1" } log_object = LogTable(**log_data) log_object.save() obj = projectservice.convToJson(log_data) client_response = Response( obj, mimetype='application/json') return client_response, status.HTTP_200_OK else: message = ResponseClass.log_expiry.value obj = ResponseClass.creationerror_message(message) client_response = Response( obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: message = ResponseClass.day_threshold_error.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: message = ResponseClass.log_threshold_error.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: message = ResponseClass.log_failure.value obj = ResponseClass.creationerror_message(message) client_response = Response(obj, mimetype='application/json') return client_response, status.HTTP_400_BAD_REQUEST else: obj = json.dumps(return_data) ret_status = return_response.status_code client_response = Response(obj, mimetype='application/json') return client_response, ret_status except Exception as e: print(e) message = ResponseClass.project_details_exception.value obj = ResponseClass.creationerror_message(message) resp = Response(obj, mimetype='application/json') return resp, status.HTTP_400_BAD_REQUEST