updated migrations + setup process update

This commit is contained in:
vabene1111 2021-02-21 15:34:35 +01:00
parent 846c660811
commit 24e43e3e2e
13 changed files with 164 additions and 141 deletions

View File

@ -22,7 +22,7 @@ admin.site.unregister(Group)
class SpaceAdmin(admin.ModelAdmin): class SpaceAdmin(admin.ModelAdmin):
list_display = ('name', 'message') list_display = ('name', 'created_by', 'message')
admin.site.register(Space, SpaceAdmin) admin.site.register(Space, SpaceAdmin)

View File

@ -2,6 +2,8 @@ from django.shortcuts import redirect
from django.urls import reverse from django.urls import reverse
from django_scopes import scope, scopes_disabled from django_scopes import scope, scopes_disabled
from cookbook.views import views
class ScopeMiddleware: class ScopeMiddleware:
def __init__(self, get_response): def __init__(self, get_response):
@ -10,17 +12,17 @@ class ScopeMiddleware:
def __call__(self, request): def __call__(self, request):
if request.user.is_authenticated: if request.user.is_authenticated:
if request.user.groups.count() == 0:
return redirect('view_no_group')
with scopes_disabled():
if request.user.userpreference.space is None and not reverse('view_no_space') in request.path and not reverse('account_logout') in request.path:
return redirect(reverse('view_no_space'))
if request.path.startswith('/admin/'): if request.path.startswith('/admin/'):
with scopes_disabled(): with scopes_disabled():
return self.get_response(request) return self.get_response(request)
with scopes_disabled():
if request.user.userpreference.space is None and not reverse('account_logout') in request.path:
return views.no_space(request)
if request.user.groups.count() == 0 and not reverse('account_logout') in request.path:
return views.no_groups(request)
request.space = request.user.userpreference.space request.space = request.user.userpreference.space
# with scopes_disabled(): # with scopes_disabled():
with scope(space=request.space): with scope(space=request.space):

View File

