Exemplo n.º 1
0
def get_ranking(request):

    try:
        ranking = list()
        vehicles = Vehicle.objects.values('model', 'manufactured',
                                          'motor').distinct()
        for vehicle in vehicles:
            equal_vehicles = Vehicle.objects.get_equal_vehicles(
                vehicle.get('model'), vehicle.get('manufactured'),
                vehicle.get('motor'))
            ranking.append({
                "vehicle":
                str(Model.objects.get_by_pk(pk=vehicle.get('model'))) + " - " +
                str(vehicle.get('motor')) + " - " +
                str(vehicle.get('manufactured')),
                "details":
                Supply.objects.get_detail_equal_vehicles(equal_vehicles),
            })
        ranking = sorted(ranking,
                         key=lambda k: k['details']['total_average'],
                         reverse=True)
        return response_commit({'ranking': ranking})

    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 2
0
def get_summary_by_vehicle(request, id_vehicle):
    try:
        # Sumarry vehicle
        vehicle = Vehicle.objects.get_vehicle_by_id_and_user(
            id_vehicle, request.user)
        supplies = vehicle.supply_set.all().order_by('-odometer')
        odometer = supplies[0].odometer if supplies else 0

        vehicle_response = {
            "id": vehicle.pk,
            "odometer": odometer,
            "motor": vehicle.motor,
            "manufactured": vehicle.manufactured
        }
        # Sumarry Supplies
        supplies_response = Supply.objects.get_details(id_vehicle,
                                                       request.user)

        # Sumarry Vehicle supplies same model
        equal_vehicles = Vehicle.objects.get_equal_vehicles(
            vehicle.model, vehicle.manufactured, vehicle.motor)
        equal_vehicles_response = Supply.objects.get_detail_equal_vehicles(
            equal_vehicles)

        return response_commit({
            'vehicle': vehicle_response,
            'supplies': supplies_response,
            'equal_vehicles': equal_vehicles_response
        })
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 3
0
def import_old_contcomb(request):

    try:
        # # Please, refactor this code! :S

        # Save vehicle first...
        serializer = SaveVehicleSerializer(data=request.DATA)

        if serializer.is_valid():

            vehicle = serializer.object
            vehicle.user = request.user
            vehicle.save()

            supplies = json.loads(request.DATA.get('supplies'), strict=False)
            current_km = request.DATA.get('current_km')
            walked_km = request.DATA.get('walked_km')

            # Validations
            is_valid_float_greater_zero_param(current_km)
            is_valid_float_greater_zero_param(walked_km)

            current_odometer = float(current_km) - float(walked_km)

            # Iterate supplies...
            for supply in supplies:

                # Set default values...
                supply['fuel'] = Fuel.objects.get_first_fuel()
                supply['fuel_price'] = 2
                supply['vehicle'] = vehicle.id
                supply['station'] = ''
                supply['is_full'] = True

                # Set values
                last_odometer = float(supply['odometer'])
                supply['odometer'] = current_odometer

                # Save supply
                serializer = SaveSerializer(data=supply)

                if serializer.is_valid():
                    supply_obj = serializer.object
                    supply_obj.save()
                    current_odometer -= last_odometer
                else:
                    logger.error(serializer.errors)
                    return ValidationExceptionSerializer.response_exception(
                        serializer.errors)

            return response_commit({})
        else:
            logger.error(serializer.errors)
            return ValidationExceptionSerializer.response_exception(
                serializer.errors)

    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 4
