-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
162 lines (142 loc) · 5.5 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
from django.template.loader import render_to_string
from django.utils.encoding import smart_str
from django.utils.safestring import mark_safe
from django.http import JsonResponse
from django.conf import settings
import random
import string
import json
import importlib
import htmlement
import logging
import xml.etree.ElementTree as ET
from .utils import get_id, get_component_name, instance_class
from .utils import get_vars, snakecase
log = logging.getLogger(__name__)
def livewire_message(request, component_name):
inst = instance_class(component_name)
context = {}
if request.method == "POST":
context = inst.parser_payload(request)
resp = inst.render(**context)
return JsonResponse(resp, safe=False)
class LivewireTemplateTag:
def render_to_templatetag(self, **kwargs):
self.id = get_id()
component = self.get_component_name()
data = self.get_context_data(**kwargs)
initial_data = {
"id": self.id,
"name": component,
"redirectTo": False,
"events": [],
"eventQueue": [],
"dispatchQueue": [],
"data": data,
"children": {},
"effects": [],
"checksum": "9e4c194bb6aabf5f1", # TODO: checksum
}
context = {}
context["initial_data"] = initial_data
component_template = self.get_template_name()
self.render(**context)
return self.render_component(component_template, context)
class LivewireProcessData:
def fill(self, context): # Livewire Compatility https://laravel-livewire.com/docs/properties
self.update_context(context)
def update_context(self, data_context):
for key, value in data_context.items():
setattr(self, key, value)
context = self.get_context_data()
if data_context:
context.update(data_context)
return context
def parser_payload(self, request):
self.request = request
payload = json.loads(request.body)
self.id = payload.get("id")
data = payload.get("data", {})
action_queue = payload.get("actionQueue", [])
for action in action_queue:
action_type = action.get("type")
action_payload = action.get("payload")
if action_type == "callMethod":
self.update_context(payload.get("data"))
method = action_payload.get("method")
params = action_payload.get("params", [])
"""
TODO:
RUN THIS IT IS REALLY SAFE ???
https://www.toptal.com/python/python-design-patterns
patterns
"""
local_method = getattr(self, method)
return local_method(*params)
elif action_type == "syncInput":
data[action_payload["name"]] = action_payload["value"]
return self.update_context(data)
class LivewireComponent(LivewireTemplateTag, LivewireProcessData):
id = None
def get_component_name(self):
name = self.__class__.__name__.replace("Livewire", "")
name = snakecase(name)
return name
def get_template_name(self):
return self.template_name
def get_context_data(self, **kwargs):
mount_result = {}
# call mount if exists
if hasattr(self, "mount") and callable(self.mount): # Livewire Compatility
mount_result = self.mount()
params = get_vars(self)
for property in params:
mount_result[property] = getattr(self, property)
if kwargs:
mount_result.update(kwargs)
return mount_result
def get_dom(self, template_name, context):
context = self.get_context_data(**context)
return self.render_component(template_name, context)
def render(self, **context):
"""
A Livewire component's render method gets called on the initial page load AND every subsequent component update.
TODO: to Implement
"""
template_name = self.get_template_name()
return self.view(template_name, context)
def view(self, template_name, context):
dom = self.get_dom(template_name, context)
return self.render_to_response(template_name, dom)
def render_component(self, component_template, context={}):
initial_data = context.get("initial_data")
if initial_data:
del context["initial_data"]
component_render = render_to_string(
component_template, context=context
)
root = htmlement.fromstring(component_render).find("div")
root.set("wire:id", self.id)
if initial_data:
root.set("wire:initial-data", json.dumps(initial_data))
res = ET.tostring(root)
return mark_safe(smart_str(res))
def render_to_response(self, template_name, dom): # TODO: chnge to use render method on component view
json_response = {
"id": self.id,
"name": self.get_component_name(),
"dom": dom,
"fromPrefetch": "",
"redirectTo": "",
"children": [],
"dirtyInputs": [],
"data": self.get_context_data(),
"eventQueue": [],
"dispatchQueue": [],
"events": [],
"events":[],
"checksum": "c24",
}
if hasattr(self, "updates_query_string"):
json_response.update({'updatesQueryString': self.updates_query_string})
return json_response