Exemplo n.º 1
0
    def patch(self, user_id):

        data = user_id or None

        if data is None or data is ' ':
            return resp_data_invalid('Users', [], msg=MSG_NO_DATA)

        try:
            user = get_user_by_id(user_id)

            user.active = True
            user.save()

        except DoesNotExist as e:
            return resp_does_not_exist('Users', 'Usuário')

        except FieldDoesNotExist as e:
            return resp_exception('Users', description=e.__str__())

        except NotUniqueError:
            return resp_already_exists('Users', 'usuário')

        except ValidationError as e:
            return resp_exception('Users',
                                  msg=MSG_INVALID_DATA,
                                  description=e.__str__()
                                  )

        return resp_ok('Users', MSG_RESOURCE_ACTIVE.format('Usuário'))
    def get(self, rf, month_id):        
        try:
            # Fetch point by rf
            points = Point.objects(
                Q(date__contains=month_id) & Q(rf=rf)
            ).order_by('date')
            
        except DoesNotExist:
            return resp_does_not_exist('PointServices', 'ponto')

        except FieldDoesNotExist as e:
            return resp_exception('PointServices', description=e.__str__())

        except Exception as e:
            return resp_exception('PointServices', description=e.__str__())

        # It will be auxiliar for changing date
        # Convert string to datetime
        staticDate = datetime.strptime(points.first().date, '%Y-%m-%d').date()
        result = {}
        hoursPerDay = 0
        hoursPerMonth = 0
        for point in range(0, points.count()):
            date = datetime.strptime(
                points[point].date, '%Y-%m-%d'
            ).date()

            if (date != staticDate):
                staticDate = date
                hoursPerDay=0
            
            if point%2 == 0:
                punchIn = datetime.strptime(
                    points[point].time, '%H:%M:%S'
                ).hour
            else:
                punchOut = datetime.strptime(
                    points[point].time, '%H:%M:%S'
                ).hour

            if point%2 == 1:
                hoursPerDay = hoursPerDay + (punchOut-punchIn)
                
                # Take work hours per day,
                # Convert datetime to string
                result["Day " + date.strftime('%Y-%m-%d')] = hoursPerDay
                
                hoursPerMonth = hoursPerMonth + punchOut - punchIn
        
        result["Month " + month_id] = hoursPerMonth
        return resp_ok(
            'ReportService',
            MSG_RESOURCE_FETCHED.format('Relatorio'),
            result
        )
        
Exemplo n.º 3
0
def get_order_by_id(order_id: str):
    try:
        return OrderModel.objects.get(id=order_id)

    except DoesNotExist:
        return resp_does_not_exist('Order', 'Pedido')

    except FieldDoesNotExist as e:
        return resp_exception('Order', description=e.__str__())

    except Exception as e:
        return resp_exception('Order', description=e.__str__())
Exemplo n.º 4
0
def get_user(resource, identity=None, object_id=None):
    try:
        if object_id:
            return User.objects.get(id=object_id)
        else:
            return User.objects.get(email=identity)

    except DoesNotExist:
        return resp_does_not_exist(resource, 'usuário')

    except Exception as e:
        return resp_exception(resource, description=e)
Exemplo n.º 5
0
def get_user_by_document(cpf: str):
    try:
        return User.objects.get(cpf=cpf)

    except DoesNotExist:
        return resp_does_not_exist('Users', 'Usuário')

    except FieldDoesNotExist as e:
        return resp_exception('Users', description=e.__str__())

    except Exception as e:
        return resp_exception('Users', description=e.__str__())
    def get(self, user_id: int) -> dict:

        try:
            query = get_user_by_id(user_id)
            result = UserSchema(many=True).dump(query)

        except Exception:
            return resp_does_not_exist('User', "user")

        return resp_ok('User',
                       MSG_RESOURCE_FETCHED.format('User'),
                       data=result)
