wjbmattingly commited on
Commit
540343f
·
verified ·
1 Parent(s): a980c4b

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +41 -46
Dockerfile CHANGED
@@ -157,20 +157,37 @@ RUN DJANGO_SETTINGS_MODULE=vlamy_ocr.settings_no_auth python manage.py collectst
157
  RUN cp /app/vlamy_ocr/urls_no_auth.py /app/vlamy_ocr/urls.py
158
 
159
  # Create anonymous user middleware for no-auth mode
160
- RUN echo 'from django.contrib.auth.models import User, AnonymousUser\n\
 
 
 
 
161
  \n\
162
  class AnonymousUserMiddleware:\n\
163
- """Middleware that automatically creates and assigns an anonymous user for all requests"""\n\
164
  \n\
165
  def __init__(self, get_response):\n\
166
  self.get_response = get_response\n\
167
  self._anonymous_user = None\n\
 
 
168
  \n\
169
- def __call__(self, request):\n\
170
- # Assign anonymous user to all requests\n\
171
- if not self._anonymous_user:\n\
172
- try:\n\
173
- # Get or create the anonymous user\n\
 
 
 
 
 
 
 
 
 
 
 
174
  self._anonymous_user, created = User.objects.get_or_create(\n\
175
  username="anonymous",\n\
176
  defaults={\n\
@@ -179,6 +196,7 @@ class AnonymousUserMiddleware:\n\
179
  "last_name": "User"\n\
180
  }\n\
181
  )\n\
 
182
  # Ensure user profile exists and is approved\n\
183
  if hasattr(self._anonymous_user, "profile"):\n\
184
  if not self._anonymous_user.profile.is_approved:\n\
@@ -186,13 +204,23 @@ class AnonymousUserMiddleware:\n\
186
  self._anonymous_user.profile.save()\n\
187
  else:\n\
188
  from ocr_app.models import UserProfile\n\
189
- UserProfile.objects.get_or_create(\n\
190
  user=self._anonymous_user,\n\
191
  defaults={"is_approved": True}\n\
192
  )\n\
193
- except Exception as e:\n\
194
- print(f"Error creating anonymous user: {e}")\n\
195
- self._anonymous_user = AnonymousUser()\n\
 
 
 
 
 
 
 
 
 
 
196
  \n\
197
  # Always assign the anonymous user to the request\n\
198
  request.user = self._anonymous_user\n\
@@ -207,43 +235,10 @@ RUN echo '#!/bin/bash\n\
207
  # Set the custom settings module\n\
208
  export DJANGO_SETTINGS_MODULE=vlamy_ocr.settings_no_auth\n\
209
  \n\
