Add profile model

pull/1371/head
Piero Toffanin 2023-08-24 15:02:30 -04:00
rodzic 5ba0d472af
commit ba1965add0
8 zmienionych plików z 118 dodań i 0 usunięć

Wyświetl plik

@ -10,10 +10,13 @@ from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils.html import format_html
from guardian.admin import GuardedModelAdmin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from app.models import PluginDatum
from app.models import Preset
from app.models import Plugin
from app.models import Profile
from app.plugins import get_plugin_by_name, enable_plugin, disable_plugin, delete_plugin, valid_plugin, \
get_plugins_persistent_path, clear_plugins_cache, init_plugins
from .models import Project, Task, Setting, Theme
@ -260,3 +263,14 @@ class PluginAdmin(admin.ModelAdmin):
admin.site.register(Plugin, PluginAdmin)
class ProfileInline(admin.StackedInline):
model = Profile
can_delete = False
class UserAdmin(BaseUserAdmin):
inlines = [ProfileInline]
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

Wyświetl plik

@ -0,0 +1,35 @@
# Generated by Django 2.2.27 on 2023-08-24 16:35
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
def create_profiles(apps, schema_editor):
User = apps.get_model('auth', 'User')
Profile = apps.get_model('app', 'Profile')
for u in User.objects.all():
p = Profile.objects.create(user=u)
p.save()
print("Created user profile for %s" % u.username)
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('app', '0036_task_size'),
]
operations = [
migrations.CreateModel(
name='Profile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('quota', models.FloatField(blank=True, default=-1, help_text='Maximum disk quota in megabytes', verbose_name='Quota')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.RunPython(create_profiles),
]

Wyświetl plik

@ -5,6 +5,7 @@ from .theme import Theme
from .setting import Setting
from .plugin_datum import PluginDatum
from .plugin import Plugin
from .profile import Profile
# deprecated
def image_directory_path(image_upload, filename):

Wyświetl plik

@ -0,0 +1,37 @@
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.db.models.signals import post_save
from django.dispatch import receiver
from app.models import Task
from django.db.models import Sum
from django.core.cache import cache
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
quota = models.FloatField(default=-1, blank=True, help_text=_("Maximum disk quota in megabytes"), verbose_name=_("Quota"))
def has_quota(self):
return self.quota != -1
def used_quota(self):
return Task.objects.filter(project__owner=self.user).aggregate(total=Sum('size'))['total']
def used_quota_cached(self):
k = f'used_quota_{self.user.id}'
cached = cache.get(k)
if cached is not None:
return cached
v = self.used_quota()
cache.set(k, v, 300) # 2 minutes
return v
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()

Wyświetl plik

@ -15,6 +15,17 @@
{% blocktrans with user=user.username %}Hello, {{ user }}!{% endblocktrans %}<br/>
<span class="email">{{ user.email }}</span>
</li>
{% if user.profile.has_quota %}
<li class="divider"></li>
{% with tot_quota=user.profile.quota %}
{% with used_quota=user.profile.used_quota_cached %}
{% percentage 0 tot_quota as perc_quota %}
Tot: {{ tot_quota }} Used: {{ used_quota }}
Perc: {{ perc_quota|floatformat:0 }}
{% endwith %}{% endwith %}
{% endif %}
<li class="divider"></li>
<li><a href="/logout/"><i class="fa fa-sign-out-alt fa-fw"></i> {% trans 'Logout' %}</a>
</li>

Wyświetl plik

@ -7,6 +7,15 @@ from webodm import settings
register = template.Library()
logger = logging.getLogger('app.logger')
@register.simple_tag
def percentage(num, den, maximum=None):
if den == 0:
return 0
perc = max(0, num / den * 100)
if maximum is not None:
perc = min(perc, maximum)
return perc
@register.simple_tag
def is_single_user_mode():
return settings.SINGLE_USER_MODE

Wyświetl plik

@ -14,6 +14,7 @@ django-filter==2.4.0
django-guardian==1.4.9
django-imagekit==4.0.1
django-libsass==0.7
django-redis==4.12.1
django-webpack-loader==0.6.0
djangorestframework==3.13.1
djangorestframework-jwt==1.9.0

Wyświetl plik

@ -377,6 +377,16 @@ CELERY_INCLUDE=['worker.tasks', 'app.plugins.worker']
CELERY_WORKER_REDIRECT_STDOUTS = False
CELERY_WORKER_HIJACK_ROOT_LOGGER = False
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": os.environ.get('WO_BROKER', 'redis://localhost'),
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
# Number of minutes a processing node hasn't been seen
# before it should be considered offline
NODE_OFFLINE_MINUTES = 5