INSTALLED_APPS = (
'accounts.apps.AccountsConfig',
'emails.apps.EmailsConfig',
+ 'ftp',
'services.apps.ServicesConfig',
'ssh_keys.apps.SshKeysConfig',
--- /dev/null
+from django.contrib import admin
+from django.utils.safestring import mark_safe
+from django.utils.translation import gettext as _
+from . import models
+from .utils import generate_password
+
+
+@admin.register(models.FtpUser)
+class FtpUserAdmin(admin.ModelAdmin):
+ fields = ['login', 'created_at', 'last_seen_at', 'password', 'password_set_at']
+ readonly_fields = ['created_at', 'last_seen_at', 'password_set_at']
+
+ def save_model(self, request, obj, form, change):
+ if not obj.password:
+ pwd = obj.set_password(save=False)
+ self.message_user(
+ request,
+ mark_safe(
+ _('Password for <strong>%(login)s</strong> set to: <input disabled value="%(password)s">.') % {
+ "login": obj.login,
+ "password": pwd,
+ }
+ )
+ )
+ return super().save_model(request, obj, form, change)
+
+
+
--- /dev/null
+from django.apps import AppConfig
+
+
+class FtpConfig(AppConfig):
+ default_auto_field = 'django.db.models.BigAutoField'
+ name = 'ftp'
--- /dev/null
+# Generated by Django 3.2.6 on 2021-09-14 14:00
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ initial = True
+
+ dependencies = [
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='FtpUser',
+ fields=[
+ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('created_at', models.DateTimeField(auto_now_add=True)),
+ ('last_seen_at', models.DateTimeField(null=True)),
+ ('login', models.CharField(max_length=255, unique=True)),
+ ('passwd', models.CharField(blank=True, max_length=255)),
+ ],
+ ),
+ ]
--- /dev/null
+# Generated by Django 3.2.6 on 2021-09-17 21:16
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('ftp', '0001_initial'),
+ ]
+
+ operations = [
+ migrations.RenameField(
+ model_name='ftpuser',
+ old_name='passwd',
+ new_name='password',
+ ),
+ migrations.AddField(
+ model_name='ftpuser',
+ name='password_set_at',
+ field=models.DateTimeField(editable=False, null=True),
+ ),
+ migrations.AlterField(
+ model_name='ftpuser',
+ name='last_seen_at',
+ field=models.DateTimeField(editable=False, null=True),
+ ),
+ ]
--- /dev/null
+from crypt import crypt
+from django.db import models
+from django.contrib.messages import add_message
+from django.utils.timezone import now
+from .utils import generate_password
+
+
+class FtpUser(models.Model):
+ created_at = models.DateTimeField(auto_now_add=True)
+ last_seen_at = models.DateTimeField(null=True, editable=False)
+ login = models.CharField(max_length=255, unique=True)
+ password = models.CharField(max_length=255, blank=True)
+ password_set_at = models.DateTimeField(null=True, editable=False)
+
+ def __str__(self):
+ return self.login
+
+ def set_password(self, save=True):
+ pwd = generate_password()
+ self.password = crypt(pwd)
+ self.password_set_at = now()
+ if save:
+ self.save()
+ return pwd
--- /dev/null
+import secrets
+import string
+
+def generate_password(length=10):
+ chars = string.ascii_letters + string.digits + string.punctuation
+ return ''.join(secrets.choice(chars) for i in range(length))