add predefined_token

as an alternative to the randomly generated, which is also possible
when no predefined_token is defined

also update .env.example vars
This commit is contained in:
pedro 2024-10-16 21:54:54 +02:00 committed by Cayo Puigdefabregas
parent f757c58356
commit 67869bc6f5
4 changed files with 31 additions and 4 deletions

View File

@ -1,2 +1,21 @@
DOMAIN=localhost
DEMO=false
STATIC_ROOT=/tmp/static/
MEDIA_ROOT=/tmp/media/
ALLOWED_HOSTS=localhost,localhost:8000,127.0.0.1,
DOMAIN=localhost
DEBUG=True
EMAIL_HOST="mail.example.org"
EMAIL_HOST_USER="fillme_noreply"
EMAIL_HOST_PASSWORD="fillme_passwd"
EMAIL_PORT=587
EMAIL_USE_TLS=True
EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend"
EMAIL_FILE_PATH="/tmp/app-messages"
ENABLE_EMAIL=false
COMMIT=$(git log --format="%H %ad" --date=iso -n 1)
PREDEFINED_TOKEN='5018dd65-9abd-4a62-8896-80f34ac66150'
# TODO review these vars
#SNAPSHOTS_DIR=/path/to/TODO
#EVIDENCES_DIR=/path/to/TODO

View File

@ -7,6 +7,7 @@ services:
- DEBUG=true
- DOMAIN=${DOMAIN:-localhost}
- DEMO=${DEMO:-n}
- PREDEFINED_TOKEN=${PREDEFINED_TOKEN:-}
volumes:
- .:/opt/devicehub-django
ports:

View File

@ -25,9 +25,10 @@ deploy() {
INIT_USER="${INIT_USER:-user@example.org}"
INIT_PASSWD="${INIT_PASSWD:-1234}"
ADMIN='True'
PREDEFINED_TOKEN="${PREDEFINED_TOKEN:-}"
./manage.py add_institution "${INIT_ORG}"
# TODO: one error on add_user, and you don't add user anymore
./manage.py add_user "${INIT_ORG}" "${INIT_USER}" "${INIT_PASSWD}" "${ADMIN}"
./manage.py add_user "${INIT_ORG}" "${INIT_USER}" "${INIT_PASSWD}" "${ADMIN}" "${PREDEFINED_TOKEN}"
if [ "${DEMO:-}" ]; then
./manage.py up_snapshots example/snapshots/ "${INIT_USER}"

View File

@ -17,15 +17,17 @@ class Command(BaseCommand):
parser.add_argument('email', type=str, help='email')
parser.add_argument('password', type=str, help='password')
parser.add_argument('is_admin', nargs='?', default=False, type=str, help='is admin')
parser.add_argument('predefined_token', nargs='?', default='', type=str, help='predefined token')
def handle(self, *args, **kwargs):
email = kwargs['email']
password = kwargs['password']
is_admin = kwargs['is_admin']
predefined_token = kwargs['predefined_token']
institution = Institution.objects.get(name=kwargs['institution'])
self.create_user(institution, email, password, is_admin)
self.create_user(institution, email, password, is_admin, predefined_token)
def create_user(self, institution, email, password, is_admin):
def create_user(self, institution, email, password, is_admin, predefined_token):
self.u = User.objects.create(
institution=institution,
email=email,
@ -34,6 +36,10 @@ class Command(BaseCommand):
)
self.u.set_password(password)
self.u.save()
if predefined_token:
token = predefined_token
else:
token = uuid4()
Token.objects.create(token=token, owner=self.u)
print(f"TOKEN: {token}")