Skip to content

Commit 742d486

Browse files
committed
Initial Commit
1 parent d7072ee commit 742d486

37 files changed

+571
-1
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This is the top-most .editorconfig file (do not search in parent directories)
2+
root = true
3+
4+
### All files
5+
[*]
6+
charset = utf-8
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
end_of_line = lf

README.md

+43-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
1-
cdc-python-netcore
1+
Debezium is an open source project for change data capture (CDC).
2+
This example has two components to demonstrate the utilities.
3+
- A Django application with a MySQL database that saves data.
4+
- A .NET Core application with a PostgreSQL database that consumes this data.
5+
6+
Debezium
7+
8+
Requirements
9+
Just docker
10+
11+
12+
Create your Django app:
13+
14+
docker-compose run --rm --no-deps web django-admin startproject django_cdc .
15+
16+
Modify src/django_cdc/settings.py
17+
18+
DATABASES = {
19+
'default': {
20+
'ENGINE': 'django.db.backends.mysql',
21+
'NAME': 'djangodb',
22+
'USER': 'django',
23+
'PASSWORD': 'django',
24+
'HOST': 'mysql',
25+
'PORT': 3306,
26+
}
27+
}
28+
29+
Run:
30+
31+
docker-compose up -d
32+
33+
Run for default django tables:
34+
35+
docker-compose run --rm --no-deps web python manage.py migrate
36+
docker-compose run --rm --no-deps web python manage.py makemigrations polls
37+
docker-compose run --rm --no-deps web python manage.py migrate polls
38+
39+
Add some polls from admin page
40+
41+
docker-compose run --rm --no-deps web python manage.py createsuperuser
42+
43+
The default login and password for the admin site is admin:admin.

django-mysql/Dockerfile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM python:3.8
2+
ENV PYTHONUNBUFFERED 1
3+
WORKDIR /app
4+
COPY ./src /app
5+
RUN pip install -r requirements.txt

django-mysql/docker-compose.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: '3.7'
2+
services:
3+
mysql:
4+
image: mysql:8.0
5+
command: --default-authentication-plugin=mysql_native_password
6+
environment:
7+
MYSQL_ROOT_PASSWORD: pass
8+
MYSQL_USER: django
9+
MYSQL_PASSWORD: django
10+
MYSQL_DATABASE: djangodb
11+
adminer:
12+
image: adminer:latest
13+
environment:
14+
ADMINER_DEFAULT_SERVER: mysql
15+
ADMINER_DEFAULT_DB_NAME: djangodb
16+
ports:
17+
- 8080:8080
18+
web:
19+
build: .
20+
command: python manage.py runserver 0.0.0.0:8000
21+
volumes:
22+
- ./src:/app
23+
ports:
24+
- "8000:8000"
25+
depends_on:
26+
- mysql

django-mysql/src/manage.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == '__main__':
22+
main()

django-mysql/src/mysite/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

django-mysql/src/mysite/settings.py

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
Django settings for mysite project.
3+
4+
Generated by 'django-admin startproject' using Django 1.11.2.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/1.11/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/1.11/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = 'noiujayeuck+=apj3e)q(d7f-!)tvvuysa80+#v))-*hmm&%1m'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'polls.apps.PollsConfig',
35+
'django.contrib.admin',
36+
'django.contrib.auth',
37+
'django.contrib.contenttypes',
38+
'django.contrib.sessions',
39+
'django.contrib.messages',
40+
'django.contrib.staticfiles',
41+
]
42+
43+
MIDDLEWARE = [
44+
'django.middleware.security.SecurityMiddleware',
45+
'django.contrib.sessions.middleware.SessionMiddleware',
46+
'django.middleware.common.CommonMiddleware',
47+
'django.middleware.csrf.CsrfViewMiddleware',
48+
'django.contrib.auth.middleware.AuthenticationMiddleware',
49+
'django.contrib.messages.middleware.MessageMiddleware',
50+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
51+
]
52+
53+
ROOT_URLCONF = 'mysite.urls'
54+
55+
TEMPLATES = [
56+
{
57+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
58+
'DIRS': [os.path.join(BASE_DIR, 'templates')],
59+
'APP_DIRS': True,
60+
'OPTIONS': {
61+
'context_processors': [
62+
'django.template.context_processors.debug',
63+
'django.template.context_processors.request',
64+
'django.contrib.auth.context_processors.auth',
65+
'django.contrib.messages.context_processors.messages',
66+
],
67+
},
68+
},
69+
]
70+
71+
WSGI_APPLICATION = 'mysite.wsgi.application'
72+
73+
74+
# Database
75+
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
76+
77+
DATABASES = {
78+
'default': {
79+
'ENGINE': 'django.db.backends.mysql',
80+
'NAME': 'djangodb',
81+
'USER': 'django',
82+
'PASSWORD': 'django',
83+
'HOST': 'mysql',
84+
'PORT': 3306,
85+
}
86+
}
87+
88+
89+
# Internationalization
90+
# https://docs.djangoproject.com/en/1.11/topics/i18n/
91+
92+
LANGUAGE_CODE = 'en-us'
93+
94+
TIME_ZONE = 'UTC'
95+
96+
USE_I18N = True
97+
98+
USE_L10N = True
99+
100+
USE_TZ = True
101+
102+
103+
# Static files (CSS, JavaScript, Images)
104+
# https://docs.djangoproject.com/en/1.11/howto/static-files/
105+
106+
STATIC_URL = '/static/'

