Exemplo n.º 1
0
 def get(self, request):
     try:
         print(request.user)
         if request.user.role.name == 'buyer':
             user_id = request.GET.get('user_id')
         elif request.user.role.name == 'seller':
             user_id = request.user.id
         else:
             return Response(
                 helpers.fail_context(message="Permission denied"),
                 status=status.HTTP_403_FORBIDDEN)
         if user_id is None or User.objects.filter(
                 id=user_id).first() is None:
             response = Response(
                 helpers.fail_context(message="user/toko tidak valid"),
                 status=status.HTTP_200_OK)
         else:
             product_data = Product.objects.filter(store=user_id).all()
             products = ProductSerializer(product_data, many=True).data
             response = Response(helpers.success_context(products=products),
                                 status=status.HTTP_200_OK)
     except Exception as e:
         print(str(e))
         response = Response(helpers.fatal_context(),
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return response
Exemplo n.º 2
0
    def post(self, request):
        try:
            user = request.user
            store_name = request.data.get('name')
            store_category = request.data.get('category')
            open_time = request.data.get('open_time')
            close_time = request.data.get('close_time')
            image_base64 = request.data.get('image')
            user_store_category = StoreCategory.objects.filter(
                id=store_category).first()
            if user.role.name != 'seller':
                response = Response(
                    helpers.fail_context(message="Permission denied"),
                    status=status.HTTP_403_FORBIDDEN)
            elif store_name is None or len(store_name) < 4:
                response = Response(
                    helpers.fail_context(message="Nama toko tidak valid"),
                    status=status.HTTP_200_OK)
            elif user_store_category is None:
                response = Response(
                    helpers.fail_context(message="Kategori toko tidak valid"),
                    status=status.HTTP_200_OK)
            elif open_time is None or close_time is None:
                response = Response(helpers.fail_context(
                    message="Jam operasional toko tidak valid"),
                                    status=status.HTTP_200_OK)
            else:
                image = None
                if image_base64 is not None:
                    image_data = b64decode(image_base64)
                    image_name = "store-" + str(uuid.uuid4()) + ".jpg"
                    file = ContentFile(image_data, image_name)
                    image = Image.objects.create(filename=image_name,
                                                 file=file)

                store_status = StoreStatus.objects.filter(name="open").first()
                if store_status is None:
                    store_status = StoreStatus.objects.create(name="open")
                Store.objects.filter(id=user.store.id).update(
                    name=store_name,
                    status=store_status,
                    open_time=open_time,
                    close_time=close_time,
                    category=user_store_category,
                    image=image)
                store = Store.objects.filter(id=user.store.id).first()

                data = StoreSerializer(store).data
                response = Response(helpers.success_context(store=data),
                                    status=status.HTTP_200_OK)
        except Exception as e:
            print(str(e))
            response = Response(helpers.fatal_context(),
                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        return response
Exemplo n.º 3
0
 def post(self, request):
     try:
         user = request.user
         latitude = request.data.get('latitude')
         longitude = request.data.get('longitude')
         if latitude is None or longitude is None or latitude == 0 or longitude == 0:
             response = Response(helpers.fail_context(
                 message="koordinat lokasi tidak valid"),
                                 status=status.HTTP_200_OK)
         else:
             geolocator = ArcGIS()
             location = geolocator.reverse(
                 str(latitude) + ", " + str(longitude))
             user_location = UserLocation.objects.filter(user=user).first()
             if user_location is None:
                 user_location = UserLocation.objects.create(user=user)
             user_location.latitude = latitude
             user_location.longitude = longitude
             user_location.current_address = location.address
             user_location.updated_at = datetime.utcnow()
             user_location.save()
             data = UserLocationSerializer(user_location).data
             response = Response(helpers.success_context(location=data),
                                 status=status.HTTP_200_OK)
     except Exception as e:
         print(str(e))
         response = Response(helpers.fatal_context(),
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return response
Exemplo n.º 4
0
 def post(self, request):
     try:
         name = request.data.get('name')
         username = request.data.get('username')
         password = request.data.get('password')
         role = request.data.get('role')
         phone_number = request.data.get('phone_number')
         user_role = Group.objects.filter(name=role).first()
         if User.objects.filter(username=username).first() is not None:
             response = Response(
                 helpers.fail_context(message="username sudah digunakan"),
                 status=status.HTTP_200_OK)
         elif user_role is None:
             response = Response(
                 helpers.fail_context(message="role tidak valid"),
                 status=status.HTTP_200_OK)
         elif password is None or len(password) < 5:
             response = Response(
                 helpers.fail_context(message="password tidak valid"),
                 status=status.HTTP_200_OK)
         elif username is None or len(username) < 5:
             response = Response(
                 helpers.fail_context(message="username tidak valid"),
                 status=status.HTTP_200_OK)
         elif phone_number is None or len(phone_number) < 10:
             response = Response(
                 helpers.fail_context(message="nomor hp tidak valid"),
                 status=status.HTTP_200_OK)
         else:
             user = User.objects.create(first_name=name,
                                        username=username,
                                        password=make_password(password),
                                        role=user_role,
                                        phone_number=phone_number)
             if user.role.name == 'seller':
                 store = Store.objects.create(name=username)
                 user.store = store
                 user.save()
             content = helpers.construct_login_return_content(user)
             response = Response(content, status=status.HTTP_200_OK)
     except Exception as e:
         print(str(e))
         response = Response(helpers.fatal_context(),
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return response
Exemplo n.º 5
0
 def post(self, request):
     try:
         user = authenticate(username=request.data.get('username'),
                             password=request.data.get('password'))
         if user is None:
             response = Response(helpers.fail_context(
                 message="username atau password tidak valid"),
                                 status=status.HTTP_401_UNAUTHORIZED)
         elif user.role.name != request.data.get('role'):
             response = Response(
                 helpers.fail_context(message="role tidak valid"),
                 status=status.HTTP_400_BAD_REQUEST)
         else:
             content = helpers.construct_login_return_content(user)
             response = Response(content, status=status.HTTP_200_OK)
     except Exception as e:
         print(str(e))
         response = Response(helpers.fatal_context(),
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return response
Exemplo n.º 6
0
 def post(self, request, req_id):
     try:
         r = Order.objects.filter(id=req_id).first()
         status_name = request.data.get('status')
         order_status = OrderStatus.objects.filter(name=status_name).first()
         if r is None:
             response = Response(helpers.fail_context(message="order tidak ditemukan"),
                                 status=status.HTTP_200_OK)
         elif order_status is None:
             response = Response(helpers.fail_context(message="order status salah"),
                                 status=status.HTTP_200_OK)
         else:
             r.status = order_status
             r.save()
             order_data = OrderSerializer(r).data
             response = Response(helpers.success_context(order=order_data), status=status.HTTP_200_OK)
     except Exception as e:
         print(str(e))
         response = Response(helpers.fatal_context(), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return response
Exemplo n.º 7
0
 def post(self, request):
     try:
         user = request.user
         name = request.data.get('name')
         print(name)
         price = request.data.get('price')
         image_base64 = request.data.get('image')
         if user.role.name != 'seller':
             response = Response(
                 helpers.fail_context(message="Permission denied"),
                 status=status.HTTP_403_FORBIDDEN)
         elif name is None or len(name) < 4:
             response = Response(helpers.fail_context(
                 message="nama produk anda tidak valid"),
                                 status=status.HTTP_200_OK)
         elif price is None or int(price) < 0:
             response = Response(helpers.fail_context(
                 message="harga produk anda tidak valid"),
                                 status=status.HTTP_200_OK)
         elif image_base64 is None:
             response = Response(
                 helpers.fail_context(message="Gambar tidak valid"),
                 status=status.HTTP_200_OK)
         else:
             image_data = b64decode(image_base64)
             image_name = "product-" + str(uuid.uuid4()) + ".jpg"
             file = ContentFile(image_data, image_name)
             image = Image.objects.create(filename=image_name, file=file)
             product = Product.objects.create(store=user,
                                              name=name,
                                              price=price,
                                              image=image)
             product_data = ProductSerializer(product).data
             response = Response(
                 helpers.success_context(product=product_data),
                 status=status.HTTP_200_OK)
     except Exception as e:
         print(str(e))
         response = Response(helpers.fatal_context(),
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return response
Exemplo n.º 8
0
 def post(self, request):
     try:
         seller_id = request.data.get('seller_id')
         product_ids = request.data.get('product_ids')
         latitude = request.data.get('latitude')
         longitude = request.data.get('longitude')
         note = request.data.get('note')
         address = request.data.get('address')
         seller = User.objects.filter(id=seller_id).first()
         user = request.user
         if seller is None or seller.role.name != 'seller':
             response = Response(helpers.fail_context(message="penjual yang anda pilih tidak valid"),
                                 status=status.HTTP_200_OK)
         elif address is None:
             response = Response(helpers.fail_context(message="alamat tidak valid"),
                                 status=status.HTTP_200_OK)
         elif user.role.name != 'buyer':
             response = Response(helpers.fail_context(message="Permission denied"),
                                 status=status.HTTP_403_FORBIDDEN)
         elif product_ids is None or len(product_ids) == 0:
             response = Response(helpers.fail_context(message="daftar produk tidak valid"),
                                 status=status.HTTP_200_OK)
         else:
             products = Product.objects.filter(pk__in=product_ids).all()
             total_price = 0
             for p in products:
                 total_price += p.price
             order = Order.objects.create(seller=seller, buyer=user, status_id=1, total_price=total_price,
                                          latitude=latitude, longitude=longitude, note=note, address=address)
             for p in products:
                 count = product_ids.count(p.id)
                 OrderItem.objects.create(order=order, item=p, count=count)
             request_data = OrderSerializer(order).data
             response = Response(helpers.success_context(order=request_data), status=status.HTTP_200_OK)
     except Exception as e:
         print(str(e))
         response = Response(helpers.fatal_context(), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return response
Exemplo n.º 9
0
 def get(self, request, category_id):
     try:
         store_category = StoreCategory.objects.filter(
             id=category_id).first()
         if category_id is None:
             response = Response(
                 helpers.fail_context(message="Kategori toko tidak valid"),
                 status=status.HTTP_200_OK)
         elif store_category is None:
             response = Response(
                 helpers.fail_context(message="Kategori toko tidak valid"),
                 status=status.HTTP_200_OK)
         else:
             stores = Store.objects.filter(category=store_category).all()
             users = User.objects.filter(store__in=stores).all()
             data = UserSerializer(users, many=True).data
             response = Response(helpers.success_context(users=data),
                                 status=status.HTTP_200_OK)
     except Exception as e:
         print(str(e))
         response = Response(helpers.fatal_context(),
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return response
Exemplo n.º 10
0
 def get(self, request):
     try:
         if request.user is None:
             response = Response(helpers.fail_context(message="Auth gagal"),
                                 status=status.HTTP_200_OK)
         else:
             stores = StoreCategory.objects.all()
             data = StoreCategorySerializer(stores, many=True).data
             response = Response(helpers.success_context(categories=data),
                                 status=status.HTTP_200_OK)
     except Exception as e:
         print(str(e))
         response = Response(helpers.fatal_context(),
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return response
Exemplo n.º 11
0
 def post(self, request):
     user = request.user
     image_base64 = request.data.get('image')
     try:
         if image_base64 is None:
             response = Response(
                 helpers.fail_context(message="Gambar tidak valid"),
                 status=status.HTTP_200_OK)
         else:
             image_data = b64decode(image_base64)
             image_name = "user-" + str(uuid.uuid4()) + ".jpg"
             file = ContentFile(image_data, image_name)
             image = Image.objects.create(filename=image_name, file=file)
             user.image = image
             user.save()
             data = UserSerializer(user).data
             response = Response(helpers.success_context(user=data),
                                 status=status.HTTP_200_OK)
     except Exception as e:
         print(str(e))
         response = Response(helpers.fatal_context(),
                             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return response
Exemplo n.º 12
0
 def get(self, request):
     try:
         user = request.user
         if user.role.name == 'seller':
             location = UserLocation.objects.get(user=user)
             orders = Order.objects.filter(seller=user).all()
             order_data = []
             for o in orders:
                 buyer_location = (location.latitude, location.longitude)
                 seller_location = (o.latitude, o.longitude)
                 distance = vincenty(buyer_location, seller_location).km
                 data = OrderSerializer(o).data
                 data['distance'] = distance
                 order_data += [data]
             response = Response(helpers.success_context(orders=order_data),
                                 status=status.HTTP_200_OK)
         elif user.role.name == 'buyer':
             orders = Order.objects.filter(buyer=user).all()
             order_data = []
             for o in orders:
                 print(o.seller)
                 location = UserLocation.objects.get(user=o.seller)
                 buyer_location = (location.latitude, location.longitude)
                 seller_location = (o.latitude, o.longitude)
                 distance = vincenty(buyer_location, seller_location).km
                 data = OrderSerializer(o).data
                 data['distance'] = distance
                 order_data += [data]
             response = Response(helpers.success_context(orders=order_data),
                                 status=status.HTTP_200_OK)
         else:
             response = Response(helpers.fail_context(message="Permission denied"),
                                 status=status.HTTP_403_FORBIDDEN)
     except Exception as e:
         print(str(e))
         response = Response(helpers.fatal_context(), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return response