def put(model, schema, name, param):
        try:
            obj = model.objects.get(id=param)
            json: dict = schema().dump(obj)
            json.update(request.get_json())

            njson = {}
            for key, value in json.items():
                if json[key]:
                    njson[key] = value
            json = njson

            json = schema().load(json)
            obj.update(**json)
            obj.reload()
            result = schema().dump(obj)
            log_write(
                f"Successful attempt to reach REST API PUT -> '{name}' with id {param}"
            )
        except (s.ValidationError, m.ValidationError, m.NotUniqueError,
                m.DoesNotExist) as e:
            log_write(
                f"Unsuccessful attempt to reach REST API PUT -> '{name}' with id {param}. Error: {str(e)}"
            )
            result = str(e)
        return jsonify(result)
 def post(model, schema, name):
     try:
         json = schema().load(request.get_json())
         result = schema().dump(model.objects.create(**json))
         log_write(f"Successful attempt to reach REST API POST -> '{name}'")
     except (s.ValidationError, m.NotUniqueError) as e:
         log_write(
             f"Unsuccessful attempt to reach REST API POST -> '{name}'. Error: {str(e)}"
         )
         result = str(e)
     return jsonify(result)
Example #3
0
def users_activity_check():
    customers = Customer.objects
    log_write("Blocked users collector started running")
    blocked = 0
    for customer in customers:
        try:
            bot.send_chat_action(customer.user_id, 'typing')
            customer.is_blocked = False
        except apihelper.ApiException as e:
            customer.is_blocked = True
            blocked += 1
        customer.save()
    log_write(f"Collector found {blocked} blocked users")
 def get(model, schema, name, param):
     if param is not None:
         try:
             obj = model.objects.get(id=param)
             result = schema().dump(obj)
             log_write(
                 f"Successful attempt to reach REST API GET -> '{name}' with id {param}"
             )
         except (s.ValidationError, m.ValidationError, m.DoesNotExist) as e:
             log_write(
                 f"Unsuccessful attempt to reach REST API GET -> '{name}' with id {param}. Error: {str(e)}"
             )
             result = str(e)
     else:
         objs = model.objects
         try:
             result = schema().dump(objs, many=True)
             log_write(
                 f"Successful attempt to reach REST API GET -> '{name}'")
         except s.ValidationError as e:
             log_write(
                 f"Unsuccessful attempt to reach REST API GET -> '{name}'. Error: {str(e)}"
             )
             result = str(e)
     return jsonify(result)
 def delete(model, name, param):
     if param:
         try:
             obj = model.objects.get(id=param)
             obj.delete()
             result = {'status': 'OK'}
         except (m.OperationError, m.ValidationError, m.DoesNotExist) as e:
             log_write(
                 f"Unsuccessful attempt to reach REST API DELETE -> '{name}' with id {param}. Error: {str(e)}"
             )
             result = str(e)
     else:
         log_write(
             f"Unsuccessful attempt to reach REST API DELETE -> '{name}'. Error: parameter 'id' not specified!"
         )
         result = {'status': 'Error'}
     return jsonify(result)
 def get(self, customer_id=None, property_name=None):
     if property_name is None:
         return GlobalResource.get(m.Customer, s.CustomerSchema, 'customer',
                                   customer_id)
     else:
         if property_name == 'carts':
             try:
                 customer = m.Customer.objects.get(id=customer_id)
                 carts = m.Cart.objects.filter(customer=customer)
                 result = s.CartSchema().dump(carts, many=True)
                 log_write(
                     f"Successful attempt to reach REST API GET -> 'customer' with id {customer_id}"
                 )
             except (s.ValidationError, m.ValidationError,
                     m.DoesNotExist) as e:
                 log_write(
                     f"Unsuccessful attempt to reach REST API GET -> 'customer' with id {customer_id}. Error: {str(e)}"
                 )
                 result = str(e)
             return jsonify(result)
         else:
             log_write(
                 f"Unsuccessful attempt to reach REST API GET -> 'customer' with id {customer_id}. Property {property_name} is invalid"
             )
             result = {"Status": "Error"}
             return jsonify(result)
Example #7
0
from web_shop.bot.setup import app, set_webhook, bot
from web_shop.db.seeder import seed, generate
from web_shop.bot.config import DEBUG
from web_shop.db.models import is_db_empty
from web_shop.api.setup import start as startAPI
from web_shop.log_writer import log_write, log_clear

log_write("\n\nRESTARTING BOT..\n\n")
log_write("STARTING LOGGING..")
if is_db_empty():
    generate(category=10, product=25, news=5)
if not DEBUG:
    log_write("STARTING API")
    startAPI()
    log_write("API STARTED SUCCESSFULLY")
    set_webhook()
    log_write("WEBHOOK SET SUCCESSFULLY")
    log_write("STARTING SERVER")
    app.run(port=8000)
else:
    startAPI()
    log_write("API STARTED SUCCESSFULLY")
    app.run(port=8000)
    bot.polling()