How can I implement built in signals, for my app?

24 views
Skip to first unread message

Joel Mathew

unread,
Oct 21, 2018, 12:52:09 PM10/21/18
to django...@googlegroups.com
How to implement built in signals, for my app?

I have a project myappointments, with two apps appointments and clinic in it.

Objective:
When a user logins, details should be entered in the database.

appointments/models.py:

class Event(models.Model):
id=models.AutoField(primary_key=True, unique=True)
type=models.CharField(max_length=60)
description = models.CharField(max_length=150)
time = models.DateTimeField(default=timezone.now)

appointments/__init__.py:

default_app_config = 'appointments.apps.AppointmentsConfig'

appointments/apps.py:

from django.apps import AppConfig
class AppointmentsConfig(AppConfig):
name = 'appointments'
def ready(self):
import appointments.signals # noqa

appointments/signals.py:

from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver
def record_loggedin(sender, user, request, **kwargs):
ev = Event(type="Login", description = 'User logged in:
{}'.format(request.user.id))
ev.save()
print("User has logged in. Saved event to DB.")
user_logged_in.connect(record_loggedin)

What other modifications do I need to do this?

- Joel G Mathew

Matthew Pava

unread,
Oct 22, 2018, 9:46:17 AM10/22/18
to django...@googlegroups.com
You may want to look at this middleware package that will automatically audit user logins.
https://github.com/muccg/django-useraudit

Also, in using AppConfigs, avoid using default_app_config as stated here:
https://docs.djangoproject.com/en/2.1/ref/applications/#configuring-applications
"New applications should avoid default_app_config. Instead they should require the dotted path to the appropriate AppConfig subclass to be configured explicitly in INSTALLED_APPS."

Your settings file should have something like this:
INSTALLED_APPS = [....,'appointments.apps.AppointmentsConfig', ...]
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3Diw_8ViZgvx8ugBaC0_1uqaOUkCMLmS7adMCoQUmOZL9nJfQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages