Skip to content

Meringue core¤

This is a basic package that contains a variety of general purpose functionality such as abstract models, various handlers, utilities, and more.

Mixins¤

CMTimeMixin¤

A primitive abstract model that adds the ctime and mtime fields to your model.

SortingMixin¤

An abstract model that adds a sortable field, as well as a manager with sorting correction functionality.

correction_sorting¤

This is a method to update/fix the sorting of the selected list of items.

The sorting will be done according to the queryset sorting, so sorting can be controlled by executing .order_by() before calling the correction_sorting method.

The selection for updating sorting can be pre-limited by filtering the list.

PublicationMixin¤

Mixin with the functionality of manual publishing.

Examples:

>>> FooModel.object.published()
>>> FooModel.object.unpublished()

published¤

Method to getting published items.

unpublished¤

Method for getting unpublished items.

PublicationDatesMixin¤

Mixin with the functionality of publishing in a certain period.

Examples:

>>> FooModel.object.published()
>>> FooModel.object.unpublished()

published¤

Method to getting published items.

unpublished¤

Method for getting unpublished items.

Utils¤

datetime¤

format_date_from_to¤

Method to display date period.

Possible output formats depending on the input data:

  • DD.MM.YYYY - DD.MM.YYYY
  • DD.MM - DD.MM.YYYY
  • DD - DD.MM.YYYY
  • DD.MM.YYYY

Examples:

>>> print(format_date_from_to(dt.date(2020, 1, 1), dt.date(2020, 2, 1)))
01.01 - 01.02.2020

Attributes:

  • date_start

    Period start date.

  • date_end

    Period end date.

  • delimiter

    Dates delimiter.

Returns:

  • str

    Date period string.

crypt¤

This module contains two extremely simplified functions for encrypting and decrypting a message using the AES algorithm and the GCM method. The main task that these functions are designed to solve is to encrypt small amounts of data for situations such as password recovery links and the like.

Note

These functions are just a wrapper for encryption methods from the pycryptodome library (you also need to install it).

encrypt_message¤

Method for encrypting a message with AES algorithm and the GCM method.

decrypt_message¤

Method for decrypting a message encrypted with AES algorithm and the GCM method.

Methods for encryption use a key that can be set in the CRYPTO_KEY parameter. By default, the parameter uses the first 32 characters of SECRET_KEY.

frontend¤

get_link is a method for getting a link to a resource.

Modern sites mainly work according to the scheme when the backend provides an api to which the front sends requests, in this regard, reverse, which provides django, cannot give actual links to the resource, but links are still needed in the backend (for example, in letters and sms sent to the user or in admin panel for managers). As a result, this small utility was implemented that will help you get a link to the desired resource.

To use the utility, you must specify a list of links in the FRONTEND_URLS parameter, and also, if you plan to receive absolute links, the frontend domain in the FRONTEND_DOMAIN parameter:

settings.py
MERINGUE = {
    "FRONTEND_URLS": {
        "index": "/"
        "user": "/user/{id}"
    },
    "FRONTEND_DOMAIN": "https://example.com",
}

You can get links in code like this:

>>> from meringue.core.utils.frontend import get_link
>>> get_link("index")
https://example.com/

>>> get_link("user", id=123)
https://example.com/user/123

Templatetags¤

cop_year¤

A tag that displays the year or range of years for the copyright string in YYYY-YYYY format.

Examples:

<p>Copyright © {% cop_year %} My company</p>

Raises:

  • Exception

    To use the cop_year tag, you must fill in the COP_YEAR parameter in the meringue settings

Returns:

  • str

    Year for copyrights.

For the tag to work, you must fill in the COP_YEAR parameter in the settings.

Also, in the COP_YEARS_DIFF parameter, you can specify the minimum difference in years when the period in copyrights will be displayed, and not the current year.

date_range¤

Return range of date in one of the following formats:

  • DD.MM.YYYY - DD.MM.YYYY
  • DD.MM - DD.MM.YYYY
  • DD - DD.MM.YYYY
  • DD.MM.YYYY

Attributes:

  • date_start

    Period start date.

  • date_end

    Period end date.

Examples:

{% date_range date_start date_end %}

Returns:

  • str

    Date period.

Translations¤

If you use djano-modeltranslation, then when connecting meringue.core, you can register fields for translations by setting the list of fields in the m_translate_fields field in the meta of the corresponding model:

class FooModel(models.Model):
    name = models.CharField(max_length=32)

    class Meta:
        m_translate_fields = ["name", ]

Upload handlers¤

The standard django load handlers leave the original file name where possible. However, often when uploading a file to the server, the file can be called somehow ugly (and sometimes indecent), in order to avoid this problem, the following two upload handlers are implemented - MemoryFileUploadHandler and TemporaryFileUploadHandler. These two loaders replace the corresponding django loaders but in the process they rename the file being loaded.

The renaming process can be overridden by specifying your own renaming method in the UPLOAD_RENAME_HANDLER parameter.

To use them, specify them in the FILE_UPLOAD_HANDLERS parameter:

FILE_UPLOAD_HANDLERS = [
    "meringue.core.upload_handlers.TemporaryFileUploadHandler",
    "meringue.core.upload_handlers.MemoryFileUploadHandler",
]

Views¤

im_a_teapot¤

We all at some point want to brew boiling water.

Examples:

urls.py
from django.urls import path
from meringue.core.views import im_a_teapot

urlpatterns = [
    path('make_coffee', im_a_teapot, name="make_coffee"),
]
Authors: Dmitry Dobrynin