Skip to content

Commit d58c93e

Browse files
committed
Working on Django and Postgres-XL
0 parents  commit d58c93e

17 files changed

+479
-0
lines changed

ads/__init__.py

Whitespace-only changes.

ads/admin.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
4+
from ads.models import Company, Ads, Campaign
5+
6+
7+
@admin.register(Company)
8+
class CompanyAdmin(admin.ModelAdmin):
9+
pass
10+
11+
12+
@admin.register(Campaign)
13+
class CampaignAdmin(admin.ModelAdmin):
14+
pass
15+
16+
17+
@admin.register(Ads)
18+
class AdsAdmin(admin.ModelAdmin):
19+
pass
20+
21+
22+
23+

ads/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 AdsConfig(AppConfig):
5+
name = 'ads'

ads/migrations/0001_initial.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Generated by Django 2.1.4 on 2019-01-27 07:57
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
initial = True
11+
12+
dependencies = [
13+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name='Ads',
19+
fields=[
20+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
21+
('created_at', models.DateTimeField(auto_created=True)),
22+
('name', models.CharField(max_length=512)),
23+
('clicks_count', models.IntegerField()),
24+
('updated_at', models.DateTimeField(auto_now_add=True)),
25+
],
26+
),
27+
migrations.CreateModel(
28+
name='Campaign',
29+
fields=[
30+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
31+
('created_at', models.DateTimeField(auto_created=True)),
32+
('name', models.CharField(max_length=512)),
33+
('updated_at', models.DateTimeField(auto_now_add=True)),
34+
],
35+
),
36+
migrations.CreateModel(
37+
name='Click',
38+
fields=[
39+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
40+
('clicked_at', models.DateTimeField(auto_created=True)),
41+
('ads', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ads.Ads')),
42+
('campaign', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ads.Campaign')),
43+
],
44+
),
45+
migrations.CreateModel(
46+
name='Company',
47+
fields=[
48+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
49+
('created_at', models.DateTimeField(auto_created=True)),
50+
('name', models.CharField(max_length=512)),
51+
('updated_at', models.DateTimeField(auto_now_add=True)),
52+
('users', models.ManyToManyField(to=settings.AUTH_USER_MODEL)),
53+
],
54+
),
55+
migrations.AddField(
56+
model_name='click',
57+
name='company',
58+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ads.Company'),
59+
),
60+
migrations.AddField(
61+
model_name='campaign',
62+
name='company',
63+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ads.Company'),
64+
),
65+
migrations.AddField(
66+
model_name='ads',
67+
name='campaign',
68+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ads.Campaign'),
69+
),
70+
migrations.AddField(
71+
model_name='ads',
72+
name='company',
73+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ads.Company'),
74+
),
75+
migrations.AlterUniqueTogether(
76+
name='click',
77+
unique_together={('id', 'company')},
78+
),
79+
migrations.AlterUniqueTogether(
80+
name='campaign',
81+
unique_together={('id', 'company')},
82+
),
83+
migrations.AlterUniqueTogether(
84+
name='ads',
85+
unique_together={('id', 'company')},
86+
),
87+
]
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Generated by Django 2.1.4 on 2019-01-23 14:26
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('ads', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.RunSQL(
14+
"ALTER TABLE public.ads_company DISTRIBUTE BY HASH(id);"
15+
),
16+
]
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Generated by Django 2.1.4 on 2019-01-23 14:26
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
('ads', '0002_auto_20190123_1426'),
9+
]
10+
11+
operations = [
12+
13+
# Django considers "id" the primary key of these tables, but
14+
# Postgres-XL want the primary key to be (company_id, id)
15+
migrations.RunSQL("""
16+
ALTER TABLE ads_campaign
17+
DROP CONSTRAINT ads_campaign_pkey CASCADE;
18+
19+
ALTER TABLE ads_campaign
20+
ADD CONSTRAINT ads_campaign_pkey
21+
PRIMARY KEY (company_id, id)
22+
"""),
23+
24+
migrations.RunSQL("""
25+
ALTER TABLE ads_ads
26+
DROP CONSTRAINT ads_ads_pkey CASCADE;
27+
28+
ALTER TABLE ads_ads
29+
ADD CONSTRAINT ads_ads_pkey
30+
PRIMARY KEY (company_id, id)
31+
"""),
32+
33+
migrations.RunSQL(
34+
"ALTER TABLE public.ads_campaign DISTRIBUTE BY HASH(company_id);"
35+
),
36+
37+
migrations.RunSQL(
38+
"ALTER TABLE public.ads_ads DISTRIBUTE BY HASH(company_id);"
39+
),
40+
]

ads/migrations/__init__.py

Whitespace-only changes.

