def guardar(request): #Coloca los datos en la base de datos us = request.POST.get("user_id") ciudad = request.POST.get("ciudad") locacion = request.POST.get("locacion") km = request.POST.get("km") color = request.POST.get("color") precio = request.POST.get("precio") car_infid = request.POST.get("car_infid") marca = request.POST.get("marca") modelo = request.POST.get("modelo") anio = request.POST.get("anio") print( request ) sys.stdout.flush() try: ci = carinf_type = Car_info.objects.get( model = modelo ) ui = User.objects.get( id = us ) except Exception as e: return HttpResponse("Indice incorrecto") if request.method == "POST": BDG = Car(user_id = ui ,status = "Disponible",city = ciudad ,location = locacion ,km = km , color = color , price = precio, carinfo_id = ci, year_purch = anio) BDG.save() return HttpResponse("guardado en BD")
def signup_user(request): if request.method == 'GET': context = { 'form': UserRegisterForm(), } return render(request, 'accounts/signup.html', context) else: form = UserRegisterForm(request.POST) if form.is_valid(): car = Car() user = form.save() user_model = User.objects.get(id=user.id) car.fk_user_id = user_model car.car = '' car.save() login(request, user) return redirect('user profile') context = { 'form': form, } return render(request, 'accounts/signup.html', context)
def test_car(self): """Test the Car model.""" car = Car() car.make = 'Make' car.model = 'Model' car.year = 1999 car.save()
def add_new_car(cls, brand=None, model=None, year=None, vin=None): """Add new car to database\n return car id """ logger.info("add_new_car / {}".format(locals())) car = Car(brand=brand, model=model, year=year, vin=vin) car.save() return car.id
def handle(self, dataset, **options): """Command handler.""" print(dataset) with open(dataset, newline='\n') as csvfile: car_reader = csv.reader(csvfile, delimiter=',') for year, make, model in car_reader: manufacturer = _get_or_create_manufacturer(make) car = Car(name=model, year=year, manufacturer=manufacturer) car.save() print('Created {0} {1} {2}'.format(year, model, make))
def add_car(request): car = Car() if request.method == 'POST': form = NewCarForm(request.POST, instance=car) if form.is_valid(): car.save() request.user.get_profile().cars.add(car) return HttpResponseRedirect('/users/edit_cars/') else: form = NewCarForm(instance=car) return render_to_response('users/add_car.html', {'form': form}, context_instance=RequestContext(request))
def guardar(request): #Coloca los datos en la base de datos us = request.POST.get("user_id") ciudad = request.POST.get("ciudad") locacion = request.POST.get("locacion") km = request.POST.get("km") color = request.POST.get("color") precio = request.POST.get("precio") car_infid = request.POST.get("car_infid") marca = request.POST.get("marca") modelo = request.POST.get("modelo") anio = request.POST.get("anio") trans = request.POST.get("trans") tipo = request.POST.get("type") traccion = request.POST.get("traccion") hpower = request.POST.get("hpower") Motor = request.POST.get("Motor") fuel = request.POST.get("fuel") print( "request" ) sys.stdout.flush() carros = Car_info.objects.all() try: ci = 0 ui = 0 ui = User.objects.get( id = us ) for x in carros: if x.brand == marca and x.model == modelo: if x.transmission == trans: ci = carinf_type = x if ci == 0: BDCI = Car_info(transmission = str(trans), type = str(tipo), traction = str(traccion), hpower = str(hpower), motor = str(Motor) ,model = str(modelo),brand = str(marca), fuel = str(fuel), year = anio) BDCI.save() for x in carros: if x.brand == marca and x.model == modelo: if x.transmission == trans: ci = carinf_type = x except Exception as e: print(e) return HttpResponse("Indice incorrecto") if request.method == "POST": BDG = Car(user_id = ui ,status = "Disponible",city = ciudad ,location = locacion ,km = km , color = color , price = precio, carinfo_id = ci, year_purch = anio) BDG.save() return HttpResponse("guardado en BD")
def create_cars(self): """ Create all for cars. """ # Car brands car_brands = [] car_models = [] for car_brand_slug, car_brand_name in CAR_BRANDS: try: car_brand = CarBrand.objects.get( slug=car_brand_slug) except CarBrand.DoesNotExist: car_brand = CarBrand.objects.create( slug=car_brand_slug, name=car_brand_name) car_brands.append(car_brand) for affix in ('I', 'II', 'III', 'IV', 'V'): car_model_name = ' ' . join( map(lambda x: x.strip(), (car_brand.name, affix))) try: car_model = CarModel.objects.get(name=car_model_name) except CarModel.DoesNotExist: car_model = CarModel.objects.create( mark=car_brand, name=car_model_name) car_models.append(car_model) for i in xrange(0, DEFAULT_TOTAL_CARS): new_car = Car() new_car.driver = random.choice(self._drivers) new_car.model = random.choice(car_models) new_car.car_no = u'%s-RT-%s' % ( RandomItemSupplier.get_random_chars(string.digits, 2), RandomItemSupplier.get_random_chars(string.digits, 3)) new_car.release_year = random.choice(range(1999, 2015)) new_car.color = random.choice( ('red', 'green', 'yellow', 'brown', 'black')) new_car.vin = u'VIN-%s' % \ RandomItemSupplier.get_random_chars(string.digits, 7) new_car.save() self._cars.append(new_car)
def make(request): colors = ["Red", "Blue", "Pink", "Magenta"] descriptions = ["Great car", "Awful car", "Ran over a hobo with it"] owner_names = ["Ali", "Tommy", "Dan", "Catherine", "Alex"] car_type = [1,2,4][randint(a=0, b=2)] car_color = colors[randint(a=0, b=len(colors)-1)] car_desc = descriptions[randint(a=0, b=len(descriptions)-1)] owner_name = owner_names[randint(a=0, b = len(owner_names)-1)] car = Car( owner_name= owner_name, color=car_color, type=car_type, description=car_desc, created = timezone.now() ) car.save() return redirect(to='index')
def process_item(self, item, spider): from cars.models import Car, Price # Creating model instances and saving them to DB print( "Entering process_item method, and attempting to create a car object" ) if item['Finn_kode'] in spider.codes: print('This is a crappy car.') car = Car.objects.filter(Finn_kode=item['Finn_kode']).get() temp_age = timezone.now() - car.created_at print('Age: {}'.format(temp_age)) car.last_updated = timezone.now() if car.age < temp_age.days: print('New age {}'.format(temp_age)) car.age = temp_age.days if car.solgt != item['solgt']: print("Marking car as sold") car.solgt = item['solgt'] if car.removed != item['removed']: print('Marking car as removed') car.removed = item['removed'] car.save() else: print("{} is not in the DB".format(item['Finn_kode'])) # Consider: https://stackoverflow.com/questions/8372703/how-can-i-use-different-pipelines-for-different-spiders-in-a-single-scrapy-proje car = Car() # Finn car.Finn_kode = item['Finn_kode'] # General info car.Antalldører = item['Antalldører'] car.Antalleiere = item['Antalleiere'] car.Antallseter = item['Antallseter'] car.Årsavgift = item['Årsavgift'] car.Årsmodell = item['Årsmodell'] car.Avgiftsklasse = item['Avgiftsklasse'] car.Bilenståri = item['Bilenståri'] car.ChassisnrVIN = item['ChassisnrVIN'] car.CO2utslipp = item['CO2utslipp'] car.Drivstoff = item['Drivstoff'] car.Effekt = item['Effekt'] car.Farge = item['Farge'] car.Fargebeskrivelse = item['Fargebeskrivelse'] car.foorstegangregistrert = item['foorstegangregistrert'] car.Girkasse = item['Girkasse'] car.header = item['header'] car.Hjuldrift = item['Hjuldrift'] car.Karosseri = item['Karosseri'] car.Kmstand = item['Kmstand'] car.last_updated = timezone.now() car.name = item['name'] car.Omregistrering = item['Omregistrering'] car.Priseksomreg = item['Priseksomreg'] car.Regnr = item['Regnr'] car.Salgsform = item['Salgsform'] car.Sylindervolum = item['Sylindervolum'] car.totalpris = item['totalpris'] car.Vekt = item['Vekt'] car.solgt = item['solgt'] car.RekkeviddeWLTP = item['RekkeviddeWLTP'] car.Batterikapasitet = item['Batterikapasitet'] # Farger car.Interiørfarge = item['Interiørfarge'] print(car) car.save() price = Price() price.date = date.today() price.price = item['totalpris'] price.car = car price.save() return item
}, ] }, { 'domain': 'bmw', 'cars': [ { 'model': 'BMW Serie 1', 'year': '2019', 'color': 'Blue', 'stock': 5 }, { 'model': 'BMW X6', 'year': '2019', 'color': 'Green', 'stock': 2 }, ] } ] for company_cars_info in cars_by_company_info: with schema_context(company_cars_info['domain']): for car_info in company_cars_info['cars']: car = Car(model=car_info['model'], year=car_info['year'], color=car_info['color'], stock=car_info['stock']) car.save()