Skip to content

Commit

Permalink
Merge pull request #1 from maexled/mysql-support
Browse files Browse the repository at this point in the history
added mysql support
  • Loading branch information
maexled authored Jun 11, 2022
2 parents 16e34b3 + 12dad19 commit 27a564a
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 203 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Django Tests

on:
push:
branches: [ "master" ]
branches: ["*"]
pull_request:
branches: [ "master" ]
workflow_call:
Expand Down
10 changes: 7 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
FROM python:3.9-slim-buster
FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt ./


RUN apt-get update && \
apt-get install --no-install-recommends -y gcc default-libmysqlclient-dev


RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir gunicorn && \
pip install --no-cache-dir -r requirements.txt

COPY . .
RUN python manage.py makemigrations && python manage.py migrate
CMD ["gunicorn", "--bind=0.0.0.0:8000", "pim.wsgi:application"]
CMD python manage.py makemigrations && python manage.py migrate && gunicorn --bind=0.0.0.0:8000 pim.wsgi:application
EXPOSE 8000/tcp
22 changes: 0 additions & 22 deletions mystrom/migrations/0001_initial.py

This file was deleted.

This file was deleted.

This file was deleted.

11 changes: 6 additions & 5 deletions mystrom_rest/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by Django 4.0.5 on 2022-06-07 15:23
# Generated by Django 4.0.5 on 2022-06-11 14:47

import django.core.validators
from django.db import migrations, models
import django.db.models.deletion

Expand All @@ -15,9 +16,9 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='MystromDevice',
fields=[
('id', models.IntegerField(primary_key=True, serialize=False)),
('id', models.AutoField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=16)),
('ip', models.CharField(max_length=16)),
('ip', models.CharField(max_length=16, validators=[django.core.validators.RegexValidator(message='Not valid IP Address', regex='^(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$')])),
],
options={
'db_table': 'devices',
Expand All @@ -26,13 +27,13 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='MystromResult',
fields=[
('id', models.IntegerField(primary_key=True, serialize=False)),
('id', models.AutoField(primary_key=True, serialize=False)),
('power', models.FloatField()),
('ws', models.FloatField()),
('relay', models.IntegerField()),
('temperature', models.FloatField()),
('date', models.DateTimeField(auto_now_add=True)),
('device_id', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='mystrom_rest.mystromdevice')),
('device', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='mystrom_rest.mystromdevice')),
],
options={
'db_table': 'results',
Expand Down
19 changes: 0 additions & 19 deletions mystrom_rest/migrations/0002_alter_mystromdevice_ip.py

This file was deleted.

19 changes: 0 additions & 19 deletions mystrom_rest/migrations/0003_alter_mystromdevice_ip.py

This file was deleted.

19 changes: 0 additions & 19 deletions mystrom_rest/migrations/0004_alter_mystromdevice_ip.py

This file was deleted.

19 changes: 0 additions & 19 deletions mystrom_rest/migrations/0005_alter_mystromdevice_ip.py

This file was deleted.

19 changes: 0 additions & 19 deletions mystrom_rest/migrations/0006_alter_mystromdevice_ip.py

This file was deleted.

6 changes: 3 additions & 3 deletions mystrom_rest/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class MystromDevice(models.Model):

id = models.IntegerField(primary_key=True)
id = models.AutoField(primary_key=True)

name = models.CharField(max_length=16)
ip = models.CharField(max_length=16, validators=[
Expand All @@ -22,9 +22,9 @@ class Meta:

class MystromResult(models.Model):

id = models.IntegerField(primary_key=True)
id = models.AutoField(primary_key=True)

device_id = models.ForeignKey(MystromDevice, on_delete=models.PROTECT)
device = models.ForeignKey(MystromDevice, on_delete=models.PROTECT)

power = models.FloatField()
ws = models.FloatField()
Expand Down
3 changes: 1 addition & 2 deletions mystrom_rest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ def device_results(request, id):
device = MystromDevice.objects.get(id=id)
except MystromDevice.DoesNotExist:
return JsonResponse({'message': 'The device does not exist'}, status=status.HTTP_404_NOT_FOUND)
results = MystromResult.objects.filter(device_id=device.id)
results = MystromResult.objects.filter(device_id=device)

if request.method == 'GET':
result_serializer = MystromResultSerializer(results, many=True)
return JsonResponse(result_serializer.data, safe=False)

23 changes: 18 additions & 5 deletions pim/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -92,12 +93,24 @@
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
if os.getenv("DB_NAME"):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ["DB_NAME"],
'USER': os.environ["DB_USER"],
'PASSWORD': os.environ["DB_PASSWORD"],
'HOST': os.environ["DB_HOST"],
'PORT': os.environ["DB_PORT"],
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
}


# Password validation
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
django==4.0.5
django-widget-tweaks==1.4.12
djangorestframework==3.13.1
django-cors-headers==3.13.0
django-cors-headers==3.13.0
mysqlclient==2.1.0

0 comments on commit 27a564a

Please sign in to comment.