Format code
This commit is contained in:
parent
a42455c9dc
commit
ede06900e5
@ -5,7 +5,12 @@ from urllib.parse import urlparse
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.http import Http404, HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
|
||||
from django.http import (
|
||||
Http404,
|
||||
HttpResponse,
|
||||
HttpResponseBadRequest,
|
||||
HttpResponseForbidden,
|
||||
)
|
||||
from django.shortcuts import render, reverse
|
||||
from django.utils import timezone
|
||||
from django.utils.decorators import method_decorator
|
||||
@ -53,7 +58,10 @@ class ValidateServiceOriginsMixin:
|
||||
|
||||
if origins != "*":
|
||||
remote_origin = request.META.get("HTTP_ORIGIN")
|
||||
if remote_origin is None and request.META.get("HTTP_REFERER") is not None:
|
||||
if (
|
||||
remote_origin is None
|
||||
and request.META.get("HTTP_REFERER") is not None
|
||||
):
|
||||
parsed = urlparse(request.META.get("HTTP_REFERER"))
|
||||
remote_origin = f"{parsed.scheme}://{parsed.netloc}".lower()
|
||||
origins = [origin.strip().lower() for origin in origins.split(",")]
|
||||
|
@ -37,7 +37,7 @@ REFERRERS = [
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
""
|
||||
"",
|
||||
]
|
||||
|
||||
USER_AGENTS = [
|
||||
@ -45,7 +45,7 @@ USER_AGENTS = [
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/43.4",
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 11_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko)",
|
||||
"Version/10.0 Mobile/14E304 Safari/602.1"
|
||||
"Version/10.0 Mobile/14E304 Safari/602.1",
|
||||
]
|
||||
|
||||
|
||||
@ -70,7 +70,9 @@ class Command(BaseCommand):
|
||||
owner = User.objects.get(email=options.get("owner_email"))
|
||||
service = Service.objects.create(name=options.get("name"), owner=owner)
|
||||
|
||||
print(f"Created demo service `{service.name}` (uuid: `{service.uuid}`, owner: {owner})")
|
||||
print(
|
||||
f"Created demo service `{service.name}` (uuid: `{service.uuid}`, owner: {owner})"
|
||||
)
|
||||
|
||||
# Go through each day requested, creating sessions and hits
|
||||
for days in range(options.get("days")):
|
||||
@ -78,12 +80,13 @@ class Command(BaseCommand):
|
||||
print(f"Populating info for {day}...")
|
||||
avg = options.get("avg")
|
||||
deviation = options.get("deviation")
|
||||
ips = [".".join(map(str, (random.randint(0, 255) for _ in range(4)))) for _ in range(avg)]
|
||||
ips = [
|
||||
".".join(map(str, (random.randint(0, 255) for _ in range(4))))
|
||||
for _ in range(avg)
|
||||
]
|
||||
|
||||
n = avg + random.randrange(-1 * deviation * avg, deviation * avg)
|
||||
for _ in range(
|
||||
n
|
||||
):
|
||||
for _ in range(n):
|
||||
time = day + timedelta(
|
||||
hours=random.randrange(0, 23),
|
||||
minutes=random.randrange(0, 59),
|
||||
@ -92,14 +95,20 @@ class Command(BaseCommand):
|
||||
ip = random.choice(ips)
|
||||
load_time = random.normalvariate(options.get("load_time"), 500)
|
||||
referrer = random.choice(REFERRERS)
|
||||
location = "https://example.com" + random.choice(LOCATIONS).replace("{rand}", str(random.randint(0, n)))
|
||||
location = "https://example.com" + random.choice(LOCATIONS).replace(
|
||||
"{rand}", str(random.randint(0, n))
|
||||
)
|
||||
user_agent = random.choice(USER_AGENTS)
|
||||
ingress_request(service.uuid, "JS", time, {"loadTime": load_time, "referrer": referrer}, ip, location, user_agent)
|
||||
ingress_request(
|
||||
service.uuid,
|
||||
"JS",
|
||||
time,
|
||||
{"loadTime": load_time, "referrer": referrer},
|
||||
ip,
|
||||
location,
|
||||
user_agent,
|
||||
)
|
||||
|
||||
print(f"Created {n} demo hits on {day}!")
|
||||
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
f"Successfully created demo data!"
|
||||
)
|
||||
)
|
||||
self.stdout.write(self.style.SUCCESS(f"Successfully created demo data!"))
|
||||
|
@ -6,18 +6,20 @@ from django.db import migrations, models
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0007_service_ignore_robots'),
|
||||
("core", "0007_service_ignore_robots"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='service',
|
||||
name='script_inject',
|
||||
field=models.TextField(blank=True, default=''),
|
||||
model_name="service",
|
||||
name="script_inject",
|
||||
field=models.TextField(blank=True, default=""),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='user',
|
||||
name='first_name',
|
||||
field=models.CharField(blank=True, max_length=150, verbose_name='first name'),
|
||||
model_name="user",
|
||||
name="first_name",
|
||||
field=models.CharField(
|
||||
blank=True, max_length=150, verbose_name="first name"
|
||||
),
|
||||
),
|
||||
]
|
||||
|
@ -18,7 +18,7 @@ class ServiceForm(forms.ModelForm):
|
||||
"hide_referrer_regex",
|
||||
"origins",
|
||||
"collaborators",
|
||||
"script_inject"
|
||||
"script_inject",
|
||||
]
|
||||
widgets = {
|
||||
"name": forms.TextInput(),
|
||||
@ -28,7 +28,7 @@ class ServiceForm(forms.ModelForm):
|
||||
"collect_ips": forms.RadioSelect(choices=[(True, "Yes"), (False, "No")]),
|
||||
"ignore_robots": forms.RadioSelect(choices=[(True, "Yes"), (False, "No")]),
|
||||
"hide_referrer_regex": forms.TextInput(),
|
||||
"script_inject": forms.Textarea(attrs={'class':'font-mono', 'rows': 5})
|
||||
"script_inject": forms.Textarea(attrs={"class": "font-mono", "rows": 5}),
|
||||
}
|
||||
labels = {
|
||||
"origins": "Allowed origins",
|
||||
@ -60,7 +60,9 @@ class ServiceForm(forms.ModelForm):
|
||||
|
||||
def clean_collaborators(self):
|
||||
collaborators = []
|
||||
users_to_emails = {} # maps users to the email they are listed under as a collaborator
|
||||
users_to_emails = (
|
||||
{}
|
||||
) # maps users to the email they are listed under as a collaborator
|
||||
for collaborator_email in self.cleaned_data["collaborators"].split(","):
|
||||
email = collaborator_email.strip()
|
||||
if email == "":
|
||||
@ -72,7 +74,9 @@ class ServiceForm(forms.ModelForm):
|
||||
raise forms.ValidationError(f"Email '{email}' is not registered")
|
||||
user = collaborator_email_linked.user
|
||||
if user in collaborators:
|
||||
raise forms.ValidationError(f"The emails '{email}' and '{users_to_emails[user]}' both correspond to the same user")
|
||||
raise forms.ValidationError(
|
||||
f"The emails '{email}' and '{users_to_emails[user]}' both correspond to the same user"
|
||||
)
|
||||
users_to_emails[user] = email
|
||||
collaborators.append(collaborator_email_linked.user)
|
||||
return collaborators
|
||||
|
@ -146,7 +146,7 @@ def iconify(text):
|
||||
"chrome mobile webview": "chrome.com",
|
||||
"firefox mobile": "firefox.com",
|
||||
"edge mobile": "microsoft.com",
|
||||
"chromium": "chromium.org"
|
||||
"chromium": "chromium.org",
|
||||
}
|
||||
|
||||
domain = None
|
||||
|
Loading…
Reference in New Issue
Block a user