Skip to main content

Simple email confirmation for django model instances.

Project description

djeveric

Simple email confirmation for django model instances with Django Rest Framework.

Usage

Create a model

Create a model class inheriting from ConfirmableModelMixin with a ConfirmationField and refer to a ConfirmationEmail class like this:

from django.conf import settings
from django.db import models

from djeveric.emails import ConfirmationEmail
from djeveric.fields import ConfirmationField
from djeveric.models import ConfirmableModelMixin

class MyModelConfirmationEmail(ConfirmationEmail):
    subject = "Please confirm"
    
    def get_body(self, context):
        return f"Use this link to confirm: http://my-frontend/confirm/{self.token}"
    
    def get_recipient(self) -> str:
        return self.instance.owner.email

class MyModel(ConfirmableModelMixin, models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    is_confirmed = ConfirmationField(email_class=MyModelConfirmationEmail)

When unconfirmed instances of the model are saved, djeveric sends a confirmation email to the specified address.

Create a ViewSet

To actually confirm a viewset, your backend needs a view set using the ConfirmModelMixin:

from rest_framework import viewsets

from djeveric.views import ConfirmModelMixin


class MyModelViewSet(ConfirmModelMixin, viewsets.GenericViewSet):
    queryset = MyModel.objects

On a POST /api/my-models/{pk}/confirm/ with {"token": "THE TOKEN"} as data the model instance will be confirmed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djeveric-2.2.1.tar.gz (4.4 kB view details)

Uploaded Source

File details

Details for the file djeveric-2.2.1.tar.gz.

File metadata

  • Download URL: djeveric-2.2.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for djeveric-2.2.1.tar.gz
Algorithm Hash digest
SHA256 854167ea099bf4a6817dfea2812421c04b14d3bac1ee5df1d1f613947a3d69d6
MD5 f5a0a0acbcb55e90458d41e4cb9f0e19
BLAKE2b-256 4d1306eb5dbe1465422860c4e60a9dc4f52cdf36fcc9cd4c8e563d854f4b6ba5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page