/
YamangulovAN
/
python_django
Обзор
Документация
Войти
/
YamangulovAN
/
python_django
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
HTML_CSS/myproject/mainapp/views.py
49 строк
2 KB
Andrei Iamangulov
homework module 7 дополнил работу, чтобы использовать django приложение
04 ноя 2025, 12:35
04 ноя 2025, 12:35
0473e2a
Код
Авторство
О чём код?
""" Views for mainapp: main page rendering and sending image archive via email. """ import os import zipfile from django.conf import settings from django.shortcuts import render from django.views.decorators.csrf import csrf_exempt from django.core.mail import EmailMessage from django.http import JsonResponse, HttpResponse def main_page(request): """ Render the main landing page with HTML+CSS+JS. """ return render(request, 'index.html') @csrf_exempt # не в продакшн так, CSRF - для чистого API примера def send_images(request): """ Receives POST with email, zips all img/ png/jpg, sends to user's email, removes zip file. """ if request.method == 'POST': user_email = request.POST.get('email') if not user_email: return JsonResponse({'success': False, 'message': 'No email'}, status=400) images_dir = os.path.join(settings.BASE_DIR, 'mainapp', 'static', 'img') archive_path = os.path.join(settings.BASE_DIR, 'images.zip') with zipfile.ZipFile(archive_path, 'w') as zipf: for file in os.listdir(images_dir): if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')): filepath = os.path.join(images_dir, file) zipf.write(filepath, file) email = EmailMessage( subject='Обработанные изображения проекта', body='Во вложении архив с обработанными изображениями.', from_email=settings.DEFAULT_FROM_EMAIL, to=[user_email] ) with open(archive_path, 'rb') as f: email.attach('images.zip', f.read(), 'application/zip') email.send() os.remove(archive_path) return JsonResponse({'success': True, 'message': 'Images sent successfully'}) return JsonResponse({'success': False, 'message': 'Invalid request'}, status=405)