def test_simple_product(self): parent_tag = Tag(name='Alimentation', parent=None) parent_tag.save() son_tag = Tag(name='Potatoes', parent=parent_tag) son_tag.save() User = get_user_model() user = User.objects.create_user( username="******", email="*****@*****.**", password='******', tin='12345X', fiscal_address='St. Nothing', phone='123456789', lat=1.41, lon=1.51, description='Some weird description', country='ES', ) product = Product( creator=user, tag=son_tag, name='Patata Manhattan', description='45kg bag of potatoes', ) product.save() self.assertEquals(user, product.creator) self.assertEquals(son_tag, product.tag) self.assertEquals(parent_tag, product.tag.parent) self.assertEquals('Patata Manhattan', product.name) self.assertEquals('45kg bag of potatoes', product.description)
def new_product(request): if request.is_ajax(): data = request.POST['data'] data = json.loads(data) data.pop("id", None) data['status'] = True try: product = Product(**data) product.validate_unique() product.save() context = { 'results': product.as_json(), 'totalProperty': 0, 'success': True } except ValidationError as e: non_field_errors = e.messages context = { 'msg': non_field_errors, 'results': [], 'totalProperty': 0, 'success': False } context = json.dumps(context) return HttpResponse(context, content_type='application/json') return Http404
def get_object(self): if self.request.method == 'PUT': resource = Product.objects.filter(id=self.kwargs.get('pk')).first() if resource: return resource else: return Product(id=self.kwargs.get('pk')) else: return super(PutProductAPIView, self).get_object()
def handle(self, *args, **options): self.clean() categories = loadFromJSON('categories') for category in categories: new_category = Category(**category) new_category.save() products = loadFromJSON('products') for product in products: product['category'] = Category.objects.get( name=product["category"]) new_product = Product(**product) new_product.save()
def product_new(): if request.method == 'POST': flag, model = load_data() if not flag: return model id_1c = model.get('id_1c') if not id_1c: return jsonify({'success': False, 'msg': 'id_1c field not found.'}) is_exist = Product.query(Product.id_1c == id_1c).count() if is_exist: return jsonify({ 'success': False, 'msg': 'Product with id_1c: %s is exist' % id_1c }) product = Product() if model_populate(model, product): product.put() return jsonify({'success': True})