Sunil Mohan Adapa 74214c18ae
*: Use Django gettext functions instead of ugettext
- ugettext functions will be removed in Django 4.0. Each use emits a warning
when running with Django 3.2. Since we have warnings enabled in developer mode,
we see quite a few messages because of this.

- ugettext is already a simple alias of gettext. So, no regressions are
expected.

Tests:

- Accessing an affected app in UI with Django 3.2 and Django 2.2 works fine.

- Using Django 3.2 there are no warnings related to removal of ugettext
functions.

- Ran regular unit tests.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2021-09-20 16:50:16 -04:00

72 lines
1.9 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
import logging
import os
import pwd
import subprocess
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from plinth.actions import superuser_run
from plinth.errors import ActionError
from plinth.modules.email_server import interproc
logger = logging.getLogger(__name__)
def exists_nam(username):
"""Returns True if the user's home directory exists"""
try:
passwd = pwd.getpwnam(username)
except KeyError as e:
raise ValidationError(_('User does not exist')) from e
return _exists(passwd)
def exists_uid(uid_number):
"""Returns True if the user's home directory exists"""
try:
passwd = pwd.getpwuid(uid_number)
except KeyError as e:
raise ValidationError(_('User does not exist')) from e
return _exists(passwd)
def _exists(passwd):
return os.path.exists(passwd.pw_dir)
def put_nam(username):
"""Create a home directory for the user (identified by username)"""
_put('nam', username)
def put_uid(uid_number):
"""Create a home directory for the user (identified by UID)"""
_put('uid', str(uid_number))
def _put(arg_type, user_info):
try:
args = ['-i', 'home', 'mk', arg_type, user_info]
superuser_run('email_server', args)
except ActionError as e:
raise RuntimeError('Action script failure') from e
def action_mk(arg_type, user_info):
if arg_type == 'nam':
passwd = pwd.getpwnam(user_info)
elif arg_type == 'uid':
passwd = pwd.getpwuid(int(user_info))
else:
raise ValueError('Unknown arg_type')
args = ['sudo', '-n', '--user=#' + str(passwd.pw_uid)]
args.extend(['/bin/sh', '-c', 'mkdir -p ~'])
completed = subprocess.run(args, capture_output=True)
if completed.returncode != 0:
interproc.log_subprocess(completed)
raise OSError('Could not create home directory')