import urllib.request import urllib.error import json import ssl import time ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE endpoint = 'https://appwrite.almeira.com.co/v1' project = '69b84df5003bd8957c5c' key = 'standard_cce3cdb866ea6433888d40cedd24dc45987372308fdc3dd0697de08648c0e5c7e04b02c3bcee271403fd656b00a5820f04c08b51631284a6e8a3582d8aed2aa33234f3c29cc777e9b162eb54810b789251b0d006a9a3b081ebc2da53b9baa94614b124b3508ad849fd9a4187cc6094f506d4b57f409b69d9562a74df594f3ec3' db = 'sneakers_multistore_db' col_id = 'custom_requests' headers = { 'X-Appwrite-Project': project, 'X-Appwrite-Key': key, 'Content-Type': 'application/json' } # 1. Crear colección si no existe check_url = f'{endpoint}/databases/{db}/collections/{col_id}' try: req = urllib.request.Request(check_url, headers=headers) with urllib.request.urlopen(req, context=ctx, timeout=10) as r: print('[OK] La colección custom_requests ya existe en Appwrite.') except urllib.error.HTTPError as e: if e.code == 404: print('Creando colección custom_requests...') url = f'{endpoint}/databases/{db}/collections' body = json.dumps({ 'collectionId': col_id, 'name': 'Encargos y Pedidos Especiales', 'permissions': ['read("any")', 'create("any")', 'update("any")', 'delete("any")'] }).encode('utf-8') req = urllib.request.Request(url, data=body, headers=headers) with urllib.request.urlopen(req, context=ctx, timeout=10) as r: print('[OK] Colección creada:', r.read().decode()) else: print('Error consultando colección:', e.code, e.read().decode()) # 2. Atributos attributes = [ {'type': 'string', 'key': 'customerName', 'size': 255, 'required': True}, {'type': 'string', 'key': 'customerPhone', 'size': 64, 'required': True}, {'type': 'string', 'key': 'customerEmail', 'size': 255, 'required': False}, {'type': 'string', 'key': 'customerCity', 'size': 128, 'required': False}, {'type': 'string', 'key': 'brand', 'size': 128, 'required': True}, {'type': 'string', 'key': 'modelName', 'size': 255, 'required': True}, {'type': 'string', 'key': 'size', 'size': 32, 'required': True}, {'type': 'string', 'key': 'colorway', 'size': 255, 'required': False}, {'type': 'integer', 'key': 'targetBudget', 'required': False, 'min': 0, 'max': 100000000}, {'type': 'string', 'key': 'notes', 'size': 2000, 'required': False}, {'type': 'string', 'key': 'imageUrl', 'size': 1000, 'required': False}, {'type': 'string', 'key': 'status', 'size': 32, 'required': True}, {'type': 'integer', 'key': 'quotedPrice', 'required': False, 'min': 0, 'max': 100000000}, {'type': 'string', 'key': 'adminNotes', 'size': 2000, 'required': False}, {'type': 'string', 'key': 'createdAt', 'size': 64, 'required': False}, {'type': 'string', 'key': 'updatedAt', 'size': 64, 'required': False}, {'type': 'string', 'key': 'userId', 'size': 128, 'required': False}, ] attr_base_url = f'{endpoint}/databases/{db}/collections/{col_id}/attributes' for attr in attributes: attr_type = attr['type'] key_name = attr['key'] url = f'{attr_base_url}/{attr_type}' payload = { 'key': key_name, 'required': attr['required'] } if attr_type == 'string': payload['size'] = attr['size'] elif attr_type == 'integer': if 'min' in attr: payload['min'] = attr['min'] if 'max' in attr: payload['max'] = attr['max'] req = urllib.request.Request(url, data=json.dumps(payload).encode('utf-8'), headers=headers) try: with urllib.request.urlopen(req, context=ctx, timeout=10) as r: print(f' [+] Atributo {key_name} ({attr_type}) creado.') except urllib.error.HTTPError as e: if e.code == 409: print(f' [=] Atributo {key_name} ya existe.') else: print(f' [!] Error en {key_name}:', e.code, e.read().decode()) time.sleep(0.3) print('[FIN] Colección y atributos configurados en Appwrite.')