def register(): # If fields correct: if is_request_json_field_exist('username') and is_request_json_field_exist( 'password'): username = request.json.get('username', None) password = request.json.get('password', None) role = 'user' if is_request_json_field_exist('role'): role = request.json.get('role') # TODO: Add check for ip4_address field exist ip4_address = request.headers.environ.get( 'REMOTE_ADDR', None) # check HTTP_X_FORWARDED_FOR # Check username already exist try: User.objects.get(username=username) # If username does not exist create new user: except DoesNotExist: refresh_token = create_refresh_token(identity=username) user = User(username=username, role=role, ip4_address=ip4_address, refresh_token=refresh_token) user.hash_password(password=password) user.save() access_token = create_access_token(identity=username) return jsonify(userId=str(user.id), username=username, access_token=access_token, refresh_token=refresh_token), 201 # If username exist - abort with 409 error g.custom_http_error_msg = 'User already exist' abort(409) # If something wrong with fields abort(400)
def create_new_def_record(): """ Create new defectura record and return it ID """ record = DefecturaCard() if is_request_json_field_exist('drugName') and is_request_json_field_exist( 'employeeName'): record.drug_name = request.json['drugName'] record.employee_name = request.json['employeeName'] if is_request_json_field_exist('comment'): record.comment = request.json['comment'] record.save() return jsonify({'msg': 'OK', 'recordId': str(record.id)}), 201 abort(400)
def create_employee(): """ Create new employee """ employee = Employee() if is_request_json_field_exist( 'firstName') and is_request_json_field_exist('lastName'): employee.first_name = request.json['firstName'] employee.last_name = request.json['lastName'] if is_request_json_field_exist('patronymic'): employee.patronymic = request.json['patronymic'] if is_request_json_field_exist('prefferedTime'): employee.preffered_time = request.json['prefferedTime'] employee.save() return jsonify({'msg': 'OK', 'employeeId': str(employee.id)}), 201 abort(400)
def add_news_post(): """ Return created record ID """ if is_request_json_field_exist( 'postBody') and request.json['postBody'] != '': post = NewsPost() post.post_body = request.json['postBody'] post.save() return jsonify({"msg": "OK", "postId": str(post.id)}), 201 abort(400)
def login(): # Check if fields correct if is_request_json_field_exist('username') and is_request_json_field_exist( 'password'): username = request.json.get('username', None) password = request.json.get('password', None) # Try to get user from DB by username or return 404: user = User.objects.get_or_404(username=username) # Check user password and if it correct return access and refresh tokens if user.check_password(password): access_token = create_access_token(identity=username) refresh_token = create_refresh_token(identity=username) user.refresh_token = refresh_token user.save() return jsonify(access_token=access_token, refresh_token=refresh_token) # If password wrong return 401 abort(401) # If something wrong with fields abort(400)
def update_news_post(news_post_id): """ Update record by it ID and return OK """ if is_request_json_field_exist( 'postBody') and request.json['postBody'] != '': post = NewsPost.objects.get_or_404(id=news_post_id) if post.post_body != request.json['postBody']: post.post_body = request.json['postBody'] post.date_edited = datetime.utcnow() post.save() return jsonify({"msg": "OK"}) return jsonify({"msg": "OK"}) abort(400)
def edit_employee(employee_id): """ Edit employee """ employee = Employee.objects.get_or_404(id=employee_id) if is_request_json_field_exist( 'firstName') and is_request_json_field_exist('lastName'): employee.first_name = request.json['firstName'] employee.last_name = request.json['lastName'] if is_request_json_field_exist('patronymic'): employee.patronymic = request.json['patronymic'] if is_request_json_field_exist('prefferedTime'): employee.preffered_time = request.json['prefferedTime'] if is_request_json_field_exist('active'): employee.active = request.json['active'] if is_request_json_field_exist('dateOfDismissal'): employee.date_of_dismissal = request.json['dateOfDismissal'] if is_request_json_field_exist('vacationDays'): employee.vacation_days = request.json['vacationDays'] employee.date_edited = datetime.utcnow() employee.save() return jsonify({'msg': 'OK'}) abort(400)
def generate_schedule(): """ Generate new schedule from json data from request year and month - of month that need to be generated, employeeIds - employees ID's shiftStartTime - Time to start employee's work day firstDateOfShift - What is the first employee shift date in a month isFirstDayOfShift, isSecondDayOfShift if firstDateOfShift get in first day of 2\2 shift, or second day shiftType - type of employee shift: 22 (2/2) - 2 work days, 2 weekends, 52 (5/2) - 5 work days, 2 weekends vacationStartDate, vacationEndDate: start and end date of vacation """ if is_request_json_field_exist('employeeIds') and is_request_json_field_exist('year') \ and is_request_json_field_exist('month'): year = request.json['year'] month = request.json['month'] date_object = datetime(year=year, month=month, day=1) days_in_month = last_day_of_month(date_object).day for employee_id, info in request.json['employeeIds'].items(): # Get employee from DB employee = Employee.objects.get_or_404(id=employee_id) # If all work, then update employee document with new data can_update_employee_document = False # Make variables from json fields # TODO: Implement checks if this fields exists shift_type = info['shiftType'] shift_start_time = info['shiftStartTime'] first_date_of_shift = datetime.strptime(info['firstDateOfShift'], '%Y-%m-%d').date() is_first_day_of_shift = info['isFirstDayOfShift'] is_second_day_of_shift = info['isSecondDayOfShift'] # If vacation in this month vacation_start_date = datetime.strptime(info['vacationStartDate'], '%Y-%m-%d').date() vacation_end_date = datetime.strptime(info['vacationEndDate'], '%Y-%m-%d').date() # Calc vacation days vacation_days = (vacation_end_date - vacation_start_date).days # Set day counter to zero day_counter = timedelta(days=0) # Get number of day in week to set day counter for 5/2 shift: if shift_type == 52: day_counter = timedelta(days=datetime(year, month, first_date_of_shift.day).weekday()) # For day in range of days in month plus one day - because range func start count from zero for day in range(days_in_month): day = day + 1 # Skip all days before first date of shift or vacation if first_date_of_shift < vacation_start_date: if datetime(year, month, day).date() < first_date_of_shift: continue # If day between vacation start/end dates if vacation_start_date <= datetime(year, month, day).date() <= vacation_end_date: schedule = Schedule() schedule.employee = employee schedule.shift_start_time = 0 schedule.vacation_day = datetime(year=year, month=month, day=day).date() schedule.save() # if employee in 5/2 shift if shift_type == 52: # Set date counter to next week day from last vacation day day_counter = timedelta(days=(datetime(year, month, vacation_end_date.day) + timedelta(days=1)).weekday()) # if employee in 2/2 shift if shift_type == 22: # Set date counter to zero for first work day day_counter = timedelta(days=0) continue # If shift type 2/2 if shift_type == 22: # If firs day of 2/2 shift schedule: if is_first_day_of_shift: if day_counter == timedelta(days=0) or day_counter < timedelta(days=2): schedule = Schedule() schedule.employee = employee schedule.work_day = datetime(year=year, month=month, day=day).date() schedule.shift_start_time = shift_start_time schedule.save() day_counter += timedelta(days=1) # If day counter equally 4, then this last weekend day - set it to zero if day_counter == timedelta(days=4): day_counter = timedelta(days=0) # If day counter equally or bigger than 2, then weekend starts: elif day_counter >= timedelta(days=2): day_counter += timedelta(days=1) # If second day of 2/2 shift schedule: elif is_second_day_of_shift: # Set day counter to 2, because this last work day of 2/2 shift day_counter = timedelta(days=2) schedule = Schedule() schedule.employee = employee schedule.work_day = datetime(year=year, month=month, day=day).date() schedule.shift_start_time = shift_start_time schedule.save() day_counter += timedelta(days=1) # Reverse to first day of shift flow is_second_day_of_shift = False is_first_day_of_shift = True # If shift type 5/2 if shift_type == 52: # If day counter < 5 - then this work days if day_counter.days < 5: schedule = Schedule() schedule.employee = employee schedule.work_day = datetime(year=year, month=month, day=day).date() schedule.shift_start_time = shift_start_time schedule.save() day_counter += timedelta(days=1) # If day counter >= 5 - then weekends start elif day_counter.days >= 5: day_counter += timedelta(days=1) # If day counter equals 7 - weekends end, start new work week if day_counter.days == 7: day_counter = timedelta(days=0) can_update_employee_document = True else: pass # If all done - update employee document if can_update_employee_document: # Change vacation days and set vacation dates in employee document: employee.update(add_to_set__vacation_dates=[employee.VacationDates( vacation_start_date=vacation_start_date, vacation_end_date=vacation_end_date )]) employee.vacation_days -= vacation_days employee.date_edited = datetime.utcnow() employee.save() return 'DONE!' abort(400)