@ -1,20 +1,22 @@
# Generated by Django 3.0.2 on 2020-01-30 09:59 # Generated by Django 3.0.2 on 2020-01-30 09:59
from django.db import migrations from django.db import migrations
from django_scopes import scopes_disabled
def migrate_ingredient_units(apps, schema_editor): def migrate_ingredient_units(apps, schema_editor):
Unit = apps.get_model('cookbook', 'Unit') with scopes_disabled():
RecipeIngredients = apps.get_model('cookbook', 'RecipeIngredients') Unit = apps.get_model('cookbook', 'Unit')
RecipeIngredients = apps.get_model('cookbook', 'RecipeIngredients')
for u in RecipeIngredients.objects.values('unit').distinct(): for u in RecipeIngredients.objects.values('unit').distinct():
unit = Unit() unit = Unit()
unit.name = u['unit'] unit.name = u['unit']
unit.save() unit.save()
for i in RecipeIngredients.objects.all(): for i in RecipeIngredients.objects.all():
i.unit_key = Unit.objects.get(name=i.unit) i.unit_key = Unit.objects.get(name=i.unit)
i.save() i.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -1,19 +1,21 @@
# Generated by Django 3.0.2 on 2020-02-16 22:09 # Generated by Django 3.0.2 on 2020-02-16 22:09
from django.db import migrations from django.db import migrations
from django_scopes import scopes_disabled
def migrate_ingredients(apps, schema_editor): def migrate_ingredients(apps, schema_editor):
Ingredient = apps.get_model('cookbook', 'Ingredient') with scopes_disabled():
RecipeIngredient = apps.get_model('cookbook', 'RecipeIngredient') Ingredient = apps.get_model('cookbook', 'Ingredient')
RecipeIngredient = apps.get_model('cookbook', 'RecipeIngredient')
for u in RecipeIngredient.objects.values('name').distinct(): for u in RecipeIngredient.objects.values('name').distinct():
ingredient = Ingredient() ingredient = Ingredient()
ingredient.name = u['name'] ingredient.name = u['name']
ingredient.save() ingredient.save()
for i in RecipeIngredient.objects.all(): for i in RecipeIngredient.objects.all():
i.ingredient = Ingredient.objects.get(name=i.name) i.ingredient = Ingredient.objects.get(name=i.name)
i.save() i.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -1,15 +1,17 @@
# Generated by Django 3.0.5 on 2020-04-26 14:14 # Generated by Django 3.0.5 on 2020-04-26 14:14
from django.db import migrations from django.db import migrations
from django_scopes import scopes_disabled
def apply_migration(apps, schema_editor): def apply_migration(apps, schema_editor):
Group = apps.get_model('auth', 'Group') with scopes_disabled():
Group.objects.bulk_create([ Group = apps.get_model('auth', 'Group')
Group(name=u'guest'), Group.objects.bulk_create([
Group(name=u'user'), Group(name=u'guest'),
Group(name=u'admin'), Group(name=u'user'),
]) Group(name=u'admin'),
])
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -1,15 +1,17 @@
# Generated by Django 3.0.5 on 2020-04-27 16:00 # Generated by Django 3.0.5 on 2020-04-27 16:00
from django.db import migrations from django.db import migrations
from django_scopes import scopes_disabled
def apply_migration(apps, schema_editor): def apply_migration(apps, schema_editor):
Group = apps.get_model('auth', 'Group') with scopes_disabled():
User = apps.get_model('auth', 'User') Group = apps.get_model('auth', 'Group')
for u in User.objects.all(): User = apps.get_model('auth', 'User')
if u.groups.count() < 1: for u in User.objects.all():
u.groups.add(Group.objects.get(name='admin')) if u.groups.count() < 1:
u.save() u.groups.add(Group.objects.get(name='admin'))
u.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -2,43 +2,45 @@
from django.db import migrations from django.db import migrations
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django_scopes import scopes_disabled
def migrate_meal_types(apps, schema_editor): def migrate_meal_types(apps, schema_editor):
MealPlan = apps.get_model('cookbook', 'MealPlan') with scopes_disabled():
MealType = apps.get_model('cookbook', 'MealType') MealPlan = apps.get_model('cookbook', 'MealPlan')
MealType = apps.get_model('cookbook', 'MealType')
breakfast = MealType.objects.create( breakfast = MealType.objects.create(
name=_('Breakfast'), name=_('Breakfast'),
order=0, order=0,
) )
lunch = MealType.objects.create( lunch = MealType.objects.create(
name=_('Lunch'), name=_('Lunch'),
order=0, order=0,
) )
dinner = MealType.objects.create( dinner = MealType.objects.create(
name=_('Dinner'), name=_('Dinner'),
order=0, order=0,
) )
other = MealType.objects.create( other = MealType.objects.create(
name=_('Other'), name=_('Other'),
order=0, order=0,
) )
for m in MealPlan.objects.all(): for m in MealPlan.objects.all():
if m.meal == 'BREAKFAST': if m.meal == 'BREAKFAST':
m.meal_type = breakfast m.meal_type = breakfast
if m.meal == 'LUNCH': if m.meal == 'LUNCH':
m.meal_type = lunch m.meal_type = lunch
if m.meal == 'DINNER': if m.meal == 'DINNER':
m.meal_type = dinner m.meal_type = dinner
if m.meal == 'OTHER': if m.meal == 'OTHER':
m.meal_type = other m.meal_type = other
m.save() m.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -2,22 +2,24 @@
from django.db import migrations from django.db import migrations
from django.db.models import Q from django.db.models import Q
from django_scopes import scopes_disabled
def migrate_meal_types(apps, schema_editor): def migrate_meal_types(apps, schema_editor):
MealPlan = apps.get_model('cookbook', 'MealPlan') with scopes_disabled():
MealType = apps.get_model('cookbook', 'MealType') MealPlan = apps.get_model('cookbook', 'MealPlan')
User = apps.get_model('auth', 'User') MealType = apps.get_model('cookbook', 'MealType')
User = apps.get_model('auth', 'User')
for u in User.objects.all(): for u in User.objects.all():
for t in MealType.objects.filter(created_by=None).all(): for t in MealType.objects.filter(created_by=None).all():
user_type = MealType.objects.create( user_type = MealType.objects.create(
name=t.name, name=t.name,
created_by=u, created_by=u,
) )
MealPlan.objects.filter(Q(created_by=u) and Q(meal_type=t)).update(meal_type=user_type) MealPlan.objects.filter(Q(created_by=u) and Q(meal_type=t)).update(meal_type=user_type)
MealType.objects.filter(created_by=None).delete() MealType.objects.filter(created_by=None).delete()
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -3,11 +3,14 @@
from django.db import migrations, models from django.db import migrations, models
import uuid import uuid
from django_scopes import scopes_disabled
def invalidate_shares(apps, schema_editor): def invalidate_shares(apps, schema_editor):
ShareLink = apps.get_model('cookbook', 'ShareLink') with scopes_disabled():
ShareLink = apps.get_model('cookbook', 'ShareLink')
ShareLink.objects.all().delete() ShareLink.objects.all().delete()
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -1,16 +1,18 @@
# Generated by Django 3.0.7 on 2020-06-25 19:37 # Generated by Django 3.0.7 on 2020-06-25 19:37
from django.db import migrations from django.db import migrations
from django_scopes import scopes_disabled
def migrate_ingredients(apps, schema_editor): def migrate_ingredients(apps, schema_editor):
Recipe = apps.get_model('cookbook', 'Recipe') with scopes_disabled():
Ingredient = apps.get_model('cookbook', 'Ingredient') Recipe = apps.get_model('cookbook', 'Recipe')
Ingredient = apps.get_model('cookbook', 'Ingredient')
for r in Recipe.objects.all(): for r in Recipe.objects.all():
for i in Ingredient.objects.filter(recipe=r).all(): for i in Ingredient.objects.filter(recipe=r).all():
r.ingredients.add(i) r.ingredients.add(i)
r.save() r.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -1,21 +1,23 @@
# Generated by Django 3.0.7 on 2020-06-25 20:19 # Generated by Django 3.0.7 on 2020-06-25 20:19
from django.db import migrations, models from django.db import migrations, models
from django_scopes import scopes_disabled
def create_default_step(apps, schema_editor): def create_default_step(apps, schema_editor):
Recipe = apps.get_model('cookbook', 'Recipe') with scopes_disabled():
Step = apps.get_model('cookbook', 'Step') Recipe = apps.get_model('cookbook', 'Recipe')
Step = apps.get_model('cookbook', 'Step')
for r in Recipe.objects.filter(internal=True).all(): for r in Recipe.objects.filter(internal=True).all():
s = Step.objects.create( s = Step.objects.create(
instruction=r.instructions instruction=r.instructions
) )
for i in r.ingredients.all(): for i in r.ingredients.all():
s.ingredients.add(i) s.ingredients.add(i)
s.save() s.save()
r.steps.add(s) r.steps.add(s)
r.save() r.save()
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -2,27 +2,29 @@
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
from django_scopes import scopes_disabled
def convert_old_specials(apps, schema_editor): def convert_old_specials(apps, schema_editor):
Ingredient = apps.get_model('cookbook', 'Ingredient') with scopes_disabled():
Food = apps.get_model('cookbook', 'Food') Ingredient = apps.get_model('cookbook', 'Ingredient')
Unit = apps.get_model('cookbook', 'Unit') Food = apps.get_model('cookbook', 'Food')
Unit = apps.get_model('cookbook', 'Unit')
for i in Ingredient.objects.all(): for i in Ingredient.objects.all():
if i.amount == 0: if i.amount == 0:
i.no_amount = True i.no_amount = True
if i.unit.name == 'Special:Header': if i.unit.name == 'Special:Header':
i.header = True i.header = True
i.unit = None i.unit = None
i.food = None i.food = None
i.save() i.save()
try: try:
Unit.objects.filter(name='Special:Header').delete() Unit.objects.filter(name='Special:Header').delete()
Food.objects.filter(name='Header').delete() Food.objects.filter(name='Header').delete()
except Exception: except Exception:
pass pass
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -7,6 +7,7 @@ from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth import update_session_auth_hash from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.models import Group
from django.contrib.auth.password_validation import validate_password from django.contrib.auth.password_validation import validate_password
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import IntegrityError from django.db import IntegrityError
@ -85,10 +86,6 @@ def search(request):
def no_groups(request): def no_groups(request):
if not request.user.is_authenticated:
return HttpResponseRedirect(reverse('account_login') + '?next=' + request.GET['next'])
if request.user.is_authenticated and request.user.groups.count() > 0:
return HttpResponseRedirect(reverse('index'))
return render(request, 'no_groups_info.html') return render(request, 'no_groups_info.html')
@ -335,37 +332,40 @@ def system(request):
def setup(request): def setup(request):
if User.objects.count() > 0 or 'django.contrib.auth.backends.RemoteUserBackend' in settings.AUTHENTICATION_BACKENDS: with scopes_disabled():
messages.add_message(request, messages.ERROR, _('The setup page can only be used to create the first user! If you have forgotten your superuser credentials please consult the django documentation on how to reset passwords.')) if User.objects.count() > 0 or 'django.contrib.auth.backends.RemoteUserBackend' in settings.AUTHENTICATION_BACKENDS:
return HttpResponseRedirect(reverse('account_login')) messages.add_message(request, messages.ERROR, _('The setup page can only be used to create the first user! If you have forgotten your superuser credentials please consult the django documentation on how to reset passwords.'))
return HttpResponseRedirect(reverse('account_login'))
if request.method == 'POST': if request.method == 'POST':
form = UserCreateForm(request.POST) form = UserCreateForm(request.POST)
if form.is_valid(): if form.is_valid():
if form.cleaned_data['password'] != form.cleaned_data['password_confirm']: # noqa: E501 if form.cleaned_data['password'] != form.cleaned_data['password_confirm']:
form.add_error('password', _('Passwords dont match!')) form.add_error('password', _('Passwords dont match!'))
else: else:
user = User(username=form.cleaned_data['name'], is_superuser=True, is_staff=True) user = User(username=form.cleaned_data['name'], is_superuser=True, is_staff=True)
try: try:
validate_password(form.cleaned_data['password'], user=user) validate_password(form.cleaned_data['password'], user=user)
user.set_password(form.cleaned_data['password']) user.set_password(form.cleaned_data['password'])
user.save() user.save()
user.userpreference.space = Space.objects.first()
user.userpreference.save() user.groups.add(Group.objects.get(name='admin'))
user.userpreference.space = Space.objects.first()
user.userpreference.save()
with scopes_disabled():
for x in Space.objects.all(): for x in Space.objects.all():
x.created_by = user x.created_by = user
x.save() x.save()
messages.add_message(request, messages.SUCCESS, _('User has been created, please login!')) messages.add_message(request, messages.SUCCESS, _('User has been created, please login!'))
return HttpResponseRedirect(reverse('account_login')) return HttpResponseRedirect(reverse('account_login'))
except ValidationError as e: except ValidationError as e:
for m in e: for m in e:
form.add_error('password', m) form.add_error('password', m)
else: else:
form = UserCreateForm() form = UserCreateForm()
return render(request, 'setup.html', {'form': form}) return render(request, 'setup.html', {'form': form})
def signup(request, token): def signup(request, token):