1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2025-07-31 12:41:08 +00:00

lint with black python. convert f style strings to old one. some small fixes

This commit is contained in:
catborise 2020-11-05 12:34:31 +03:00
parent c20c353a40
commit 508e3609be
54 changed files with 2123 additions and 1824 deletions

View file

@ -8,7 +8,7 @@ def apply_change_password(sender, **kwargs):
Depending on settings SHOW_PROFILE_EDIT_PASSWORD
'''
from django.conf import settings
from django.contrib.auth.models import User, Permission
from django.contrib.auth.models import Permission, User
if hasattr(settings, 'SHOW_PROFILE_EDIT_PASSWORD'):
print('\033[1m! \033[92mSHOW_PROFILE_EDIT_PASSWORD is found inside settings.py\033[0m')
print('\033[1m* \033[92mApplying permission can_change_password for all users\033[0m')
@ -29,8 +29,8 @@ def create_admin(sender, **kwargs):
'''
Create initial admin user
'''
from django.contrib.auth.models import User
from accounts.models import UserAttributes
from django.contrib.auth.models import User
plan = kwargs.get('plan', [])
for migration, rolled_back in plan:

View file

@ -1,9 +1,8 @@
# Generated by Django 2.2.10 on 2020-01-28 07:01
from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):

View file

@ -1,7 +1,8 @@
from django import template
import base64
import hashlib
from django import template
register = template.Library()

View file

@ -35,27 +35,27 @@ def validate_ssh_key(key):
return False
# unpack the contents of data, from data[:4] , property of ssh key .
try:
str_len = struct.unpack('>I', data[:4])[0]
str_len = struct.unpack(">I", data[:4])[0]
except struct.error:
return False
# data[4:str_len] must have string which matches with the typeofkey, another ssh key property.
if data[4:4 + str_len] == typeofkey:
if data[4 : 4 + str_len] == typeofkey:
return True
else:
else:
return False
def send_email_with_otp(user, device):
send_mail(
_('OTP QR Code'),
_('Please view HTML version of this message.'),
_("OTP QR Code"),
_("Please view HTML version of this message."),
None,
[user.email],
html_message=render_to_string(
'accounts/email/otp.html',
"accounts/email/otp.html",
{
'totp_url': device.config_url,
'user': user,
"totp_url": device.config_url,
"user": user,
},
),
fail_silently=False,

View file

@ -23,40 +23,52 @@ def profile(request):
if profile_form.is_valid():
profile_form.save()
messages.success(request, _('Profile updated'))
return redirect('accounts:profile')
messages.success(request, _("Profile updated"))
return redirect("accounts:profile")
return render(request, "profile.html", {
'publickeys': publickeys,
'profile_form': profile_form,
'ssh_key_form': ssh_key_form,
})
return render(
request,
"profile.html",
{
"publickeys": publickeys,
"profile_form": profile_form,
"ssh_key_form": ssh_key_form,
},
)
def ssh_key_create(request):
key_form = UserSSHKeyForm(request.POST or None, user=request.user)
if key_form.is_valid():
key_form.save()
messages.success(request, _('SSH key added'))
return redirect('accounts:profile')
messages.success(request, _("SSH key added"))
return redirect("accounts:profile")
return render(request, 'common/form.html', {
'form': key_form,
'title': _('Add SSH key'),
})
return render(
request,
"common/form.html",
{
"form": key_form,
"title": _("Add SSH key"),
},
)
def ssh_key_delete(request, pk):
ssh_key = get_object_or_404(UserSSHKey, pk=pk, user=request.user)
if request.method == 'POST':
if request.method == "POST":
ssh_key.delete()
messages.success(request, _('SSH key deleted'))
return redirect('accounts:profile')
messages.success(request, _("SSH key deleted"))
return redirect("accounts:profile")
return render(request, 'common/confirm_delete.html', {
'object': ssh_key,
'title': _('Delete SSH key'),
})
return render(
request,
"common/confirm_delete.html",
{
"object": ssh_key,
"title": _("Delete SSH key"),
},
)
@superuser_only
@ -67,13 +79,16 @@ def account(request, user_id):
publickeys = UserSSHKey.objects.filter(user_id=user_id)
return render(
request, "account.html", {
'user': user,
'user_insts': user_insts,
'instances': instances,
'publickeys': publickeys,
'otp_enabled': settings.OTP_ENABLED,
})
request,
"account.html",
{
"user": user,
"user_insts": user_insts,
"instances": instances,
"publickeys": publickeys,
"otp_enabled": settings.OTP_ENABLED,
},
)
@permission_required("accounts.change_password", raise_exception=True)
@ -118,7 +133,7 @@ def user_instance_update(request, pk):
return render(
request,
'common/form.html',
"common/form.html",
{
"form": form,
"title": _("Update User Instance"),
@ -150,29 +165,33 @@ def email_otp(request):
if form.is_valid():
UserModel = get_user_model()
try:
user = UserModel.objects.get(email=form.cleaned_data['email'])
user = UserModel.objects.get(email=form.cleaned_data["email"])
except UserModel.DoesNotExist:
pass
else:
device = get_user_totp_device(user)
send_email_with_otp(user, device)
messages.success(request, _('OTP Sent to %s') % form.cleaned_data['email'])
return redirect('accounts:login')
messages.success(request, _("OTP Sent to %(email)s") % {"email": form.cleaned_data["email"]})
return redirect("accounts:login")
return render(request, 'accounts/email_otp_form.html', {
'form': form,
'title': _('Email OTP'),
})
return render(
request,
"accounts/email_otp_form.html",
{
"form": form,
"title": _("Email OTP"),
},
)
@superuser_only
def admin_email_otp(request, user_id):
user = get_object_or_404(get_user_model(), pk=user_id)
device = get_user_totp_device(user)
if user.email != '':
if user.email != "":
send_email_with_otp(user, device)
messages.success(request, _('OTP QR code was emailed to user %s') % user)
messages.success(request, _("OTP QR code was emailed to user %(user)s") % {"user": user})
else:
messages.error(request, _('User email not set, failed to send QR code'))
return redirect('accounts:account', user.id)
messages.error(request, _("User email not set, failed to send QR code"))
return redirect("accounts:account", user.id)