Skip to main content

A Django package to create and manage user-specific SQLite databases.

Project description

Django SQLite User DB (DSUD)

codecov

Disclaimer:
This package is purely experimental and is not intended for production use. Use at your own discretion.


Overview

Django SQLite User DB (DSUD) is a Django package that allows each user to have their own individual SQLite database. This enables user-specific data management while maintaining a lightweight and simple setup. Perfect for development, prototyping, and learning, DSUD provides a unique approach to user data storage.

Features

  • Individual SQLite databases for each user
  • Easy integration with existing Django projects
  • Lightweight and minimal configuration

Installation

To install DSUD, do the following modifications in your Django settings:

INSTALLED_APPS = [
    ...
    'dsud',
    ...
]
MIDDLEWARE = [
    ...
    "dsud.middleware.DatabaseSwitchMiddleware",
    ...
]
DATABASE_ROUTERS = ["dsud.router.UserSpecificDatabaseRouter"]
TEST_RUNNER = "dsud.test_runner.UserSQLiteDBManagerTestRunner"

Defining Users For Test Cases

In your settings, you can define test users by adding setting USER_SQLITE_DB_MANAGER_TEST_USER_DETAILS. This setting should contain a list of user payloads, such as:

USER_SQLITE_DB_MANAGER_TEST_USER_DETAILS = [
    {"id": 1337, "username": "user_1337", "password": "secret"},
    # Add more test users as needed
]

These users will be enrolled during the test runner's setup_database step, including the creation of their individual databases. They will also be automatically torn down after the tests are completed.

Admin Panel Integration

This assumes you've registered dsud.admin.UserAdmin for your User model, or inherit from it in a custom model admin and include "switch_user_button" in list_display.

To easily manage user databases through the Django admin panel, follow these steps:

  1. Update urls.py:

    Add the following path to your base urls.py file:

    from django.urls import path, include
    
    urlpatterns = [
         path(
              "admin/",
              include(
                    "dsud.urls",
                    namespace="dsud",
              ),
         ),
         ...
    ]
    
  2. Customize Admin Templates:

    Create a file at templates/admin/base_site.html with the following contents:

    {% extends "dsud/base_site.html" %}
    

This will allow you to manage user-specific databases directly from the Django admin interface. Simply go to the users table, and click the "Switch user" button

Manual Database Entry

If you need to create entries in the user-specific database outside of requests (like test case setups for example), you can use the set_user_in_middleware context manager provided by DSUD. Here is an example:

from dsud.context_manager import set_user_in_middleware
from your_app.models import Model

# Assuming you have a user instance
user = User.objects.get(username='example_user')

# Manually create entries in the user's database
with set_user_in_middleware(user):
    obj1 = Model.objects.create(...)
    obj2 = Model.objects.create(...)

This approach allows you to explicitly set the user context and perform database operations within that context, ensuring that the entries are created in the correct user-specific database.


Feel free to customize further based on your project's specific details!

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

django_sqlite_user_db-0.1.1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

django_sqlite_user_db-0.1.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file django_sqlite_user_db-0.1.1.tar.gz.

File metadata

  • Download URL: django_sqlite_user_db-0.1.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for django_sqlite_user_db-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5cbbc531d6e2588ebe1f293125f6caebbed7596bd03569a5dcc858719edc4a44
MD5 5bc3e6f2cb4d1299964ee3a1de6c368c
BLAKE2b-256 75e929f2964bce4c5829f628c513db28f208c70233c0642be755ffa3d9f64f9b

See more details on using hashes here.

File details

Details for the file django_sqlite_user_db-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sqlite_user_db-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cec45819e232f6be75329f64a8aea53567f7e13b286a1d9d0b52b32f20eef4fa
MD5 1fcf7d13e2267533eeb7c21497a8cc58
BLAKE2b-256 defc6b36734b074fd522d2da3429a4a4356f600740f91fc527a9d77796be14d7

See more details on using hashes here.

Supported by

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