Add Project Files
This commit is contained in:
0
hosts/__init__.py
Normal file
0
hosts/__init__.py
Normal file
27
hosts/admin.py
Normal file
27
hosts/admin.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import requests
|
||||
from django.contrib import admin
|
||||
from .models import Host
|
||||
|
||||
|
||||
def toggle_check_hosts(queryset):
|
||||
for item in queryset:
|
||||
try:
|
||||
r = requests.get(item.host_name)
|
||||
if r.status_code == 200:
|
||||
item.status = True
|
||||
else:
|
||||
item.status = False
|
||||
except requests.RequestException:
|
||||
item.status = False
|
||||
item.save()
|
||||
|
||||
|
||||
toggle_check_hosts.short_description = "Проверить"
|
||||
|
||||
|
||||
@admin.register(Host)
|
||||
class HostAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"host_name", "port", "status", "status_code", "created", "updated"
|
||||
)
|
||||
actions = [toggle_check_hosts]
|
||||
5
hosts/apps.py
Normal file
5
hosts/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class HostsConfig(AppConfig):
|
||||
name = "hosts"
|
||||
22
hosts/management/commands/check_hosts.py
Normal file
22
hosts/management/commands/check_hosts.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import requests
|
||||
from django.core.management import BaseCommand
|
||||
from ...models import Host
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""
|
||||
Check Hosts Django Management Command
|
||||
"""
|
||||
def handle(self, *args, **kwargs):
|
||||
hosts = Host.objects.all()
|
||||
for host in hosts:
|
||||
try:
|
||||
r = requests.get(host.host_name)
|
||||
if r.status_code == 200:
|
||||
host.status = True
|
||||
else:
|
||||
host.status = False
|
||||
|
||||
except requests.exceptions.RequestException:
|
||||
host.status = False
|
||||
host.save()
|
||||
0
hosts/migrations/__init__.py
Normal file
0
hosts/migrations/__init__.py
Normal file
21
hosts/models.py
Normal file
21
hosts/models.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Host(models.Model):
|
||||
"""
|
||||
Host model
|
||||
"""
|
||||
host_name = models.URLField("Host", max_length=100)
|
||||
port = models.PositiveSmallIntegerField("Port", default=80)
|
||||
status = models.BooleanField("Status", default=True)
|
||||
status_code = models.PositiveSmallIntegerField("Status Code", default=200)
|
||||
created = models.DateTimeField("Created", auto_now_add=True)
|
||||
updated = models.DateTimeField("Updated", auto_now=True)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Host"
|
||||
verbose_name_plural = "Hosts"
|
||||
ordering = ["-created"]
|
||||
|
||||
def __str__(self):
|
||||
return self.host_name
|
||||
3
hosts/tests.py
Normal file
3
hosts/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
8
hosts/urls.py
Normal file
8
hosts/urls.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('hosts/', views.Index.as_view(), name='index'),
|
||||
path('hosts/<int:pk>/', views.HostDetail.as_view(), name='host-info'),
|
||||
path('add/', views.HostCreate.as_view(), name='add'),
|
||||
]
|
||||
0
hosts/utils.py
Normal file
0
hosts/utils.py
Normal file
23
hosts/views.py
Normal file
23
hosts/views.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic import ListView, DetailView
|
||||
from django.views.generic.edit import CreateView
|
||||
|
||||
from .models import Host
|
||||
|
||||
|
||||
class Index(ListView):
|
||||
model = Host
|
||||
template_name = 'index.html'
|
||||
paginate_by = 10
|
||||
|
||||
|
||||
class HostCreate(CreateView):
|
||||
model = Host
|
||||
fields = ['host_name', 'port']
|
||||
template_name = 'add_host.html'
|
||||
success_url = reverse_lazy('index')
|
||||
|
||||
|
||||
class HostDetail(DetailView):
|
||||
model = Host
|
||||
template_name = 'host_info.html'
|
||||
Reference in New Issue
Block a user