0
def get_by_user(request):
    try:
        response = Vehicle.objects.get_vehicles_by_user(request.user)
        
        return response_commit({'cars': response})
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 5
0
def get_by_user_vehicle(request, id_vehicle):
    try:
        response = Supply.objects.filter_by_user_vehicle(request.user, id_vehicle)
        
        return response_commit({'supplies': response})
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 6
0
def get_by_user(request):
    try:
        response = Vehicle.objects.get_vehicles_by_user(request.user)

        return response_commit({'cars': response})
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 7
0
def get_vehicle_fuel_by_user(request):
    try:
        vehicles = Vehicle.objects.get_vehicles_by_user(request.user)
        fuels = Fuel.objects.all().values()

        return response_commit({'vehicles': vehicles, 'fuels': fuels})
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 8
0
def get_by_user_vehicle(request, id_vehicle):
    try:
        response = Supply.objects.filter_by_user_vehicle(
            request.user, id_vehicle)

        return response_commit({'supplies': response})
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 9
0
def get_vehicle_fuel_by_user(request):
    try:
        vehicles = Vehicle.objects.get_vehicles_by_user(request.user)
        fuels = Fuel.objects.all().values()
        
        return response_commit({'vehicles': vehicles, 'fuels': fuels})
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)    
Exemplo n.º 10
0
def import_old_contcomb(request):
    
    try:
        # # Please, refactor this code! :S
        
        # Save vehicle first...
        serializer = SaveVehicleSerializer(data=request.DATA)

        if serializer.is_valid():

            vehicle = serializer.object
            vehicle.user = request.user
            vehicle.save()
            
            supplies = json.loads(request.DATA.get('supplies'), strict=False)
            current_km = request.DATA.get('current_km')
            walked_km = request.DATA.get('walked_km')

            # Validations
            is_valid_float_greater_zero_param(current_km)
            is_valid_float_greater_zero_param(walked_km)

            current_odometer = float(current_km) - float(walked_km)

            # Iterate supplies...
            for supply in supplies:
                
                # Set default values...
                supply['fuel'] = Fuel.objects.get_first_fuel()
                supply['fuel_price'] = 2
                supply['vehicle'] = vehicle.id
                supply['station'] = ''
                supply['is_full'] = True

                # Set values
                last_odometer = float(supply['odometer'])
                supply['odometer'] = current_odometer

                # Save supply
                serializer = SaveSerializer(data=supply)

                if serializer.is_valid():
                    supply_obj = serializer.object
                    supply_obj.save()
                    current_odometer -=  last_odometer
                else:
                    logger.error(serializer.errors)
                    return ValidationExceptionSerializer.response_exception(serializer.errors)
                
            return response_commit({})
        else:
            logger.error(serializer.errors)
            return ValidationExceptionSerializer.response_exception(serializer.errors)

    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 11
0
def get_by_id(request, id_supply):
 
    try:
        supply = Supply.objects.get_by_id_and_user(id_supply, request.user)
         
        return response_commit(model_to_dict(supply))
     
    except ObjectDoesNotExist, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(error_messages.get("invalid") % u"Abastecimento")
Exemplo n.º 12
0
def get_models(request):
 
    try:
        models = Model.objects.filter(valid=True).values_list("name", flat=True)
         
        return response_commit({'models': models})
     
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 13
0
def get_models(request):

    try:
        models = Model.objects.filter(valid=True).values_list("name",
                                                              flat=True)

        return response_commit({'models': models})

    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 14
0
def delete(request, id_supply):
     
    try:
        contact = Supply.objects.get(pk=id_supply, vehicle__user=request.user)
        contact.delete()
         
        return response_commit()
 
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 15
0
def delete(request, id_supply):

    try:
        contact = Supply.objects.get(pk=id_supply, vehicle__user=request.user)
        contact.delete()

        return response_commit()

    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 16
0
def get_by_id(request, id_supply):

    try:
        supply = Supply.objects.get_by_id_and_user(id_supply, request.user)

        return response_commit(model_to_dict(supply))

    except ObjectDoesNotExist, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(
            error_messages.get("invalid") % u"Abastecimento")
Exemplo n.º 17
0
def get_by_id(request, id_car):

    try:
        vehicle = Vehicle.objects.get_vehicle_by_id_and_user(id_car, request.user)
        
        return response_commit({"id": vehicle.pk, 
                                "model": vehicle.model.name, 
                                "motor": vehicle.motor,
                                "manufactured": vehicle.manufactured})
    
    except ObjectDoesNotExist, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(error_messages.get("invalid") % u"Veículo")
Exemplo n.º 18
0
def delete(request, id_user):

    try:

        # Valid ID
        try:
            is_valid_int_greater_zero_param(id_user)
        except ValidationError, e:
            return ValidationExceptionSerializer.response_exception({"id_user": e.messages})

        # Find User by ID to check if it exist
        try:
            user = User.objects.get_by_pk(id_user)
        except ObjectDoesNotExist, e:
            return ServiceExceptionSerializer.response_exception(error_messages.get("invalid") % "Usuário")