django-mysql/src/mysite/urls.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.conf.urls import include, url
2+
from django.contrib import admin
3+
4+
urlpatterns = [
5+
url(r'^polls/', include('polls.urls')),
6+
url(r'^admin/', admin.site.urls),
7+
]

django-mysql/src/mysite/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for mysite project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
15+
16+
application = get_wsgi_application()

django-mysql/src/polls/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

django-mysql/src/polls/admin.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from django.contrib import admin
2+
3+
from .models import Choice, Question
4+
5+
6+
class ChoiceInline(admin.TabularInline):
7+
model = Choice
8+
extra = 3
9+
10+
11+
class QuestionAdmin(admin.ModelAdmin):
12+
fieldsets = [
13+
(None, {'fields': ['question_text']}),
14+
('Date information', {'fields': ['pub_date'],
15+
'classes': ['collapse']}),
16+
]
17+
inlines = [ChoiceInline]
18+
list_display = ('question_text', 'pub_date', 'was_published_recently')
19+
list_filter = ['pub_date']
20+
search_fields = ['question_text']
21+
22+
admin.site.register(Question, QuestionAdmin)

django-mysql/src/polls/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class PollsConfig(AppConfig):
5+
name = 'polls'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Generated by Django 3.2 on 2021-07-16 06:43
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
initial = True
10+
11+
dependencies = [
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='Question',
17+
fields=[
18+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
19+
('question_text', models.CharField(max_length=200)),
20+
('pub_date', models.DateTimeField(verbose_name='date published')),
21+
],
22+
),
23+
migrations.CreateModel(
24+
name='Choice',
25+
fields=[
26+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
27+
('choice_text', models.CharField(max_length=200)),
28+
('votes', models.IntegerField(default=0)),
29+
('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.question')),
30+
],
31+
),
32+
]

django-mysql/src/polls/migrations/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.

django-mysql/src/polls/models.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import datetime
2+
3+
from django.utils import timezone
4+
from django.db import models
5+
6+
7+
class Question(models.Model):
8+
question_text = models.CharField(max_length=200)
9+
pub_date = models.DateTimeField('date published')
10+
11+
def __str__(self):
12+
return self.question_text
13+
14+
def was_published_recently(self):
15+
now = timezone.now()
16+
return now - datetime.timedelta(days=1) <= self.pub_date <= now
17+
18+
was_published_recently.admin_order_field = 'pub_date'
19+
was_published_recently.boolean = True
20+
was_published_recently.short_description = 'Published recently?'
21+
22+
23+
class Choice(models.Model):
24+
question = models.ForeignKey(Question, on_delete=models.CASCADE)
25+
choice_text = models.CharField(max_length=200)
26+
votes = models.IntegerField(default=0)
27+
28+
def __str__(self):
29+
return self.choice_text
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
body {
2+
background: white url("images/background.gif") no-repeat;
3+
}
4+
5+
li a {
6+
color: green;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends "admin/base.html" %}
2+
3+
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
4+
5+
{% block branding %}
6+
<h1 id="site-name"><a href="{% url 'admin:index' %}">Polls Administration</a></h1>
7+
{% endblock %}
8+
9+
{% block nav-global %}{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<h1>{{ question.question_text }}</h1>
2+
3+
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
4+
5+
<form action="{% url 'polls:vote' question.id %}" method="post">
6+
{% csrf_token %}
7+
{% for choice in question.choice_set.all %}
8+
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
9+
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
10+
{% endfor %}
11+
<input type="submit" value="Vote" />
12+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% load static %}
2+
3+
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />
4+
5+
{% if latest_question_list %}
6+
<ul>
7+
{% for question in latest_question_list %}
8+
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
9+
{% endfor %}
10+
</ul>
11+
{% else %}
12+
<p>No polls are available.</p>
13+
{% endif %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1>{{ question.question_text }}</h1>
2+
3+
<ul>
4+
{% for choice in question.choice_set.all %}
5+
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
6+
{% endfor %}
7+
</ul>
8+
9+
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>

0 commit comments

Comments
 (0)