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)
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)
def delete(request, id_message): contact = Contact.objects.get(pk=id_message, user=request.user) contact.delete() return response_commit()
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)
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)
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)
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)
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)
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)
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)
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)
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)
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")
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)
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")
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)
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)
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")
def get_by_username(request, username): try: # Valid Username validate_required(username) # Find User by username to check if it exist user = User.objects.get_by_username(username) return response_commit({"user": model_to_dict(user, exclude=["password", "token"])}) except ValidationError, e: return ValidationExceptionSerializer.response_exception({"username": e.messages})
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)
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")
def save(request): try: serializer = SaveSerializer(data=request.DATA) 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)
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)
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)
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)
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)
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)
def save(request): try: serializer = SaveSerializer(data=request.DATA) 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)
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)
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")
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")
def update(request): try: supply = Supply.objects.get_by_pk(request.DATA.get('supply_id')) serializer = SaveSerializer(clone(supply), data=request.DATA) if serializer.is_valid(): supply = serializer.object supply.save() return response_commit(model_to_dict(supply)) else: return ValidationExceptionSerializer.response_exception(serializer.errors) except ObjectDoesNotExist, e: logger.error(e) return ObjectDoesNotExistExceptionSerializer.response_exception(e.message)
def update(request): try: supply = Supply.objects.get_by_pk(request.DATA.get('supply_id')) serializer = SaveSerializer(clone(supply), data=request.DATA) if serializer.is_valid(): supply = serializer.object supply.save() return response_commit(model_to_dict(supply)) else: return ValidationExceptionSerializer.response_exception( serializer.errors) except ObjectDoesNotExist, e: logger.error(e) return ObjectDoesNotExistExceptionSerializer.response_exception( e.message)
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")
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)
def get_by_id(request, id_message): response = Contact.objects.get_by_pk(pk=id_message) return response_commit(model_to_dict(response))
def get_by_user(request): response = Contact.objects.get_messages_by_user(request.user) return response_commit({'messages': response})
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: logger.error(e) return ObjectDoesNotExistExceptionSerializer.response_exception(error_messages.get("invalid") % "Usuário") return response_commit({"user": model_to_dict(user, exclude=["password", "token"])}) except Exception, e: logger.error(e) return ServiceExceptionSerializer.response_exception(e) @log @commit_manually @commit_or_rollback @api_view(['GET']) @authentication_classes((TokenAuthentication,)) @permission_classes((IsAuthenticated,)) @renderer_classes(Renderer) def delete(request, id_user): try:
def get_by_user(request): response = Contact.objects.get_messages_by_user(request.user) return response_commit({'messages': response})
def get_by_id(request, id_message): response = Contact.objects.get_by_pk(pk=id_message) return response_commit(model_to_dict(response))
def delete(request, id_message): contact = Contact.objects.get(pk=id_message, user=request.user) contact.delete() return response_commit()