Exemplo n.º 7
0
def get_user_by_email(email: str):
    try:
        # buscamos todos os usuários da base utilizando o paginate
        return User.objects.get(email=email)

    except DoesNotExist as e:
        return resp_does_not_exist('Users', 'Usuário')

    except FieldDoesNotExist as e:
        return resp_exception('Users', description=e.__str__())

    except Exception as e:
        return resp_exception('Users', description=e.__str__())
Exemplo n.º 8
0
def get_planet_by_id(planet_id: str):
    try:
        # Search planet by id
        return Planet.objects.get(id=planet_id)

    except DoesNotExist as e:
        return resp_does_not_exist('Planets', 'Planet')

    except FieldDoesNotExist as e:
        return resp_exception('Planets', description=e.__str__())

    except Exception as e:
        return resp_exception('Planets', description=e.__str__())
Exemplo n.º 9
0
def get_planet_by_name(name: str):
    try:
        # Search planet by name
        return Planet.objects.get(name=name)

    except DoesNotExist as e:
        return resp_does_not_exist('Planets', 'Planet')

    except FieldDoesNotExist as e:
        return resp_exception('Planets', description=e.__str__())

    except Exception as e:
        return resp_exception('Planets', description=e.__str__())
def get_point_by_id(point_id: int):
    try:
        # Fetch point by rf
        return Point.objects.get(id=point_id)

    except DoesNotExist:
        return resp_does_not_exist('PointServices', 'ponto')

    except FieldDoesNotExist as e:
        return resp_exception('PointServices', description=e.__str__())

    except Exception as e:
        return resp_exception('PointServices', description=e.__str__())
def get_collaborator_by_rf(rf: str):
    try:
        # Fetch collaborator by rf
        return Collaborator.objects.get(rf=rf)

    except DoesNotExist:
        return resp_does_not_exist('CollaboratorServices', 'Colaborador')

    except FieldDoesNotExist as e:
        return resp_exception('ColaboratorServices', description=e.__str__())

    except Exception as e:
        return resp_exception('ColaboratorServices', description=e.__str__())
Exemplo n.º 12
0
 def get(self, *args, **kwargs):
     task_id = kwargs.get("task_id")
     result = export_users.AsyncResult(task_id)
     if result.ready():
         filename = '{}.csv'.format(task_id)
         data = {
             'filename': filename,
             'message': 'Verifique a pasta /tmp',
             'links': "CDN url or static url from flask"
         }
         return resp_ok(
             'Users', _MSG201.format('Usuário'), data=data
         )
     else:
         return resp_does_not_exist(
             'Users', 'The file is not ready. Try again in a few minutes.'
         )
Exemplo n.º 13
0
    def post(self):
        req_data = request.get_json() or None
        schema = UserSchema()

        if req_data is None:
            return resp_data_invalid('Login', [], msg=MSG_NO_DATA)

        data, errors = schema.load(req_data)

        if errors:
            return resp_data_invalid('Login', errors)

        try:
            usuarioList = UsuarioModel.objects.get(cpf=data['cpf'],
                                                   password=data['password'])
            return resp_ok('Login', MSG_SUCCESS,
                           json.loads(usuarioList.to_json()))
        except DoesNotExist:
            return resp_does_not_exist('Login', MSG_PASSWORD_OR_CPF_INVALID)
        except Exception as e:
            return resp_exception('Login', description=e.__str__())
Exemplo n.º 14
0
def test_resp_does_not_exist_response():
    description = 'Some description'
    resp = resp_does_not_exist('pytest', description)
    message = resp.json.get('message')
    assert message == MSG_DOES_NOT_EXIST.format(description)
Exemplo n.º 15
0
def test_resp_does_not_exist_response_status_code_404():
    resp = resp_does_not_exist('pytest', 'Some description')
    assert resp.status_code == 404
Exemplo n.º 16
0
def test_resp_does_not_exist_raises_error():
    with pytest.raises(ValueError):
        resp_does_not_exist(None, None)
Exemplo n.º 17
0
def get_user_by_email(email):
    user = User.query.filter_by(email=email).first()
    if not user:
        return resp_does_not_exist('Users', 'Usuário')

    return user