Dive into Code

Discover code snippets, tutorials, and programming insights

Postgresql

postgresql schema for access control list

<p>The SQL code you provided creates several tables to implement a basic user, group, and permission system. Let&#39;s break down each table and its purpose:</p> <ol> <li> <p><code>public.user</code> table:</p> <ul> …

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
create table public.user(
id serial primary key not null,
name varchar(200),
created_at timestamp not null default now(),
updated_at timestamp not null default now(),
is_active BOOLEAN NOT NULL default True
);

create table public.group(
id serial primary key not null,
name varchar(200),
created_at timestamp not null default now(),
updated_at timestamp not null default now()
);

create table public.permission(
id serial primary key not null,
name varchar(200),
created_at timestamp not null default now(),
updated_at timestamp not null default now()
);

create table public.usergroups(
user_id int references public.user(id),
group_id int references public.group(id),
created_at timestamp not null default now(),
updated_at timestamp not null default now(),
PRIMARY KEY(user_id, group_id)
);

create table public.userpermissions(
user_id int references public.user(id),
permission_id int references public.permission(id),
created_at timestamp not null default now(),
updated_at timestamp not null default now(),
PRIMARY KEY(user_id, permission_id)
);

create table public.grouppermissions(
group_id int references public.user(id),
permission_id int references public.permission(id),
created_at timestamp not null default now(),
updated_at timestamp not null default now(),
PRIMARY KEY(group_id, permission_id)
);
Django

import function from differernt app in different project in django

<p>This Python function, named <code>function_importer</code>, is designed to dynamically import a function from a specified module within a given project directory. It performs the import in a way that allows …

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import importlib
import sys
import os
from importlib import import_module

def function_importer(project_name, module_name, function_name):
    project_dir = os.path.abspath(
        os.path.join(os.path.dirname(__file__),
                     '..', '..', '..', '{}'.format(
                     project_name)))

    sys.path.insert(1, project_dir)
    p, m = module_name.rsplit('.', 1)
    module = importlib.import_module(p)
    importlib.reload(module)
    try:
        loaded_module = getattr(module, m)
    except AttributeError:
        raise('Add from .{} import * into {}.views.py'.format(
            m, p))
    funct = getattr(loaded_module, function_name)
    return funct
Django

get request data in django

<p>The provided Python function <code>get_request_data(request)</code> appears to be a Django view function for handling HTTP GET requests and extracting the data from the request&#39;s query parameters. It parses the query …

python
1
2
3
4
5
6
7
8
9
def get_request_data(request):
    context = {}
    for key in request.GET.items():
        values = request.GET.getlist(key)
        if len(values) > 1:
            context[key] = [item for item in values]
        else:
            context[key] = values[0]
    return context