-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathviews.py
184 lines (160 loc) · 6.23 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from django.shortcuts import render, redirect
from inertia.views import render_inertia, InertiaMixin
from inertia.share import share, share_flash
from .models import Contact, Organization
from django.contrib import messages
from django.core.paginator import Paginator, EmptyPage
from django.urls import reverse
from .utils import _get_objs, _filter
from django.forms import model_to_dict
from .serializers import ContactSchema, OrganizationSchema
import json
from marshmallow import INCLUDE, ValidationError
from django.contrib.auth import authenticate, login, logout
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 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 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 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 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 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 index(request):
# share_flash(request, errors=["yeah",])
return render_inertia(request, "Index")
def logout_view(request):
logout(request)
return redirect(reverse("demo:login"))
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)