Spaces:
Running
Running
Update Dockerfile
Browse files- Dockerfile +82 -9
Dockerfile
CHANGED
|
@@ -123,6 +123,9 @@ LOGGING = {\n\
|
|
| 123 |
},\n\
|
| 124 |
}\n\
|
| 125 |
\n\
|
|
|
|
|
|
|
|
|
|
| 126 |
' > /app/vlamy_ocr/settings_no_auth.py
|
| 127 |
|
| 128 |
# Create a custom URL configuration for no-auth mode
|
|
@@ -150,26 +153,96 @@ if settings.DEBUG:\n\
|
|
| 150 |
# Collect static files
|
| 151 |
RUN DJANGO_SETTINGS_MODULE=vlamy_ocr.settings_no_auth python manage.py collectstatic --noinput
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
# Create a startup script for no-auth mode
|
| 154 |
RUN echo '#!/bin/bash\n\
|
| 155 |
\n\
|
| 156 |
# Set the custom settings module\n\
|
| 157 |
export DJANGO_SETTINGS_MODULE=vlamy_ocr.settings_no_auth\n\
|
| 158 |
\n\
|
| 159 |
-
# Replace the main urls.py with no-auth version (create backup first)\n\
|
| 160 |
-
cp /app/vlamy_ocr/urls.py /app/vlamy_ocr/urls_original.py\n\
|
| 161 |
-
cp /app/vlamy_ocr/urls_no_auth.py /app/vlamy_ocr/urls.py\n\
|
| 162 |
-
\n\
|
| 163 |
# Run migrations for in-memory database (required for each startup)\n\
|
| 164 |
python manage.py migrate --noinput\n\
|
| 165 |
\n\
|
| 166 |
-
# Create
|
| 167 |
-
python
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
"\n\
|
| 172 |
\n\
|
|
|
|
|
|
|
| 173 |
# Start the Django development server\n\
|
| 174 |
python manage.py runserver 0.0.0.0:$PORT\n\
|
| 175 |
' > /app/start.sh
|
|
|
|
| 123 |
},\n\
|
| 124 |
}\n\
|
| 125 |
\n\
|
| 126 |
+
# Create anonymous user middleware reference\n\
|
| 127 |
+
MIDDLEWARE.insert(0, "vlamy_ocr.middleware.AnonymousUserMiddleware")\n\
|
| 128 |
+
\n\
|
| 129 |
' > /app/vlamy_ocr/settings_no_auth.py
|
| 130 |
|
| 131 |
# Create a custom URL configuration for no-auth mode
|
|
|
|
| 153 |
# Collect static files
|
| 154 |
RUN DJANGO_SETTINGS_MODULE=vlamy_ocr.settings_no_auth python manage.py collectstatic --noinput
|
| 155 |
|
| 156 |
+
# Replace the main urls.py with no-auth version at build time
|
| 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\
|
| 177 |
+
"email": "[email protected]",\n\
|
| 178 |
+
"first_name": "Anonymous",\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\
|
| 185 |
+
self._anonymous_user.profile.is_approved = True\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\
|
| 199 |
+
\n\
|
| 200 |
+
response = self.get_response(request)\n\
|
| 201 |
+
return response\n\
|
| 202 |
+
' > /app/vlamy_ocr/middleware.py
|
| 203 |
+
|
| 204 |
# Create a startup script for no-auth mode
|
| 205 |
RUN echo '#!/bin/bash\n\
|
| 206 |
\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
|