Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 63 additions & 10 deletions cms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@

from django.core.cache import cache

import logging

logger = logging.getLogger("mail_logs.cms")

cache = caches['default']


def dispatcher(request, permalink=''):
if permalink == '':
return HttpResponseRedirect('/')
Expand Down Expand Up @@ -154,8 +161,22 @@ def send_registration_confirmation(user):
#email.attach_alternative(message, "text/html")
try:
result = email.send(fail_silently=False)
logger.info(
"Registration email sent | username=%s | user_id=%s | email=%s | subject=%s",
user.username,
user.id,
user.email,
subject,
)
return code
except Exception as e:
logger.error(
"Registration email failed | username=%s | user_id=%s | email=%s | error=%s",
user.username,
user.id,
user.email,
str(e),
)
return None

def email_otp(user, password):
Expand Down Expand Up @@ -196,7 +217,20 @@ def email_otp(user, password):
#email.attach_alternative(message, "text/html")
try:
result = email.send(fail_silently=False)
except:
logger.info(
"OTP email sent | username=%s | user_id=%s | email=%s",
user.username,
user.id,
user.email,
)
except Exception as e:
logger.error(
"OTP email failed | username=%s | user_id=%s | email=%s | error=%s",
user.username,
user.id,
user.email,
str(e),
)
pass


Expand Down Expand Up @@ -419,15 +453,34 @@ def password_reset(request):
to = to, bcc = [], cc = [],
headers={'Reply-To': 'no-reply@spoken-tutorial.org', "Content-type":"text/html;charset=iso-8859-1"}
)

result = email.send(fail_silently=False)
# redirect to next url if there or redirect to login page
# use for forum password rest form
redirectNext = request.GET.get('next', False)
if redirectNext:
return HttpResponseRedirect(redirectNext)
messages.success(request, "New password sent to your email "+user.email)
return HttpResponseRedirect('/accounts/change-password/')
try:
result = email.send(fail_silently=False)
logger.info(
"Password reset email sent | user=%s | user_id=%s | email=%s | ip=%s | path=%s",
getattr(user, "username", None),
getattr(user, "id", None),
getattr(user, "email", None),
request.META.get("REMOTE_ADDR"),
request.path,
)
# redirect to next url if there or redirect to login page
# use for forum password rest form
redirectNext = request.GET.get('next', False)
if redirectNext:
return HttpResponseRedirect(redirectNext)
messages.success(request, "New password sent to your email "+user.email)
return HttpResponseRedirect('/accounts/change-password/')
except Exception as e:
logger.error(
"Password reset email failed | user=%s | user_id=%s | email=%s | ip=%s | path=%s | error=%s",
getattr(user, "username", None),
getattr(user, "id", None),
getattr(user, "email", None),
request.META.get("REMOTE_ADDR"),
request.path,
str(e),
)
messages.error(request, "Failed to send password reset email. Please try again.")


context = {
Expand Down
14 changes: 14 additions & 0 deletions donate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
from donate.payment import save_ilw_hdfc_success_data, save_ilw_hdfc_error_data, get_ilw_session_payload, make_hdfc_session_request
from training.models import TrainingEvents
from decimal import Decimal, InvalidOperation

import logging
logger = logging.getLogger("mail_logs.donate")

# @csrf_exempt
# def donatehome(request):
# form = PayeeForm(initial={'country': 'India'})
Expand Down Expand Up @@ -299,6 +303,11 @@ def send_onetime(request):
if user.is_active:
context['message'] = "active_user"
else:
logger.info(
"Registration email triggered | user=%s | email=%s",
getattr(user, "username", None),
getattr(user, "email", None),
)
send_registration_confirmation(user)
context['message'] = "inactive_user"

Expand All @@ -311,6 +320,11 @@ def send_onetime(request):
user.is_active = False
user.save()
create_profile(user, '')
logger.info(
"OTP email triggered | user=%s | email=%s",
getattr(user, "username", None),
getattr(user, "email", None),
)
email_otp(user, password)
context['message'] = "new"

Expand Down
13 changes: 12 additions & 1 deletion spoken/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,13 @@
"class": "logging.FileHandler",
"filename": ALLOWED_LOGS['training'],
"formatter": "simple",
}
},
"mail_file": {
"level": "WARNING",
"class": "logging.FileHandler",
"filename": ALLOWED_LOGS['spoken-mails.log'],
"formatter": "simple",
},
},

"loggers": {
Expand All @@ -478,5 +484,10 @@
'level': 'ERROR',
'propagate': False,
},
"mail_logs": {
"handlers": ["mail_file"],
"level": "WARNING",
"propagate": False,
},
},
}
63 changes: 54 additions & 9 deletions training/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@

import csv
import requests
import logging

logger = logging.getLogger("mail_logs.training")

def ensure_username_equals_email(user):
"""
Ensure Django username always matches email.
This is required for ILW Moodle authentication.
"""
email = (user.email or "").strip().lower()

if email and user.username != email:
# Prevent duplicate username conflicts
if not User.objects.filter(username=email).exclude(id=user.id).exists():
user.username = email
user.save(update_fields=["username"])


class TrainingEventCreateView(CreateView):
form_class = CreateTrainingEventForm
Expand Down Expand Up @@ -808,6 +825,11 @@ def get_create_user(self, row):
try:
user = User(username=email, email=email, first_name=first, last_name=last)
user.set_password(password)
logger.info(
"Registration email triggered | user=%s | email=%s",
email,
email,
)
confirmation_code = send_registration_confirmation(user)
if confirmation_code:
user.save()
Expand Down Expand Up @@ -917,15 +939,38 @@ def ajax_check_college(request):


def get_create_user(row):
try:
return User.objects.get(email=row[2].strip())
except User.DoesNotExist:
user = User(username=row[2], email=row[2].strip(), first_name=row[0], last_name=row[1])
user.set_password(row[0]+'@ST'+str(random.random()).split('.')[1][:5])
user.save()
create_profile(user, '')
send_registration_confirmation(user)
return user
email = row[2].strip().lower()

try:
user = User.objects.get(email=email)
ensure_username_equals_email(user)
return user

except User.DoesNotExist:
user = User(
username=email,
email=email,
first_name=row[0],
last_name=row[1]
)

user.set_password(row[0] + '@ST' + str(random.random()).split('.')[1][:5])
user.save()

ensure_username_equals_email(user)

create_profile(user, '')

logger.info(
"Registration email triggered | user=%s | user_id=%s | email=%s",
getattr(user, "username", None),
getattr(user, "id", None),
getattr(user, "email", None),
)

send_registration_confirmation(user)

return user

from io import TextIOWrapper
from django.contrib.auth.decorators import login_required
Expand Down