210
- # Run migrations for in-memory database (required for each startup)\n\
211
- python manage.py migrate --noinput\n\
212
- \n\
213
- # Create anonymous user with approved profile\n\
214
- python -c "\n\
215
- import os\n\
216
- os.environ.setdefault(\"DJANGO_SETTINGS_MODULE\", \"vlamy_ocr.settings_no_auth\")\n\
217
- import django\n\
218
- django.setup()\n\
219
- \n\
220
- from django.contrib.auth.models import User\n\
221
- from ocr_app.models import UserProfile\n\
222
- \n\
223
- try:\n\
224
- user, created = User.objects.get_or_create(\n\
225
- username=\"anonymous\",\n\
226
- defaults={\n\
227
- \"email\": \"[email protected]\",\n\
228
- \"first_name\": \"Anonymous\",\n\
229
- \"last_name\": \"User\"\n\
230
- }\n\
231
- )\n\
232
- profile, profile_created = UserProfile.objects.get_or_create(\n\
233
- user=user,\n\
234
- defaults={\"is_approved\": True}\n\
235
- )\n\
236
- if not profile.is_approved:\n\
237
- profile.is_approved = True\n\
238
- profile.save()\n\
239
- print(f\"Anonymous user ready: created={created}, profile_created={profile_created}\")\n\
240
- except Exception as e:\n\
241
- print(f\"Error creating anonymous user: {e}\")\n\
242
- "\n\
243
- \n\
244
  echo "Starting VLAMy in browser-only mode (no authentication required)"\n\
 
245
  \n\
246
- # Start the Django development server\n\
247
  python manage.py runserver 0.0.0.0:$PORT\n\
248
  ' > /app/start.sh
249
 
 
157
  RUN cp /app/vlamy_ocr/urls_no_auth.py /app/vlamy_ocr/urls.py
158
 
159
  # Create anonymous user middleware for no-auth mode
160
+ RUN echo 'import os\n\
161
+ from django.contrib.auth.models import User, AnonymousUser\n\
162
+ from django.core.management import execute_from_command_line\n\
163
+ from django.db import connection\n\
164
+ from django.apps import apps\n\
165
  \n\
166
  class AnonymousUserMiddleware:\n\
167
+ """Middleware that automatically handles migrations and creates an anonymous user for all requests"""\n\
168
  \n\
169
  def __init__(self, get_response):\n\
170
  self.get_response = get_response\n\
171
  self._anonymous_user = None\n\
172
+ self._migrations_applied = False\n\
173
+ self._setup_complete = False\n\
174
  \n\
175
+ def _ensure_migrations_and_user(self):\n\
176
+ """Ensure migrations are applied and anonymous user exists"""\n\
177
+ if self._setup_complete:\n\
178
+ return\n\
179
+ \n\
180
+ try:\n\
181
+ # Check if migrations need to be applied\n\
182
+ if not self._migrations_applied:\n\
183
+ print("Applying database migrations...")\n\
184
+ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "vlamy_ocr.settings_no_auth")\n\
185
+ execute_from_command_line(["manage.py", "migrate", "--noinput"])\n\
186
+ self._migrations_applied = True\n\
187
+ print("Migrations applied successfully")\n\
188
+ \n\
189
+ # Create anonymous user\n\
190
+ if not self._anonymous_user:\n\
191
  self._anonymous_user, created = User.objects.get_or_create(\n\
192
  username="anonymous",\n\
193
  defaults={\n\
 
196
  "last_name": "User"\n\
197
  }\n\
198
  )\n\
199
+ \n\
200
  # Ensure user profile exists and is approved\n\
201
  if hasattr(self._anonymous_user, "profile"):\n\
202
  if not self._anonymous_user.profile.is_approved:\n\
 
204
  self._anonymous_user.profile.save()\n\
205
  else:\n\
206
  from ocr_app.models import UserProfile\n\
207
+ profile, profile_created = UserProfile.objects.get_or_create(\n\
208
  user=self._anonymous_user,\n\
209
  defaults={"is_approved": True}\n\
210
  )\n\
211
+ \n\
212
+ print(f"Anonymous user ready: created={created}, user_id={self._anonymous_user.id}")\n\
213
+ \n\
214
+ self._setup_complete = True\n\
215
+ \n\
216
+ except Exception as e:\n\
217
+ print(f"Error in middleware setup: {e}")\n\
218
+ # Fallback to Django AnonymousUser\n\
219
+ self._anonymous_user = AnonymousUser()\n\
220
+ \n\
221
+ def __call__(self, request):\n\
222
+ # Ensure setup is complete\n\
223
+ self._ensure_migrations_and_user()\n\
224
  \n\
225
  # Always assign the anonymous user to the request\n\
226
  request.user = self._anonymous_user\n\
 
235
  # Set the custom settings module\n\
236
  export DJANGO_SETTINGS_MODULE=vlamy_ocr.settings_no_auth\n\
237
  \n\
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  echo "Starting VLAMy in browser-only mode (no authentication required)"\n\
239
+ echo "Anonymous user will be created automatically on first request"\n\
240
  \n\
241
+ # Start the Django development server (migrations will run automatically)\n\
242
  python manage.py runserver 0.0.0.0:$PORT\n\
243
  ' > /app/start.sh
244