def organization_edit(request, id): organization = Organization.objects.get(id=id) schema = OrganizationSchema() if request.method == "POST": try: data = schema.loads(request.body) except ValidationError as err: share_flash(request, error="Exists errors on form") share_flash(request, errors= err.messages) else: Organization.objects.filter(id=id).update(**data) share_flash(request, success="Updated Organization") return redirect(reverse("demo:organizations")) if request.method == "DELETE": organization.deleted = True organization.save() share_flash(request, success="Organization Deleted") return redirect(reverse("demo:organizations")) if request.method == "PUT": organization.deleted = False organization.save() share_flash(request, success="Organization Undeleted") return redirect(reverse("demo:organizations")) org = schema.dump(organization) props = { 'organization': org, 'errors': {} } return render_inertia(request, "Organizations.Edit", props)
def orders(request): orders = Order.objects.filter(user=request.user) props = { 'orders': OrderSerializer(orders, many=True).data, 'status': dict(Order.STATUS), } return render_inertia(request, "ListOrder", props)
def contact_edit(request, id): contact = Contact.objects.get(id=id) c = ContactSchema() org_schema = OrganizationSchema(many=True, only=("id", "name")) orgs = Organization.objects.all() if request.method == "POST": data = json.loads(request.body) try: c.load(data, unknown=INCLUDE) except ValidationError as err: share_flash(request, error="Exists errors on form") share_flash(request, errors=err.messages) else: Contact.objects.filter(id=id).update(**data) share_flash(request, success="Updated contact") return redirect(reverse("demo:contacts")) if request.method == "DELETE": contact.deleted = True contact.save() share_flash(request, success="Contact Deleted") return redirect(reverse("demo:contacts")) props = { 'contact': c.dump(contact), 'organizations': org_schema.dump(orgs) } return render_inertia(request, "Contacts.Edit", props)
def index(request): foods = Food.objects.all() context = { "foods": FoodSerializer(foods, many=True).data, "user": request.user.username } return render_inertia(request, "Index", context)
def test_share_custom_data(self): requestfactory = RequestFactory() request = requestfactory.get("/") self.set_session(request) response = render_inertia(request, "Index") self.assertDictEqual({"custom_data": "custom_value"}, request.session["share"])
def test_resolve_lazy_loading_props(self): requestfactory = RequestFactory() request = requestfactory.get("/") self.set_session(request) def lazy_loaded_prop(): return "2" response = render_inertia(request, "Index", { "a": "1", "b": lazy_loaded_prop }) self.assertTrue(b'"props": {"a": "1", "b": "2"}' in response.content)
def login(request): if request.user.is_authenticated: return redirect(reverse("market:index")) if request.method == "POST": data = json.loads(request.body) username = data.get("email") password = data.get("password") user = authenticate(request, username=username, password=password) if user is not None: django_login(request, user) return redirect(reverse("market:index")) else: share_flash(request, error="username wrongly") return render_inertia(request, "Login")
def organization_create(request): props = {} org_schema = OrganizationSchema() if request.method == "POST": try: data = org_schema.loads(request.body) obj = Organization.objects.create(**data) except ValidationError as err: share_flash(request, error="Exists errors on form") share_flash(request, errors= err.messages) else: share_flash(request, success=f"Organization {obj.name} created") return redirect(reverse("demo:organizations")) return render_inertia(request, "Organizations.Create", {})
def contacts(request): objects = Contact.objects.all() objects = _filter(request, objects, "first_name__icontains") trashed = request.GET.get("trashed", "") search = request.GET.get("search", "") args = ("id", "organization__name", "first_name", "last_name", 'city', 'phone') objs, links = _get_objs(request, objects, args, "demo:contacts") contact_schema = ContactSchema(many=True) props = { 'links': links, 'contacts': { "data": contact_schema.dump(objs)}, 'filters': { 'search': search, 'trashed': trashed, } } return render_inertia(request, "Contacts", props)
def login_view(request): errors = {'email':[]} if request.user.is_authenticated: return redirect(reverse("demo:dashboard")) if request.method == "POST": data = json.loads(request.body) username = data.get("email") password = data.get("password") user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return redirect(reverse("demo:dashboard")) else: errors = {'email':["Wrong login",]} share_flash(request, errors=errors) props = {'errors':errors} return render_inertia(request, "Login",props)
def organizations(request): org_sche = OrganizationSchema(many=True) objects = Organization.objects.all() objects = _filter(request, objects, "name__icontains") args = ("id","name", 'region','city','phone') objs, links = _get_objs(request, objects, args,"demo:organizations") trashed = request.GET.get("trashed","") search = request.GET.get("search", "") props = { 'filters': { 'search':search, 'trashed':trashed, }, 'organizations':{ 'links':links, 'data': org_sche.dump(objs) } } return render_inertia(request, "Organizations", props)
def contact_create(request): contact_schema = ContactSchema() org_schema = OrganizationSchema(many=True, only=("id", "name")) orgs = Organization.objects.all() if request.method == "POST": try: c = contact_schema.loads(request.body, unknown=INCLUDE) obj = Contact.objects.create(**c) except ValidationError as err: share_flash(request, error="Exists errors on form") share_flash(request, errors=err.messages) else: share_flash(request, success=f"Contact {obj.name} created") return redirect(reverse("demo:contacts")) props = { 'organizations': org_schema.dump(orgs) } return render_inertia(request, "Contacts.Create", props)
def test_partial_loading(self): defaults = { 'X-Inertia': 'true', 'X-Inertia-Version': get_version(), 'X-Requested-With': 'XMLHttpRequest', 'X-Inertia-Partial-Data': ["a"], "X-Inertia-Partial-Component": "Index" } requestfactory = RequestFactory() request = requestfactory.get("/") request.headers = defaults self.set_session(request) def lazy_loaded_prop(): return "2" response = render_inertia(request, "Index", { "a": "1", "b": lazy_loaded_prop }) # check that b is not returned because we only ask for a self.assertIn(b'"props": {"a": "1"},', response.content)
def index(request): # share_flash(request, errors=["yeah",]) return render_inertia(request, "Index")
def test_views(self): requestfactory = RequestFactory() request = requestfactory.get("/") response = render_inertia(request, "Index") self.assertTrue(b'id="page"' in response.content)
def test_views(self): requestfactory = RequestFactory() request = requestfactory.get("/") render_inertia(request, "Index")