PaferaPy Async 0.1
ASGI framework focused on simplicity and efficiency
Loading...
Searching...
No Matches
user.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4from pafera.db import *
5
7
8from pafera.validators import *
9
10# Flag constants for the user object
11USER_MUST_CHANGE_PASSWORD = 0x01
12USER_DISABLED = 0x02
13USER_NEED_APPROVAL = 0x04
14USER_REJECTED = 0x08
15USER_CAN_MANAGE_SELF = 0x10
16
17# Constants for user-user relationships
18#
19# For example, to get all friends, you can use
20# friends = g.db.Linked(user, system_user, USER_FRIEND)
21#
22# friends must be agreed upon by both users in order to take effect.
23#
24# acquaintances are added whenever two users send messages to one
25# another.
26#
27USER_FRIEND = 1
28USER_ACQUAINTANCE = 2
29USER_FOLLOW = 3
30USER_BLOCKED = 4
31
32# *********************************************************************
34 """User accounts for the system. Handles logins, uploads, storage
35 quotas, access tokens, and the such.
36
37 Pafera adds a place field to the traditional username/password
38 combination, both for added security and to allow different places
39 to use the same phone number for their own accounts. It's the
40 unique combination of (phonenumber, place, password) that
41 identifies a particular user.
42
43 Note that displayname is a translation field, meaning that that
44 a user can have their own names in different languages.
45
46 expiredate is when the user account will expire. Set it far into
47 the future for your own administrator account.
48
49 canmanage and managedby are used for managed accounts such as
50 students being managed by a teacher. Managed accounts cannot
51 change anything besides change their own password.
52
53 accesstokens are used by the system to allow APIs to assume the
54 identity of a user. The APIs used must be explicitly specified for
55 the token to work. The token format is a dict such as
56 {
57 "token": tokenvalue,
58 "expiredate": timestamp,
59 "apilist": [
60 '/system/fileapi:search',
61 '/system/fileapi:load',
62 '/system/fileapi:save'
63 ]
64 }
65 """
66
67 _dbfields = {
68 'rid': ('INTEGER PRIMARY KEY', 'NOT NULL',),
69 'phonenumber': ('TEXT', 'NOT NULL', BlankValidator()),
70 'place': ('TEXT', 'NOT NULL', BlankValidator()),
71 'password': ('PASSWORD', 'NOT NULL', BlankValidator()),
72 'email': ('TEXT', "NOT NULL DEFAULT ''",),
73 'displayname': ('TRANSLATION', 'NOT NULL', BlankValidator()),
74 'settings': ('DICT', "NOT NULL DEFAULT ''",),
75 'expiredate': ('DATETIME', "NOT NULL DEFAULT 0",),
76 'storageused': ('BIGINT', "NOT NULL DEFAULT 0",),
77 'storagequota': ('BIGINT', "NOT NULL DEFAULT 134217728",),
78 'numcanmmanage': ('INT', "NOT NULL DEFAULT 0",),
79 'canmanage': ('NEWLINELIST', "NOT NULL DEFAULT ''",),
80 'managedby': ('NEWLINELIST', "NOT NULL DEFAULT ''",),
81 'accesstokens': ('DICT', "NOT NULL DEFAULT ''",),
82 'flags': ('INT', 'NOT NULL DEFAULT 0',),
83 }
84 _dbindexes = (
85 ('unique', ('phonenumber', 'place')),
86 )
87 _dblinks = ['system_group', 'system_user']
88 _dbdisplay = ['place', 'displayname', 'phonenumber']
89 _dbflags = 0
90
91 _managejs = """
92G.displayfuncs['system_user'] = function(type, fields, item, ls, modellinks, data)
93{
94 let modelid = G.modelids['system_user'];
95 let modelinfo = G.modelfields['system_user'];
96 let idcode = ToShortCode(item['rid']);
97
98 if (type == 'card')
99 {
100 ls.push(`<div class="blueg Center">${item['rid']} (${idcode})</div>
101 <div>
102 ${P.HeadShotImg(idcode, 'Square600')}<br>
103 </div>`
104 );
105 } else
106 {
107 ls.push(`<th class="Center">
108 ${P.HeadShotImg(idcode, 'Square600')}<br>
109 ${item['rid']} (${idcode})
110 </th>`);
111 }
112
113 for (let j = 1, m = fields.length; j < m; j++)
114 {
115 let k = fields[j];
116
117 let value = item[k];
118 let valuetype = modelinfo[k][0];
119
120 if (type == 'card')
121 {
122 if (valuetype == 'JSON')
123 {
124 ls.push('<pre>' + JSON.stringify(value, null, 2) + '</pre>');
125 } else if (valuetype.indexOf('TRANSLATION') != -1)
126 {
127 ls.push('<div>' + P.BestTranslation(value) + '</div>');
128 } else
129 {
130 ls.push('<div>' + value + '</div>');
131 }
132 } else
133 {
134 if (valuetype == 'JSON')
135 {
136 ls.push('<td>' + JSON.stringify(value, null, 2) + '</td>');
137 } else if (valuetype.indexOf('TRANSLATION') != -1)
138 {
139 ls.push('<td>' + P.BestTranslation(value) + '</td>');
140 } else
141 {
142 ls.push('<td>' + value + '</td>');
143 }
144 }
145 }
146}
147"""
148
149 # -------------------------------------------------------------------
150 def __init__(self):
151 super().__init__()
152
153
154
155
Base class for all database models.
Definition: modelbase.py:20
Throws an exception on blank values.
Definition: validators.py:43
User accounts for the system.
Definition: user.py:33
def __init__(self)
Initialize all fields at creation like a good programmer should.
Definition: user.py:150
Definition: db.py:1
def ToShortCode(val, chars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_')
Turns a 32-bit value into a six character alphanumeric code.
Definition: utils.py:36