18 """Tracker for the various types of programs that visit the site.
19 Note that this will not track every single visitor since sessions
20 depend on JavaScript being enabled to load sessionvars.js, but can
21 be a good approximation of human visits.
25 'id': (
'INTEGER PRIMARY KEY AUTOINCREMENT',
'NOT NULL',),
27 'platform': (
'TEXT',
"NOT NULL DEFAULT ''", ),
28 'browser': (
'TEXT',
"NOT NULL DEFAULT ''", ),
29 'version': (
'TEXT',
"NOT NULL DEFAULT ''", ),
30 'flags': (
'INT',
'NOT NULL DEFAULT 0',),
35 _dbdisplay = [
'platform',
'browser',
'version']
44 """Keeps track of user sessions, language selection, and application
47 This class also has the ability to record every URL that the user
48 visited
and how long they stayed on that page
if
49 g.siteconfig[
'urltracking']
is enabled. It makes the system a bit
50 slower, but gives you much better analysis of what the user did
and
51 what they were interested
in.
55 'rbid': (
'BIGINT PRIMARY KEY',
'NOT NULL',),
56 'userid': (
'INT',
'NOT NULL DEFAULT 0',),
57 'useragentid': (
'INT',
'NOT NULL',),
58 'length': (
'INT',
'NOT NULL DEFAULT 0',),
59 'flags': (
'INT',
'NOT NULL DEFAULT 0',),
63 'data': (
'DICT',
"NOT NULL DEFAULT ''",),
64 'starttime': (
'DATETIME',
'NOT NULL',),
65 'endtime': (
'DATETIME',
'NOT NULL DEFAULT 0',),
66 'urls': (
'LIST',
"NOT NULL DEFAULT ''",),
71 _dbdisplay = [
'userid',
'length',
'lang',
'starttime']
84 """See if the current session has expired and creates a new one if
87 currenttime = time.time()
89 if currenttime > self.endtime.timestamp() +
IntV(g.siteconfig,
'sessiontimeout', 600, 2678400, 604800):
91 '''with open('expired.log', 'a+') as f:
92 f.write(f""">>> Session {self.rbid} expired:
93 User ID:\t{g.session.userid}
94 Current Time:\t{currenttime}
95 End Time:\t{self.endtime.timestamp()}
96 Length:\t{currenttime - self.starttime.timestamp()}
97 Session Timeout:\t{IntV(g.siteconfig, 'sessiontimeout', 600, 2678400, 604800)}\n
""")
102 """Creates a new session, closing the old session.
104 This also setups the useragent, languages, and other default settings.
106 currenttime = time.time()
110 endtime = currenttime,
111 length = currenttime - self.starttime.timestamp(),
112 flags = SESSION_EXPIRED,
123 ua = g.request.user_agent
125 useragent = g.db.Find(system_useragent,
'WHERE header = ?', ua.string)
133 platform = ua.platform
if ua.platform
else '',
134 browser = ua.browser
if ua.browser
else '',
135 version = ua.version
if ua.version
else '',
138 g.db.Insert(useragent)
141 useragent = useragent[0]
143 lang = req.accept_languages.best_match(g.siteconfig[
'languages'])
148 langcodes = list(g.request.accept_languages.values())
155 useragentid = useragent.id,
156 ip = req.environ[
'HTTP_X_FORWARDED_FOR']
if req.environ.get(
'HTTP_X_FORWARDED_FOR')
else req.environ[
'REMOTE_ADDR'],
158 langcodes = langcodes,
160 starttime = currenttime,
161 endtime = currenttime,
165 'texttheme':
'default',
177 """Convenience functions to allow the session object to be used as
178 a dict that saves settings at the end of the script.
182 return self.
data[key]
188 """Convenience functions to allow the session object to be used as
189 a dict that saves settings at the end of the script.
192 if self.
data[key] == value:
195 self.
data[key] = value
200 """Convenience functions to allow the session object to be used as
201 a dict that saves settings at the end of the script.
207 def get(self, key, defaultvalue = None):
208 """Convenience functions to allow the session object to be used as
209 a dict that saves settings at the end of the script.
211 return self.
data.
get(key, defaultvalue)
Base class for all database models.
def Set(self, **kwargs)
We use this method instead of direct attribute access in order to keep track of what values have been...
Throws an exception on blank values.
Keeps track of user sessions, language selection, and application data.
def CheckExpired(self, g)
See if the current session has expired and creates a new one if necessary.
def __delitem__(self, key)
Convenience functions to allow the session object to be used as a dict that saves settings at the end...
def New(self, g)
Creates a new session, closing the old session.
def __init__(self)
Initialize all fields at creation like a good programmer should.
def __getitem__(self, key)
Convenience functions to allow the session object to be used as a dict that saves settings at the end...
def get(self, key, defaultvalue=None)
Convenience functions to allow the session object to be used as a dict that saves settings at the end...
def __setitem__(self, key, value)
Convenience functions to allow the session object to be used as a dict that saves settings at the end...
Tracker for the various types of programs that visit the site.
def __init__(self)
Initialize all fields at creation like a good programmer should.
def IntV(d, k, min=None, max=None, default=0)
Utility function to get an int from a dict or object given its name.