Skip to content

Commit 116e181

Browse files
author
Andres Vargas
committed
Other Component
1 parent aa36369 commit 116e181

File tree

12 files changed

+89
-25
lines changed

12 files changed

+89
-25
lines changed

app/settings.py

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# Application definition
3232

3333
INSTALLED_APPS = [
34+
'bsync',
3435
'django.contrib.admin',
3536
'django.contrib.auth',
3637
'django.contrib.contenttypes',
@@ -39,6 +40,7 @@
3940
'django.contrib.staticfiles',
4041
'core',
4142
'livewire',
43+
'django_seed',
4244
]
4345

4446
MIDDLEWARE = [

app/urls.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from django.contrib import admin
22
from django.urls import path, include
33
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
4-
from core.views import index
4+
from django.views.generic import TemplateView as TV
55

66
urlpatterns = [
77
path('admin/', admin.site.urls),
88
path('', include("livewire.urls")),
9-
path('', index),
9+
path('counter', TV.as_view(template_name="counter.html"), name="counter"),
10+
path('', TV.as_view(template_name="index.html"), name="index"),
1011
] + staticfiles_urlpatterns()

core/migrations/0001_initial.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generated by Django 3.0.4 on 2020-03-22 06:03
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Post',
16+
fields=[
17+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('title', models.CharField(max_length=100)),
19+
('context', models.TextField()),
20+
('created_at', models.DateTimeField(auto_now_add=True)),
21+
('updated_at', models.DateTimeField(auto_now=True)),
22+
],
23+
),
24+
]

core/models.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
from django.db import models
22

3-
# Create your models here.
3+
4+
class Post(models.Model):
5+
title = models.CharField(max_length=100)
6+
context = models.TextField()
7+
8+
created_at = models.DateTimeField(auto_now_add=True)
9+
updated_at = models.DateTimeField(auto_now=True)
10+
11+
def __str__(self):
12+
return self.title

core/templates/base.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ <h1 class="title">
1818
My first website with <strong>django-livewire</strong>!
1919
</p>
2020

21-
{% livewire 'counter' %}
22-
21+
{% block content %}
22+
{% endblock %}
2323
</div>
2424
</section>
2525

core/templates/counter.html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% extends "base.html" %}
2+
{% load livewire_tags %}
3+
{% block content %}
4+
5+
{% livewire "counter" %}
6+
7+
{% endblock content %}

core/templates/delete_post.livewire.html

Whitespace-only changes.

core/templates/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends "base.html" %}
2+
{% load livewire_tags %}
3+
{% block content %}
4+
5+
<div class="content">
6+
7+
<ul>
8+
<li>
9+
<a href=" {% url 'counter' %}" >counter</a>
10+
</li>
11+
</ul>
12+
</div>
13+
{% endblock content %}

core/views.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
from django.shortcuts import render
22
from livewire.views import LivewireComponent
33

4-
def index(request):
5-
context = {}
6-
return render(request, "base.html", context)
7-
8-
94

105
class CounterLivewire(LivewireComponent):
116
component_name = "counter"
12-
context = {
13-
'count': 0
14-
}
7+
count = 2
158
def decrement(self, *args):
16-
self.context["count"] -=1
9+
self.count -=1
1710

1811
def increment(self, *args):
19-
self.context["count"]+=1
12+
self.count +=1
13+
14+
def get_context(self):
15+
return {
16+
'count': self.count
17+
}
2018

livewire/templatetags/livewire_tags.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ def livewire(component,**kwargs):
1111
# TODO: register Components by livewire.register(ComponentLivewire)
1212
from core.views import CounterLivewire
1313
livewire_component = CounterLivewire()
14+
print(livewire_component.get_context())
1415
return livewire_component.render_initial()

livewire/views.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,12 @@ def livewire_message(request, component_name):
2323
def get_id():
2424
return ''.join(random.choice(string.ascii_lowercase) for i in range(20))
2525

26-
class LivewireComponent():
26+
class LivewireComponent(object):
2727
id = None
28-
context = {}
2928

3029
def get_component_name(self):
3130
return self.component_name.lower()
3231

33-
def get_context(self, **kwargs):
34-
self.context.update(kwargs)
35-
return self.context
36-
3732
def get_dom(self):
3833
context = self.get_context()
3934
return self._render_component(context)
@@ -55,9 +50,17 @@ def get_response(self):
5550
'checksum': "c24"
5651
}
5752

53+
def update_context(self, data_context):
54+
context = self.get_context()
55+
context.update(data_context)
56+
for key, value in context.items():
57+
setattr(self, key,value)
58+
return context
59+
60+
5861
def parser_payload(self, payload):
5962
self.id = payload.get("id")
60-
self.context.update(payload.get("data"))
63+
self.update_context(payload.get("data"))
6164
action_queue = payload.get("actionQueue", [])
6265
for action in action_queue:
6366
action_type = action.get("type")
@@ -66,7 +69,10 @@ def parser_payload(self, payload):
6669
method = payload.get("method")
6770
params = payload.get("params",[])
6871
"""
69-
RUN THIS IT IS REALLY SAFE ???
72+
TODO:
73+
RUN THIS IT IS REALLY SAFE ???
74+
https://www.toptal.com/python/python-design-patterns
75+
patterns
7076
"""
7177
local_method = getattr(self, method)
7278
local_method(*params)
@@ -94,12 +100,12 @@ def render_initial(self):
94100
initial_data = {
95101
'id': self.id,
96102
'name': component,
97-
'redirectTo':"",
103+
'redirectTo': False,
98104
"events": [],
99105
"eventQueue": [],
100106
"dispatchQueue": [],
101107
"data": context,
102-
"children": [],
108+
"children": {},
103109
"checksum": "9e4c194bb6aabf5f1" # TODO: checksum
104110
}
105111
return self._render_component(context, initial_data=initial_data)

requirements.in

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
django
2+
django-bsync
3+
django-seed

0 commit comments

Comments
 (0)