def update_shopkeeper_picture(self, s):
        authentic = common.is_user_authenticated()
        if (not authentic):
            return common.make_response_packet(4, "User is not authenticated",
                                               None)
        target = os.path.abspath("static/")
        if (not s['shopkeeper_id']):
            return common.make_response_packet(5, 'Shopkeeper id is required',
                                               None)
        shop = ShopKeepers.query.filter(
            ShopKeepers.id == s['shopkeeper_id']).first()
        if (not shop):
            return common.make_response_packet(6, 'shopkeeper_id is not valid',
                                               None)
        user_folder_create = os.path.join(target, shop.user_name)
        if not os.path.isdir(user_folder_create):
            os.mkdir(user_folder_create)
        user_profile_direc = os.path.join(user_folder_create, "profile_pics")
        print(target)
        print(user_profile_direc)
        if not os.path.isdir(user_profile_direc):
            os.mkdir(user_profile_direc)

        f = s['file_attachement']
        f = bytes(f, 'utf-8')

        filename = uuid.uuid1().hex
        shop.image = filename
        destination = "/".join([user_profile_direc, filename])
        print(destination)
        with open(destination + ".jpg", "wb") as fh:
            fh.write(base64.decodebytes(f))
        db_session.commit()
        return common.make_response_packet(1, 'Image Updated Successfully',
                                           'Server Data')
    def get_shopkeeper_picture(self, s):
        authentic = common.is_user_authenticated()
        if (not authentic):
            return common.make_response_packet(4, "User is not authenticated",
                                               None)
        target = os.path.abspath("static/")
        if (not s['shopkeeper_id']):
            return common.make_response_packet(5, 'Shopkeeper id is required',
                                               None)
        shop = ShopKeepers.query.filter(
            ShopKeepers.id == s['shopkeeper_id']).first()
        if (not shop):
            return common.make_response_packet(6, 'shopkeeper_id is not valid',
                                               None)
        if shop.image:
            user_profile_direc = os.path.join(
                target, shop.user_name + "\profile_pics\\" + shop.image)
            print(target)
            print(user_profile_direc)

            with open(user_profile_direc + ".jpg", "rb") as image_file:
                my_string = base64.b64encode(image_file.read())

            return common.make_response_packet(1, 'success',
                                               my_string.decode('utf-8'))
        else:
            user_profile_direc = (target + "\\default.jpg")
            print(target)
            print(user_profile_direc)

            with open(user_profile_direc, "rb") as image_file:
                my_string = base64.b64encode(image_file.read())

            return common.make_response_packet(1, 'success',
                                               my_string.decode('utf-8'))
 def process_insert_customers(self, c: Customers):
     if (not c.user_name or not c.customer_name or not c.image or not c.cnic_no or not c.password or not c.customer_phone_no):
         return common.make_response_packet(6,"data is not valid",None)
     is_user_exist =Customers.query.filter(Customers.user_name == c.user_name).first() != None
     if (is_user_exist):
         return common.make_response_packet(7, "User name already in use", None)
     c.password=generate_password_hash(c.password)
     db_session.add(c)
     db_session.commit()
     return common.make_response_packet(0, "customer inserted successfully", c.toDict())
 def validate_product(self, a: Products):
     if (not a.product_name or not a.price or not a.image1
             or not a.brand_id):
         return common.make_response_packet(6, "data is not valid", None)
     if (not common.is_number(a.price)):
         return common.make_response_packet(10, "Price is not valid number",
                                            None)
     brnd = Brands.query.filter(Brands.id == a.brand_id).first()
     if (brnd == None):
         return common.make_response_packet(11, "Brand does not exist",
                                            None)
     return True
 def get_product_by_id(self, id):
     authentic = common.is_user_authenticated()
     if (not authentic):
         return common.make_response_packet(4, "User is not authenticated",
                                            None)
     if (not id):
         return common.make_response_packet(5, "Id is not valid", None)
     s = ShopKeepers.query.filter(ShopKeepers.id == id).first()
     if (not s):
         return common.make_response_packet(6, "Shopkeeper does not exist",
                                            None)
     res = Products.query.join(Brands).filter(
         Brands.shopkeeper_id == id).all()
     resList = [res1.toDict() for res1 in res]
     return common.make_response_packet(0, "", resList)
    def insert_bankcaccount(self, a: Bankaccount):
        authentic = common.is_user_authenticated()
        if (not authentic):
            return common.make_response_packet(4, "User is not authenticated",
                                               None)
        if (not common.is_customer()):
            return common.make_response_packet(7, "You are not a Customer",
                                               None)
        validated = self.validate_bankaccount(a)
        if (validated != True):
            return validated

        db_session.add(a)
        db_session.commit()
        return common.make_response_packet(
            0, "Bank Account Successfully Inserted", None)
    def process_login(self,user_name,password,user_type):
        s = None
        if user_type == UserType.ShopKeeper:
            s = ShopKeepers.query.filter( ShopKeepers.user_name == user_name ).first()
        elif user_type == UserType.Customer:
            s = Customers.query.filter(Customers.customer_name == user_name).first()
        else:
            return common.make_response_packet(1, "Not valid user type", None)

        if(s == None):
            return common.make_response_packet(2,  "User Name Not Found", None)
        if(not check_password_hash(s.password,password)):
            return common.make_response_packet(3, "User Name or Password is Incorrect", None)
        session[constants.is_authenticated] = True
        session[constants.user_type] = user_type
        return  common.make_response_packet(0,"Login Successfully", None)
 def process_insert_shopkeeper(self,s):
     if(not s.user_name or not s.owner_name or  not s.shop_name or not s.owner_phone_no or not s.password or not s.address):
         return common.make_response_packet(6, "data is not valid", s.toDict())
     is_user_exist = ShopKeepers.query.filter(ShopKeepers.user_name == s.user_name).first() != None
     if(is_user_exist):
         return common.make_response_packet(7, "User name already in use", None)
     is_ownerphone_no_exist = ShopKeepers.query.filter(ShopKeepers.owner_phone_no == s.owner_phone_no).first() != None
     if (is_ownerphone_no_exist):
         return common.make_response_packet(7, "Phone number already in use", None)
     #is_shop_name_exist = ShopKeepers.query.filter(ShopKeepers.shop_name == s.shop_name).first() != None
     #if (is_shop_name_exist):
       #  return common.make_response_packet(7, "shop name already in use", None)
     s.password=generate_password_hash(s.password)
     db_session.add(s)
     db_session.commit()
     return common.make_response_packet(0, "shop keeper inserted successfully", s.toDict())
    def process_insert_orders(self, a: Orders):
        authentic = common.is_user_authenticated()
        if (not authentic):
            return common.make_response_packet(4, "User is not authenticated",
                                               None)
        if (not common.is_customer()):
            return common.make_response_packet(7,
                                               "you are not insert the order",
                                               None)
        validated = self.validate_order(a)
        if (validated != True):
            return validated

        db_session.add(a)
        db_session.commit()
        return common.make_response_packet(2, "Order Successfully Inserted",
                                           a.toDict())
 def process_insert_product_in_orders(self, a: Products_In_Orders):
     authentic = common.is_user_authenticated()
     if (not authentic):
         return common.make_response_packet(4, "User is not authenticated",
                                            None)
     if (not common.is_customer()):
         return common.make_response_packet(7,
                                            "you are not insert the order",
                                            None)
     if (not a.quantity or not a.product_id or not a.order_id):
         return common.make_response_packet(3, "data is not valid", None)
     if (not common.is_number(a.quantity)):
         return common.make_response_packet(8, "Quanitity is not Valid",
                                            None)
     pd_id = Products.query.filter(Products.id == a.product_id).first()
     if (pd_id == None):
         return common.make_response_packet(9, "Product id is not found",
                                            None)
     od_id = Orders.query.filter(Orders.id == a.order_id).first()
     if (od_id == None):
         return common.make_response_packet(9, "Order id is not found",
                                            None)
     if not a.date:
         import datetime
         a.date = datetime.datetime.now()
     db_session.add(a)
     db_session.commit()
     return common.make_response_packet(
         0, "Process Order Successfully Placed", a.toDict())
 def update_shopkeeper(self,s):
     authentic = common.is_user_authenticated()
     if (not authentic):
         return common.make_response_packet(4, "User is not authenticated", None)
     if (not s['shopkeeper_id']):
         return common.make_response_packet(5,'shopkeeper id is required',None)
     shop = ShopKeepers.query.filter(ShopKeepers.id== s['shopkeeper_id']).first()
     if (not shop):
         return common.make_response_packet(6,'shopkeeper_id is not valid',None)
     keys = shop.__table__.columns
     updated =False
     for k in keys:
         updated |= common.check_and_update(shop,s,k.name)
     if(updated):
         if(not shop.user_name or not shop.owner_name or  not shop.shop_name or not shop.owner_phone_no or not shop.password or not shop.address):
             return common.make_response_packet(6, "Data is not valid", None)
         is_user_exist = ShopKeepers.query.filter(and_(ShopKeepers.user_name == shop.user_name,ShopKeepers.id != shop.id)).first() != None
         if (is_user_exist):
             return common.make_response_packet(7, "User name already in use", None)
         #is_shop_name_exist = ShopKeepers.query.filter(and_(ShopKeepers.shop_name == shop.shop_name,ShopKeepers.id != shop.id)).first() != None
         #if (is_shop_name_exist):
           #  return common.make_response_packet(7, "shop name already in use", None)
         db_session.commit()
         return common.make_response_packet(0, "Shopkeeper updated successfully", shop.toDict())
     else:
         return common.make_response_packet(1, 'Nothing Updated', shop.toDict())
    def process_insert_product(self, a: Products):
        authentic = common.is_user_authenticated()
        if (not authentic):
            return common.make_response_packet(4, "User is not authenticated",
                                               None)

        validated = self.validate_product(a)
        if (validated != True):
            return validated

        if (a.image1):
            a.image1 = bytes(a.image1, 'utf-8')
        if (a.image2):
            a.image2 = bytes(a.image2, 'utf-8')
        if (a.image3):
            a.image3 = bytes(a.image3, 'utf-8')

        db_session.add(a)
        db_session.commit()
        return common.make_response_packet(0, "product inserted sucessfully",
                                           a.toDict())
 def get_brands(self, br):
     authentic = common.is_user_authenticated()
     if (not authentic):
         return common.make_response_packet(4, "User is not authenticated",
                                            None)
     if 'shopkeeper_id' not in br:
         return common.make_response_packet(5, 'Shop Keeper id is required',
                                            None)
     brn = Brands.query.filter(
         Brands.shopkeeper_id == br['shopkeeper_id']).all()
     brands_data = []
     if brn:
         for brands in brn:
             brands_data.append({
                 'id': brands.id,
                 'name': brands.brand_name,
                 'own_brand': brands.own_brand
             })
         return jsonify(brands_data)
     else:
         return common.make_response_packet(4, "No Data", None)
 def update_pass_shop(self, s):
     authentic = common.is_user_authenticated()
     if (not authentic):
         return common.make_response_packet(4, "User is not authenticated",
                                            None)
     if (not 'user_name' in s):
         return common.make_response_packet(5, 'User name is required',
                                            None)
     shop = ShopKeepers.query.filter(
         ShopKeepers.user_name == s['user_name']).first()
     if (not shop):
         return common.make_response_packet(6, 'user_name is not valid',
                                            None)
     if ('old_password' not in s or 'new_password' not in s):
         return common.make_response_packet(21, "Data is not valid", None)
     correct = check_password_hash(shop.password, s['old_password'])
     if correct:
         #issubclass(hifi,price,discount){
         #   juf = id.__annotations__.id.not
         #}
         shop.password = generate_password_hash(s['new_password'])
         db_session.commit()
         return common.make_response_packet(
             0, "Password Updated Successfully", 'Server Data')
     else:
         return common.make_response_packet(1, 'Incorrect Old Password',
                                            'Server Data')
 def update_bank(self, bnk):
     authentic = common.is_user_authenticated()
     if (not authentic):
         return common.make_response_packet(4, "User is not authenticated",
                                            None)
     if 'bank_id' not in bnk:
         return common.make_response_packet(5, 'Bank id is required', None)
     bank = Bankaccount.query.filter(
         Bankaccount.id == bnk['bank_id']).first()
     if (not bank):
         return common.make_response_packet(6, 'Bank_id is not valid', None)
     keys = bank.__table__.columns
     updated = False
     for k in keys:
         updated |= common.check_and_update(bank, bnk, k.name)
     if (updated):
         if (not bank.acount_no or not bank.acount_holder_name
                 or not bank.bank_name):
             return common.make_response_packet(3, "data is not valid",
                                                None)
         db_session.commit()
         return common.make_response_packet(
             0, "Bank Information updated successfully", bank.toDict())
     else:
         return common.make_response_packet(1, 'Nothing Updated',
                                            bank.toDict())
 def update_order(self, ord):
     authentic = common.is_user_authenticated()
     if (not authentic):
         return common.make_response_packet(4, "User is not authenticated",
                                            None)
     if 'order_id' not in ord:
         return common.make_response_packet(5, 'order id is required', None)
     orde = Orders.query.filter(Orders.id == ord['order_id']).first()
     if (not orde):
         return common.make_response_packet(6, 'order_id is not valid',
                                            None)
     keys = orde.__table__.columns
     updated = False
     for k in keys:
         updated |= common.check_and_update(orde, ord, k.name)
     if (updated):
         if (not orde.transported_point):
             db_session.refresh(orde)
             return common.make_response_packet(3, "data is not valid",
                                                None)
         db_session.commit()
         return common.make_response_packet(0, "order updated successfully",
                                            orde.toDict())
     else:
         return common.make_response_packet(1, 'Nothing Updated',
                                            orde.toDict())
    def process_insert_brands(self, a: Brands):
        authentic = common.is_user_authenticated()
        if (not authentic):
            return common.make_response_packet(4, "User is not authenticated",
                                               None)
        if (not common.is_shopkeeper()):
            return common.make_response_packet(
                6, "You are not authorize to add brands", None)
        validated = self.validate_brands(a)
        if (validated != True):
            return validated
        select_shop_keeper = Brands.query.filter(
            Brands.shopkeeper_id == a.shopkeeper_id).all()
        if select_shop_keeper:
            for i in range(len(select_shop_keeper)):
                if a.brand_name == select_shop_keeper[i].brand_name:
                    return common.make_response_packet(
                        7, "Brand Name already exists", None)

        db_session.add(a)
        db_session.commit()
        return common.make_response_packet(0, "Brand inserted successfully",
                                           a.toDict())
    def update_product(self, a):
        authentic = common.is_user_authenticated()
        if (not authentic):
            return common.make_response_packet(4, "User is not authenticated",
                                               None)
        if (not a['product_id']):
            return common.make_response_packet(14, "product id is required",
                                               None)
        prod = Products.query.filter(Products.id == a['product_id']).first()
        if (not prod):
            return common.make_response_packet(13, "Product Id not valid",
                                               None)

        keys = prod.__table__.columns
        updated = False
        for k in keys:
            updated |= common.check_and_update(prod, a, k.name)

        if updated:
            validated = self.validate_product(prod)
            if (validated != True):
                db_session.refresh(prod)
                return validated
            if ('image1' in a):
                prod.image1 = bytes(prod.image1, 'utf-8')
            if ('image2' in a):
                prod.image2 = bytes(prod.image2, 'utf-8')
            if ('image3' in a):
                prod.image3 = bytes(prod.image3, 'utf-8')
            db_session.commit()
            return common.make_response_packet(0,
                                               "product updated successfully",
                                               prod.toDict())
        else:
            return common.make_response_packet(1, 'Nothing Updated',
                                               prod.toDict())
 def update_pass_cus(self,c):
     authentic = common.is_user_authenticated()
     if (not authentic):
         return common.make_response_packet(4, "User is not authenticated", None)
     if (not 'user_name' in c):
         return common.make_response_packet(5, 'user name is required', None)
     cus = Customers.query.filter(Customers.user_name == c['user_name']).first()
     if (not cus):
         return common.make_response_packet(6, 'user_name is not valid', None)
     if('old_password' not in c or 'new_password' not in c):
         return common.make_response_packet(21,"Data is not valid",None)
     correct = check_password_hash(cus.password,c['old_password'])
     if correct:
         cus.password = generate_password_hash(c['new_password'])
         db_session.commit()
         return common.make_response_packet(0, "Password Updated Successfully", cus.toDict())
     else:
         return common.make_response_packet(1, 'Nothing Updated', cus.toDict())
 def update_customer(self,c):
     authentic = common.is_user_authenticated()
     if (not authentic):
         return common.make_response_packet(4, "User is not authenticated", None)
     if (not 'customer_id' in c):
         return common.make_response_packet(5, 'customer id is required', None)
     cus = Customers.query.filter(Customers.id == c['customer_id']).first()
     if (not cus):
         return common.make_response_packet(6,'customer_id is not valid',None)
     keys = cus.__table__.columns
     updated = False
     for k in keys:
         updated |= common.check_and_update(cus,c, k.name)
     if (updated):
         if (not cus.user_name or not cus.customer_name or not cus.image or not cus.cnic_no or not cus.password or not cus.customer_phone_no):
             return common.make_response_packet(6, "Data is not valid", None)
         is_user_exist = Customers.query.filter(and_(Customers.user_name == cus.user_name, Customers.id != cus.id)).first() != None
         if (is_user_exist):
             db_session.refresh(cus)
             return common.make_response_packet(7, "User name already in use", None)
         db_session.commit()
         return common.make_response_packet(0, "customer updated successfully", cus.toDict())
     else:
         return common.make_response_packet(1, 'Nothing Updated', cus.toDict())
    def update_brand(self, br):
        authentic = common.is_user_authenticated()
        if (not authentic):
            return common.make_response_packet(4, "User is not authenticated",
                                               None)
        if 'id' not in br:
            return common.make_response_packet(5, 'brand id is required', None)
        if br.get('brand_name'):
            select_shop_keeper = Brands.query.filter(
                Brands.shopkeeper_id == br['shopkeeper_id']).all()
            if select_shop_keeper:
                for i in range(len(select_shop_keeper)):
                    if br['brand_name'] == select_shop_keeper[i].brand_name:
                        return common.make_response_packet(
                            7, "Brand Name already exists", None)

        brn = Brands.query.filter(
            Brands.id == br['id']
            and Brands.shopkeeper_id == br['shopkeeper_id']).first()
        if (not brn):
            return common.make_response_packet(6, 'Brand_id is not valid',
                                               None)
        keys = brn.__table__.columns
        updated = False
        for k in keys:
            updated |= common.check_and_update(brn, br, k.name)
        if (updated):
            if (not common.is_shopkeeper()):
                return common.make_response_packet(
                    6, "You are not authorize to update brands", None)
            db_session.commit()
            return common.make_response_packet(0, "Brand successfully updated",
                                               brn.toDict())
        else:
            return common.make_response_packet(1, 'Nothing Updated',
                                               brn.toDict())
 def delete_brand(self, br):
     authentic = common.is_user_authenticated()
     if (not authentic):
         return common.make_response_packet(4, "User is not authenticated",
                                            None)
     if 'id' not in br:
         return common.make_response_packet(5, 'brand id is required', None)
     brn = Brands.query.filter(
         Brands.id == br['id']
         and Brands.shopkeeper_id == br['shopkeeper_id']).first()
     if (not brn):
         return common.make_response_packet(6, 'Data is not valid', None)
     if brn:
         if (not common.is_shopkeeper()):
             return common.make_response_packet(
                 6, "You are not authorize to delete brands", None)
         db_session.delete(brn)
         db_session.commit()
         return common.make_response_packet(0, "Brand successfully Deleted",
                                            '')
     else:
         return common.make_response_packet(1, 'Nothing Deleted', '')
 def process_logout(self):
     session[constants.is_authenticated] = None
     session[constants.user_type] = None
     return common.make_response_packet(0,"logout successfully",None)
 def validate_bankaccount(self, a: Bankaccount):
     if (not a.acount_no or not a.acount_holder_name or not a.bank_name):
         return common.make_response_packet(6, "Data is not Valid", None)
     return True
 def validate_brands(self, a: Brands):
     if (not a.shopkeeper_id or not a.brand_name):
         return common.make_response_packet(1, "data is not valid", None)
     return True
 def validate_order(self, a: Orders):
     if (not a.transported_point or not a.customer_id
             or not a.shopkeeper_id):
         return common.make_response_packet(3, "data is not valid", None)
     return True