Exemplo n.º 19
0
def get_all(request):

    try:

        users_list = []
        for user in User.objects.all():
            user_list = {}
            user_list['user'] = model_to_dict(user, exclude=["password", "token"])
            users_list.append(user_list)

        return response_commit(users_list)

    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 20
0
def generate_token(request):

    try:

        user = request.user

        if not user.has_token() or not user.is_valid_token():
            user.generate_token()
            user.save()

        return Response({"token": user.token})

    except Exception, e:
        logger.error(e)
        serializer = ServiceExceptionSerializer(message=e)
        return Response(serializer.data)
Exemplo n.º 21
0
def get_by_id(request, id_car):

    try:
        vehicle = Vehicle.objects.get_vehicle_by_id_and_user(
            id_car, request.user)

        return response_commit({
            "id": vehicle.pk,
            "model": vehicle.model.name,
            "motor": vehicle.motor,
            "manufactured": vehicle.manufactured
        })

    except ObjectDoesNotExist, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(
            error_messages.get("invalid") % u"Veículo")
Exemplo n.º 22
0
def save(request):

    try:

        serializer = SaveSerializer(data=request.DATA)

        if serializer.is_valid():

            user = serializer.object
            user.save()

            return response_commit({"user": model_to_dict(user, exclude=["password", "token"])})
        else:
            return ValidationExceptionSerializer.response_exception(serializer.errors)

    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 23
0
def save(request):

    try:
        serializer = SaveSerializer(data=request.DATA)

        if serializer.is_valid():

            vehicle = serializer.object
            vehicle.user = request.user
            vehicle.save()

            return response_commit({"vehicle": model_to_dict(vehicle)})
        else:
            logger.error(serializer.errors)
            return ValidationExceptionSerializer.response_exception(serializer.errors)

    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 24
0
def get_ranking(request):
 
    try:
        ranking = list()
        vehicles = Vehicle.objects.values('model', 'manufactured', 'motor').distinct()
        for vehicle in vehicles:
            equal_vehicles = Vehicle.objects.get_equal_vehicles(vehicle.get('model'), 
                                                                vehicle.get('manufactured'),
                                                                vehicle.get('motor'))
            ranking.append({
                            "vehicle": str(Model.objects.get_by_pk(pk=vehicle.get('model'))) + " - " + str(vehicle.get('motor')) + " - " + str(vehicle.get('manufactured')),
                            "details": Supply.objects.get_detail_equal_vehicles(equal_vehicles), 
                            })
        ranking = sorted(ranking, key=lambda k: k['details']['total_average'], reverse=True) 
        return response_commit({'ranking': ranking})
     
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 25
0
def save(request):

    try:

        serializer = SaveSerializer(data=request.DATA)

        if serializer.is_valid():

            contact = serializer.object
            contact.user = request.user
            contact.save()

            return response_commit()
        else:
            return ValidationExceptionSerializer.response_exception(serializer.errors)

    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 26
0
def save(request):

    try:
        serializer = SaveSerializer(data=request.DATA)

        if serializer.is_valid():

            vehicle = serializer.object
            vehicle.user = request.user
            vehicle.save()

            return response_commit({"vehicle": model_to_dict(vehicle)})
        else:
            logger.error(serializer.errors)
            return ValidationExceptionSerializer.response_exception(
                serializer.errors)

    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 27
0
def save(request):

    try:

        serializer = SaveSerializer(data=request.DATA)

        if serializer.is_valid():

            contact = serializer.object
            contact.user = request.user
            contact.save()

            return response_commit()
        else:
            return ValidationExceptionSerializer.response_exception(
                serializer.errors)

    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
Exemplo n.º 28
0
def update(request):

    try:

        user = User.objects.get_by_pk(request.user.id)

        serializer = UpdateSerializer(clone(user), data=request.DATA)

        if serializer.is_valid():

            user = serializer.object
            user.save()

            return response_commit({"user": model_to_dict(user, exclude=["password", "token"])})

        else:
            return ValidationExceptionSerializer.response_exception(serializer.errors)

    except ObjectDoesNotExist, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(error_messages.get("invalid") % "Usuário")
