def save_businesses(): for bdata in iterate_file("business", shortcircuit=False): business = Business() business.business_id = bdata['business_id'] business.name = bdata['name'] business.full_address = bdata['full_address'] business.city = bdata['city'] business.state = bdata['state'] business.latitude = bdata['latitude'] business.longitude = bdata['longitude'] business.stars = decimal.Decimal(bdata.get('stars', 0)) business.review_count = int(bdata['review_count']) business.is_open = True if bdata['open'] == "True" else False business.save() save_categories(bdata['business_id'], bdata['categories']) save_neighborhoods(bdata['business_id'], bdata['neighborhoods'])
def save_businesses(): for bdata in iterate_file("business", shortcircuit=False): business = Business() business.business_id = bdata['business_id'] business.name = bdata['name'] business.address = bdata['address'] business.city = bdata['city'] business.neighborhood = bdata['neighborhood'] business.state = bdata['state'] business.latitude = bdata['latitude'] business.longitude = bdata['longitude'] business.stars = decimal.Decimal(bdata.get('stars', 0)) business.review_count = int(bdata['review_count']) business.is_open = True if bdata['is_open'] == "1" else False business.save() temp = bdata['attributes'] if bdata['attributes'] is not None else [] temp1 = bdata['categories'] if bdata['categories'] is not None else [] save_categories(bdata['business_id'], temp1) save_attributes(bdata['business_id'], temp)
def register(self, request, **cleaned_data): new_user = super(BusinessRegistrationView, self).register(request, **cleaned_data) group = Group.objects.get(name="Business Users") new_user.groups.add(group) permission = Permission.objects.get(name="account_up-to-date") new_user.user_permissions.add(permission) new_user.first_name = cleaned_data["first_name"] new_user.save() new_profile = Business( user=new_user, street=cleaned_data["street"], area=cleaned_data["area"], city=cleaned_data["city"], region=cleaned_data["region"], country=cleaned_data["country"], telephone=cleaned_data["telephone"], min_age=cleaned_data["min_age"], mailing_list=cleaned_data["mailing_list"], ) new_profile.save() return new_user
def test_unicode_pk(self): # Primary key may be unicode string bus = Business(name=u'jaźń') bus.save()
def test_custom_pk(self): dan = Employee(employee_code=123, first_name='Dan', last_name='Jones') dan.save() self.assertQuerysetEqual(Employee.objects.all(), ['<Employee: Dan Jones>']) fran = Employee(employee_code=456, first_name='Fran', last_name='Bones') fran.save() self.assertQuerysetEqual(Employee.objects.all(), ['<Employee: Fran Bones>', '<Employee: Dan Jones>']) self.assertEqual(repr(Employee.objects.get(pk=123)), '<Employee: Dan Jones>') self.assertEqual(repr(Employee.objects.get(pk=456)), '<Employee: Fran Bones>') self.assertRaises(Employee.DoesNotExist, Employee.objects.get, pk=42) # Use the name of the primary key, rather than pk. self.assertEqual(repr(Employee.objects.get(employee_code__exact=123)), '<Employee: Dan Jones>') # pk can be used as a substitute for the primary key. self.assertQuerysetEqual(Employee.objects.filter(pk__in=[123, 456]), ['<Employee: Fran Bones>', '<Employee: Dan Jones>']) # The primary key can be accessed via the pk property on the model. e = Employee.objects.get(pk=123) self.assertEqual(e.pk, 123) # Or we can use the real attribute name for the primary key: self.assertEqual(e.employee_code, 123) # Fran got married and changed her last name. fran = Employee.objects.get(pk=456) fran.last_name = 'Jones' fran.save() self.assertQuerysetEqual(Employee.objects.filter(last_name__exact='Jones') , ['<Employee: Dan Jones>', '<Employee: Fran Jones>']) emps = Employee.objects.in_bulk([123, 456]) self.assertEqual(repr(emps[123]), '<Employee: Dan Jones>') b = Business(name='Sears') b.save() b.employees.add(dan, fran) self.assertQuerysetEqual(b.employees.all(), ['<Employee: Dan Jones>', '<Employee: Fran Jones>']) self.assertQuerysetEqual(fran.business_set.all(), ['<Business: Sears>']) self.assertEqual(repr(Business.objects.in_bulk(['Sears'])), "{u'Sears': <Business: Sears>}") self.assertQuerysetEqual(Business.objects.filter(name__exact='Sears'), ['<Business: Sears>']) self.assertQuerysetEqual(Business.objects.filter(pk='Sears'), ['<Business: Sears>']) # Queries across tables, involving primary key self.assertQuerysetEqual(Employee.objects.filter(business__name__exact='Sears'), ['<Employee: Dan Jones>', '<Employee: Fran Jones>']) self.assertQuerysetEqual(Employee.objects.filter(business__pk='Sears'), ['<Employee: Dan Jones>', '<Employee: Fran Jones>']) self.assertQuerysetEqual(Business.objects.filter(employees__employee_code__exact=123), ['<Business: Sears>']) self.assertQuerysetEqual(Business.objects.filter(employees__pk=123), ['<Business: Sears>']) self.assertQuerysetEqual(Business.objects.filter(employees__first_name__startswith='Fran'), ['<Business: Sears>'])
from models import Business import os from django.conf import settings b = Business(name='A business', password = '******') b.save()
def upload(request): """ Uploads the receipt :url: /shoebox/upload/ :param POST['email']: email identifying user :param POST['business_name']: i.e. McDonalds (blank) :param POST['address']: business address (blank) :param POST['location']: i.e. JFK International (blank) :param POST['phone']: phone number (blank) :param POST['city']: city (blank) :param POST['state']: state (blank) :param POST['purchase_date']: purchase date in NeatReceipts format :param POST['tax']: tax (blank) :param POST['tip']: tip (blank) :param POST['amount']: total amount :param POST['payment_method']: Visa, Master, Cash etc :param POST['category']: NeatReceipts category :param FILES['img']: Receipts image :rtype: JSON :: #: if success in posting returns id of created or update object in string format {'result': 'id'} #: if failed {'result': '-1'} #: if request was not POST {'result': '0'} """ if request.method == 'POST': form = ReceiptUploadForm(request.POST, request.FILES) if form.is_valid(): instance = form.save(commit=False) # assign to the current user uploading data instance.user, created = OTNUserTemp.objects.get_or_create(email=request.POST['email']) instance.save() receipt = DetailReceipt(basic=instance) if 'business_name' in request.POST: b = Business(name=request.POST['business_name']) if 'location' in request.POST: b.location = request.POST['location'] if 'phone' in request.POST: b.phone = request.POST['phone'] if 'address' in request.POST: b.address = request.POST['address'] if 'city' in request.POST: c = City(name=request.POST['city'], state=request.POST['state']) c.save() b.city = c b.save() receipt.business = b if 'category' in request.POST: cat, created = Category.objects.get_or_create(name=request.POST['category']) receipt.category = cat if 'tax' in request.POST: receipt.tax = request.POST['tax'] if 'tip' in request.POST: receipt.tip = request.POST['tip'] if 'payment_method' in request.POST: pmethod = request.POST['payment_method'].lower() if pmethod.find('cash') != -1: receipt.payment_method = receipt.CASH elif pmethod.find('amex') != -1: receipt.payment_method = receipt.AMEX elif pmethod.find('visa') != -1: receipt.payment_method = receipt.VISA elif pmethod.find('master') != -1: receipt.payment_method = receipt.MASTER elif pmethod.find('discover') != -1: receipt.payment_method = receipt.DISCOVER else: receipt.payment_method = receipt.CASH receipt.save() return JSONHttpResponse({'result':str(receipt.id)}) else: return JSONHttpResponse({'result':'-1', 'form_errors':str(form.errors)}) else: return JSONHttpResponse({'result':'0'})