def bulk_city(): state = State.objects.get(pk=1) City.objects.bulk_create([ City(name='تهران', slug='tehran', state=state), City(name='کرج', slug='karaj', state=state), City(name='شهریار', slug='shahryiar', state=state), ]) print("bulk bulk_city inserted!!!")
def AddingNewCity(request): print 'inside designation' if request.method == 'POST': json_dict = {} city_name = request.POST.get('city_name', '') print city_name print '***********************' new_city = City() new_city.city_name = city_name try: new_city.save() except Exception as e: return JsonResponse({'success': 'False', 'exception': str(e)}) json_dict['success'] = 'True' json_dict['city_name'] = city_name return JsonResponse(json_dict) else: return JsonResponse({ 'success': 'False', 'exception': 'Not a post request' })
def clean_city(self): if type(self.cleaned_data['zip']) != type(Zip()): self.clean_zip(self) try: cityobj = City.objects.get(name=self.cleaned_data['city'], zip__pk__in=[self.cleaned_data['zip'].pk,]) except City.DoesNotExist: cityobj = City() cityobj.name = self.cleaned_data['city'] cityobj.save() cityobj.zip.add(self.cleaned_data['zip']) cityobj.save() return cityobj
def clean(self): cleaned_data = super(EditRetailerForm, self).clean() zip = cleaned_data.get('zip') city = cleaned_data.get('city') if zip and city: try: cityobj = City.objects.get(name=city, zip__pk__in=[zip.pk,]) except City.DoesNotExist: cityobj = City() cityobj.name = city cityobj.save() cityobj.zip.add(zip) cityobj.save() cleaned_data['city'] = cityobj return cleaned_data
def handle(self, *args, **options): import json with open('location/management/commands/locations.json', encoding='utf-8') as file: lines = json.load(file) provinces = lines[0] cities = lines[1] districts = lines[2] bulk = [] for province in provinces: p = Province(name=province[0], code=province[1]) bulk.append(p) if len(bulk) > 20: Province.objects.bulk_create(bulk) bulk = [] Province.objects.bulk_create(bulk) bulk = [] for city in cities: province = Province.objects.get(name=city[2]) c = City(name=city[0], code=city[1], province=province) bulk.append(c) if len(bulk) > 20: City.objects.bulk_create(bulk) bulk = [] City.objects.bulk_create(bulk) bulk = [] for district in districts: city = City.objects.get(name=district[2]) d = District(name=district[0], code=district[1], city=city) bulk.append(d) if len(bulk) > 20: District.objects.bulk_create(bulk) bulk = [] District.objects.bulk_create(bulk)
def updatePost(request, id): if request.user.is_authenticated: userP = UserProfile.objects.get(userID=request.user) if not userP.editovanProfil: sweetify.sweetalert(request, title="Molimo popunite svoj CV", icon="error") return redirect('editprofil') if request.method == 'POST': if request.POST['type'] == "1": title = request.POST['naslov'] category = request.POST.get('category', None) expiration = request.POST.get('expiration', None) lokacija = request.POST['lokacija'] pozicija = request.POST['pozicija'] godineIskustva = request.POST['godineIskustva'] strucnasprema = request.POST['strucnasprema'] email = request.POST['email'] brojTel = request.POST['brojTel'] opis = request.POST['opis'] type = request.POST['type'] if request.FILES.get('image_uploads', None): myfile = request.FILES['image_uploads'] fs = FileSystemStorage() filename = fs.save(myfile.name, myfile) uploaded_file_url = fs.url(filename) else: myfile = None args = [ title, category, expiration, lokacija, pozicija, godineIskustva, strucnasprema, email, brojTel, opis, type ] keys = [ 'naslov', 'category', 'expiration', ' City', 'pozicija', 'godineIsku', 'strucnasprema', 'email', 'brojTel', 'opis', 'type', 'brojIzvr' ] argErr = [ "Naslov", "Kategorija", 'Trajanje', 'Lokacija', 'Pozicija', 'Godine iskustva', 'Sprema', 'Kontakt Email', 'Kontakt Telefon', 'Vrsta', 'Broj izrsitelja' ] valid = validationWithKeys(request, args, keys, argErr) if valid is not True: return HttpResponseRedirect( request.META.get('HTTP_REFERER', '/').split('?')[0] + valid) cat = Category.objects.get(name=category) if City.objects.all().filter(name=lokacija).exists(): city = City.objects.get(name=lokacija) else: city = City(name=lokacija) city.save() post = Post.objects.get(pk=id) post.userID = request.user post.categoryID = cat post.title = title post.region = "BiH" post.location = city.name post.position = pozicija post.type = type post.specialty = strucnasprema post.experience = godineIskustva post.contact_email = email post.contact_phone = brojTel post.content = opis post.attachment = myfile if expiration is not '0': post.expires_at = datetime.now() + timedelta( days=int(expiration)) else: post.expires_at = datetime.now() + timedelta(days=365 * 200) post.save() sweetify.success(request, title="Uspješno ažuriran oglas", text="", icon="success", timer=8000) return redirect('dashboard') else: type = request.POST['type'] btobtype = request.POST.get('b2btype', None) category = request.POST.get('category', None) kanton = request.POST.get('kanton', None) trajanje = request.POST.get('expiration', None) email = request.POST['email'] brojTel = request.POST['brojTel'] opis = request.POST['opis'] expiration = request.POST.get('expiration', None) if request.FILES.get('image_uploads', None): myfile = request.FILES.get('image_uploads', None) fs = FileSystemStorage() filename = fs.save(myfile.name, myfile) uploaded_file_url = fs.url(filename) else: myfile = None args = [ type, btobtype, category, kanton, trajanje, email, brojTel, opis ] keys = [ 'type', 'b2btype', 'category', 'kanton', 'trajanje', 'email', 'brojTel', 'opis' ] argErr = [ '', "Trazim/Nudim", "Kategorija", 'Kanton', 'Trajanje', 'Email', 'Broj Telefona', 'Opis' ] valid = validationWithKeys(request, args, keys, argErr) if valid is not True: return HttpResponseRedirect( request.META.get('HTTP_REFERER', '/').split('?')[0] + valid) cat = Category.objects.get(name=category) if category == 'Finansijske' or category == "Osiguravajuće": title = request.POST['naslov'] else: if btobtype == 1: title = "Ponuda" elif btobtype == 2: title = "Potražnja" else: title = "Partnerstvo" if category == 'Finansijske' or category == "Osiguravajuće": position = request.POST['position'] else: position = "" post = Post.objects.get(pk=id) post.attachment = myfile post.position = position post.title = title post.userID = request.user post.categoryID = cat post.type = int(type) post.b2b_type = int(btobtype) post.region = kanton post.contact_email = email post.contact_phone = brojTel post.content = opis if expiration is not '0': post.expires_at = datetime.now() + timedelta( days=int(expiration)) else: post.expires_at = datetime.now() + timedelta(days=365 * 200) post.save() sweetify.success(request, title="Uspješno ažuriran oglas", icon="success", timer=8000) return redirect('dashboard')
def handle(self, *args, **options): gradovi = [ 'Banja Luka', 'Banovići', 'Bihać', 'Bijeljina', 'Bosanska Krupa', 'Brčko', 'Bugojno', 'Cazin', 'Derventa', 'Doboj', 'Gradačac', 'Gradiška', 'Gračanica', 'Jajce', 'Kakanj', 'Kalesija', 'Konjic', 'Kozarska Dubica', 'Laktaši', 'Livno', 'Ljubuški', 'Lukavac', 'Maglaj', 'Modriča', 'Mostar', 'Novi Grad', 'Novi Travnik', 'Prijedor', 'Prnjavor', 'Republika Srpska', 'Republika Srpska', 'Sanski Most', 'Sarajevo', 'Srebrenik', 'Teslić', 'Tešanj', 'Tomislavgrad', 'Travnik', 'Trebinje', 'Tuzla', 'Velika Kladuša', 'Visoko', 'Vitez', 'Zavidovići', 'Zenica', 'Zvornik', 'Čapljina', 'Široki Brijeg', 'Žepče', 'Živinice', ] for g in gradovi: if not City.objects.filter(name=g).exists(): g = City(name=g) g.save()