ads/models.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from django.contrib.auth.models import User
2+
from django.db import models
3+
4+
5+
# Create your models here.
6+
7+
8+
class Company(models.Model):
9+
"""
10+
Distributed on id
11+
"""
12+
name = models.CharField(max_length=512)
13+
users = models.ManyToManyField(User)
14+
created_at = models.DateTimeField(auto_created=True)
15+
updated_at = models.DateTimeField(auto_now_add=True)
16+
17+
def __str__(self):
18+
return self.name
19+
20+
21+
class Campaign(models.Model):
22+
"""
23+
Distributed on (id, company)
24+
"""
25+
26+
class Meta:
27+
unique_together = (('id', 'company'),)
28+
29+
name = models.CharField(max_length=512)
30+
company = models.ForeignKey(Company, on_delete=models.CASCADE)
31+
created_at = models.DateTimeField(auto_created=True)
32+
updated_at = models.DateTimeField(auto_now_add=True)
33+
34+
def __str__(self):
35+
return self.name
36+
37+
38+
class Ads(models.Model):
39+
"""
40+
Distributed on (id, company)
41+
"""
42+
43+
class Meta:
44+
unique_together = (('id', 'company'),)
45+
46+
name = models.CharField(max_length=512)
47+
company = models.ForeignKey(Company, on_delete=models.CASCADE)
48+
campaign = models.ForeignKey(Campaign, on_delete=models.CASCADE)
49+
clicks_count = models.IntegerField()
50+
created_at = models.DateTimeField(auto_created=True)
51+
updated_at = models.DateTimeField(auto_now_add=True)
52+
53+
def __str__(self):
54+
return self.name
55+
56+
57+
class Click(models.Model):
58+
class Meta:
59+
unique_together = (('id', 'company'),)
60+
61+
ads = models.ForeignKey(Ads, on_delete=models.CASCADE)
62+
company = models.ForeignKey(Company, on_delete=models.CASCADE)
63+
campaign = models.ForeignKey(Campaign, on_delete=models.CASCADE)
64+
clicked_at = models.DateTimeField(auto_created=True)

ads/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

ads/views.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.shortcuts import render
2+
3+
# Create your views here.

dj_psxl_exp/__init__.py

Whitespace-only changes.

dj_psxl_exp/settings.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
"""
2+
Django settings for dj_psxl_exp project.
3+
4+
Generated by 'django-admin startproject' using Django 2.1.4.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.1/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/2.1/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+
# Quick-start development settings - unsuitable for production
19+
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
20+
21+
# SECURITY WARNING: keep the secret key used in production secret!
22+
SECRET_KEY = 'qeprnfr&h63t9$gxqd+8vz&fu8p#tlq71o&!t$2y2g703_rc@r'
23+
24+
# SECURITY WARNING: don't run with debug turned on in production!
25+
DEBUG = True
26+
27+
ALLOWED_HOSTS = []
28+
29+
# Application definition
30+
31+
INSTALLED_APPS = [
32+
'django.contrib.admin',
33+
'django.contrib.auth',
34+
'django.contrib.contenttypes',
35+
'django.contrib.sessions',
36+
'django.contrib.messages',
37+
'django.contrib.staticfiles',
38+
'ads',
39+
]
40+
41+
MIDDLEWARE = [
42+
'django.middleware.security.SecurityMiddleware',
43+
'django.contrib.sessions.middleware.SessionMiddleware',
44+
'django.middleware.common.CommonMiddleware',
45+
'django.middleware.csrf.CsrfViewMiddleware',
46+
'django.contrib.auth.middleware.AuthenticationMiddleware',
47+
'django.contrib.messages.middleware.MessageMiddleware',
48+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
49+
]
50+
51+
ROOT_URLCONF = 'dj_psxl_exp.urls'
52+
53+
TEMPLATES = [
54+
{
55+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
56+
'DIRS': [],
57+
'APP_DIRS': True,
58+
'OPTIONS': {
59+
'context_processors': [
60+
'django.template.context_processors.debug',
61+
'django.template.context_processors.request',
62+
'django.contrib.auth.context_processors.auth',
63+
'django.contrib.messages.context_processors.messages',
64+
],
65+
},
66+
},
67+
]
68+
69+
WSGI_APPLICATION = 'dj_psxl_exp.wsgi.application'
70+
71+
# Database
72+
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
73+
74+
DATABASES = {
75+
'default': {
76+
'ENGINE': 'django.db.backends.postgresql_psycopg2',
77+
'HOST': os.environ.get('DATABASES_HOST', '192.168.0.142'),
78+
'PORT': os.environ.get('DATABASES_PORT', 30002),
79+
'NAME': os.environ.get('DATABASES_NAME', 'test'),
80+
'USER': os.environ.get('DATABASES_USER', 'postgres'),
81+
'PASSWORD': os.environ.get('DATABASES_PASSWORD', 'postgres'),
82+
'DISABLE_SERVER_SIDE_CURSORS': True,
83+
}
84+
}
85+
86+
# Password validation
87+
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
88+
89+
AUTH_PASSWORD_VALIDATORS = [
90+
{
91+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
92+
},
93+
{
94+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
95+
},
96+
{
97+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
98+
},
99+
{
100+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
101+
},
102+
]
103+
104+
# Internationalization
105+
# https://docs.djangoproject.com/en/2.1/topics/i18n/
106+
107+
LANGUAGE_CODE = 'en-us'
108+
109+
TIME_ZONE = 'UTC'
110+
111+
USE_I18N = True
112+
113+
USE_L10N = True
114+
115+
USE_TZ = True
116+
117+
# Static files (CSS, JavaScript, Images)
118+
# https://docs.djangoproject.com/en/2.1/howto/static-files/
119+
120+
STATIC_URL = '/static/'
121+
122+
123+
# @note: Patch to define default creation tables as replication
124+
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
125+
BaseDatabaseSchemaEditor.sql_create_table = "CREATE TABLE %(table)s (%(definition)s) DISTRIBUTE BY REPLICATION"

dj_psxl_exp/urls.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""dj_psxl_exp URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/2.1/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path
18+
19+
urlpatterns = [
20+
path('admin/', admin.site.urls),
21+
]

0 commit comments

Comments
 (0)