92 lines
3.0 KiB
Python
92 lines
3.0 KiB
Python
from django.contrib import admin
|
|
from django.utils.html import format_html
|
|
from import_export.admin import ImportExportModelAdmin
|
|
from .models import StreamingSiteTestingData,StreamingRedirectData
|
|
from .utils import full_testing
|
|
from urllib.parse import urlsplit, urlunsplit
|
|
|
|
|
|
|
|
@admin.action(description="check the URLs")
|
|
def check(modeladmin, request, queryset):
|
|
for query in queryset:
|
|
website_url = query.url
|
|
status=full_testing(website_url)[0]
|
|
result= full_testing(website_url)[1]
|
|
print(str(query.url)+": "+str(status)+" -> "+str(result))
|
|
query.status = status
|
|
query.result = result
|
|
query.save(update_fields=["status","result"])
|
|
|
|
|
|
# modeladmin.message_user(
|
|
# request,
|
|
# ngettext(
|
|
# "%d episode was successfully marked as seen.",
|
|
# "%d episodes were successfully marked as seen.",
|
|
# updated,
|
|
# )
|
|
# % updated,
|
|
# messages.SUCCESS,
|
|
# )
|
|
|
|
@admin.action(description="cleaning the url")
|
|
def urlcleaning(modeladmin, request, queryset):
|
|
for query in queryset:
|
|
# Split the URL into components
|
|
parsed = urlsplit(query.url)
|
|
|
|
# Rebuild using only scheme and netloc, adding a trailing slash at the end
|
|
base_url = urlunsplit((parsed.scheme, parsed.netloc, "/", "", ""))
|
|
query.url = base_url
|
|
query.save(update_fields=["url"])
|
|
print("Cleaning :"+str(base_url))
|
|
|
|
return 0
|
|
@admin.register(StreamingSiteTestingData)
|
|
class streamingAdmin(ImportExportModelAdmin):
|
|
list_display = ['clickable_links','result_colored','newstatus']
|
|
fields = ['url']
|
|
actions = [check,urlcleaning]
|
|
|
|
def clickable_links(self, obj):
|
|
return format_html('<a href="{}">{}</a>', obj.url,obj.url)
|
|
|
|
def result_colored(self, obj):
|
|
colors = {
|
|
'REACHABLE': 'green',
|
|
'VPN REQUIRED': 'yellow',
|
|
'UNREACHABLE': 'red',
|
|
}
|
|
|
|
return format_html(
|
|
'<b style="background:{};">{}</b>',
|
|
colors[obj.result],
|
|
obj.result,
|
|
)
|
|
def newstatus(self, obj):
|
|
if obj.status != '200':
|
|
return format_html('<div style="width:100%%; height:100%%; background-color:orange;">{}</div>', obj.status)
|
|
#return obj.status
|
|
|
|
|
|
@admin.register(StreamingRedirectData)
|
|
class StreamingRedirectAdmin(ImportExportModelAdmin):
|
|
list_display = ['name','season','episode','randomViewing','url_example','addedDate','alias']
|
|
#fields = ['name','season','episode','url_example','prefix','infix','suffix','alias','randomViewing','seasonInformation']
|
|
fieldsets = (
|
|
(
|
|
'Serie', {
|
|
'fields': ('name', 'season', 'episode','randomViewing')
|
|
}),
|
|
(
|
|
'url', {
|
|
|
|
'fields': (('prefix','infix','suffix'),'url_example')
|
|
}),
|
|
(
|
|
'Information', {
|
|
"classes": ["collapse"],
|
|
'fields': ('seasonInformation','alias','betaserieShowId')
|
|
}),
|
|
) |