Skip to content

Commit

Permalink
web - implemented db config-export to help troubleshooting
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Apr 2, 2021
1 parent 915ec2e commit dea41b8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 17 deletions.
1 change: 1 addition & 0 deletions code/web/base/ga/config/nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'Service': '/system/service/', # status and restart of service(s)
'Logs': '/system/log/', # read various log files
'Scripts': '/system/script/', # upload or delete scripts
'Export': '/system/export/', # download db dump
},
},
'right': {
Expand Down
2 changes: 1 addition & 1 deletion code/web/base/ga/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from .models import *
from .submodels.helper.matrix import Matrix
from .submodels.helper.crypto import decrypt, encrypt
from .submodels.helper.crypto import encrypt

LABEL_DICT = {
'name': 'Name',
Expand Down
4 changes: 0 additions & 4 deletions code/web/base/ga/submodels/helper/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@

def encrypt(unencrypted_secret: str) -> str:
return crypto.encrypt(unencrypted_secret)


def decrypt(encrypted_secret: str) -> str:
return crypto.decrypt(encrypted_secret)
47 changes: 47 additions & 0 deletions code/web/base/ga/subviews/system/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.shortcuts import HttpResponse
from django.conf import settings
from django.contrib.auth.decorators import user_passes_test

from datetime import datetime
from os import path as os_path

from ...user import authorized_to_write
from ...utils.helper import get_controller_setting, develop_subprocess
from ...subviews.handlers import Pseudo404


@user_passes_test(authorized_to_write, login_url='/denied/')
def export_view(request):
dump_file = f"dump_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.sql.xz"
dump_file_path = f'/tmp/{dump_file}'
sql_config_file = f'{settings.BASE_DIR}/database.cnf'
dump_db = get_controller_setting(request=request, setting='sql_database')

exclude_tables = [
'',
'auth_group',
'auth_group_permissions',
'auth_permission',
'auth_user',
'auth_user_groups',
'auth_user_user_permissions',
'django_admin_log',
'django_content_type',
'django_migrations',
'django_session',
'ga_inputdatamodel',
'ga_dashboardmodel',
'ga_test',
]
exclude_string = ' --ignore-table=ga.'.join(exclude_tables)
dump_command = f"mysqldump --defaults-file={sql_config_file} {dump_db} --single-transaction {exclude_string} | xz -7 > {dump_file_path}"

develop_subprocess(request, command=dump_command, develop='nope!')

if os_path.exists(dump_file_path):
with open(dump_file_path, 'rb') as dump:
response = HttpResponse(dump.read(), content_type='application/x-xz')
response['Content-Disposition'] = f'inline; filename={dump_file}'
return response

raise Pseudo404(ga={'request': request, 'msg': f'Could not dump database config!'})
2 changes: 0 additions & 2 deletions code/web/base/ga/subviews/system/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from datetime import datetime
from os import listdir as os_listdir

from core.utils.debug import Log

from ...user import authorized_to_read
from ...utils.helper import check_develop, get_controller_setting, str_to_list, develop_subprocess
from ..handlers import handler404
Expand Down
15 changes: 6 additions & 9 deletions code/web/base/ga/templates/home.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
{% extends "./body.html" %}
{% load util %}
{% load static %}
{% block content %}
<h1>
Growautomation
</h1>
<h3>
Management interface
</h3>
<br>
<hr>
<img src="{% static 'img/ga03.png' %}" alt="" onerror="this.style.display='none'" class="ga-img-float"/>
<br><br><hr>
<h3>Management interface</h3>
<p>
The goal is to automate activities related to the cultivation of plants. <br> <br>

And thus to minimize the associated repetitive tasks and optimize the process. <br> <br>

It's simple. And should also remain so if you have little time for it. <br> <br> <br>

<h3>Thoughts about growing</h3>
<br><hr>
<h3>Thoughts about growing</h3>

It should not be necessary to check if the plants need water. <br>
- Watering should be done automatically but only when necessary. <br><br>
Expand Down
6 changes: 5 additions & 1 deletion code/web/base/ga/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.shortcuts import render, redirect
from django.shortcuts import render
from django.contrib.auth.decorators import login_required, user_passes_test

from .config.site import type_dict
Expand All @@ -15,6 +15,7 @@
from .subviews.api.data.main import ApiData
from .subviews.api.chart.main import ApiChart
from .subviews.data.dashboard.main import DashboardView
from .subviews.system.export import export_view


def view(request, **kwargs):
Expand Down Expand Up @@ -106,6 +107,9 @@ def system(request, typ: str, sub_type: str = None):

return logout_check(request=request, default=ScriptView(request=request))

elif typ == 'export':
return export_view(request=request)

return logout_check(request=request, default=handler404(request=request, msg='Not yet implemented!'))

@staticmethod
Expand Down

0 comments on commit dea41b8

Please sign in to comment.