Exemplo n.º 29
0
def update(request):

    try:

        vehicle = Vehicle.objects.get_by_pk(request.DATA.get('car_id'))

        serializer = SaveSerializer(clone(vehicle), data=request.DATA)

        if serializer.is_valid():

            vehicle = serializer.object
            vehicle.save()

            return response_commit(model_to_dict(vehicle))

        else:
            return ValidationExceptionSerializer.response_exception(serializer.errors)

    except ObjectDoesNotExist, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(error_messages.get("invalid") % u"Veículo")
Exemplo n.º 30
0
def update(request):

    try:

        vehicle = Vehicle.objects.get_by_pk(request.DATA.get('car_id'))

        serializer = SaveSerializer(clone(vehicle), data=request.DATA)

        if serializer.is_valid():

            vehicle = serializer.object
            vehicle.save()

            return response_commit(model_to_dict(vehicle))

        else:
            return ValidationExceptionSerializer.response_exception(
                serializer.errors)

    except ObjectDoesNotExist, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(
            error_messages.get("invalid") % u"Veículo")
Exemplo n.º 31
0
def get_summary_by_vehicle(request, id_vehicle):
    try:
        # Sumarry vehicle
        vehicle = Vehicle.objects.get_vehicle_by_id_and_user(id_vehicle, request.user)
        supplies = vehicle.supply_set.all().order_by('-odometer')
        odometer = supplies[0].odometer if supplies else 0

        vehicle_response = {"id": vehicle.pk, 
                           "odometer": odometer, 
                           "motor": vehicle.motor,
                           "manufactured": vehicle.manufactured}
        # Sumarry Supplies
        supplies_response = Supply.objects.get_details(id_vehicle, request.user)
        
        # Sumarry Vehicle supplies same model
        equal_vehicles = Vehicle.objects.get_equal_vehicles(vehicle.model, vehicle.manufactured, vehicle.motor)
        equal_vehicles_response = Supply.objects.get_detail_equal_vehicles(equal_vehicles)
            
        return response_commit({'vehicle': vehicle_response, 
                                'supplies': supplies_response, 
                                'equal_vehicles': equal_vehicles_response})
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)     
Exemplo n.º 32
0
        if serializer.is_valid():

            user = serializer.object
            user.save()

            return response_commit({"user": model_to_dict(user, exclude=["password", "token"])})

        else:
            return ValidationExceptionSerializer.response_exception(serializer.errors)

    except ObjectDoesNotExist, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(error_messages.get("invalid") % "Usuário")
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)

@log
@commit_manually
@commit_or_rollback
@api_view(['GET'])
@authentication_classes((BasicAuthentication,))
@permission_classes((IsAuthenticated,))
@renderer_classes(Renderer)
def get_by_username(request, username):

    try:

        # Valid Username
        validate_required(username)
        # Find User by username to check if it exist
Exemplo n.º 33
0
            supply = serializer.object
            supply.save()

            return response_commit({"supply": model_to_dict(supply)})
        else:
            logger.error(serializer.errors)
            return ValidationExceptionSerializer.response_exception(
                serializer.errors)

    except ObjectDoesNotExist, e:
        logger.error(e)
        return ObjectDoesNotExistExceptionSerializer.response_exception(
            e.message)
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)


@log
@commit_manually
@commit_or_rollback
@api_view(['POST'])
@authentication_classes((BasicAuthentication, ))
@permission_classes((IsAuthenticated, ))
@renderer_classes(Renderer)
def update(request):

    try:

        supply = Supply.objects.get_by_pk(request.DATA.get('supply_id'))
Exemplo n.º 34
0
        if serializer.is_valid():
 
            supply = serializer.object
            supply.save()
 
            return response_commit({"supply": model_to_dict(supply)})
        else:
            logger.error(serializer.errors)
            return ValidationExceptionSerializer.response_exception(serializer.errors)
 
    except ObjectDoesNotExist, e:
        logger.error(e)
        return ObjectDoesNotExistExceptionSerializer.response_exception(e.message)
    except Exception, e:
        logger.error(e)
        return ServiceExceptionSerializer.response_exception(e.message)
 
 
@log
@commit_manually
@commit_or_rollback
@api_view(['POST'])
@authentication_classes((BasicAuthentication,))
@permission_classes((IsAuthenticated,))
@renderer_classes(Renderer)
def update(request):
 
    try:
 
        supply = Supply.objects.get_by_pk(request.DATA.get('supply_id'))