blob: 37ffe9b281d6c4d0c479d5ffc73ade62438c322f [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053017import webnotes
18from webnotes.utils import cint
Rushabh Mehta63d669f2012-02-03 12:56:12 +053019import home
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053020
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053021
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053022def on_login_post_session(login_manager):
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053023 """
24 called after login
25 update login_from and delete parallel sessions
26 """
Anand Doshib63a0072012-01-03 16:11:04 +053027 # Clear previous sessions i.e. logout previous log-in attempts
Rushabh Mehtad31bf622012-01-19 15:09:49 +053028 exception_list = ['demo@webnotestech.com', 'Administrator', 'Guest']
Anand Doshi9fdee382012-01-03 11:26:00 +053029 if webnotes.session['user'] not in exception_list:
Anand Doshib63a0072012-01-03 16:11:04 +053030 sid_list = webnotes.conn.sql("""
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053031 DELETE FROM `tabSessions`
Anand Doshib63a0072012-01-03 16:11:04 +053032 WHERE
33 user=%s AND
Rushabh Mehtabedc1fe2012-01-17 18:17:06 +053034 sid!=%s""", \
Anand Doshib63a0072012-01-03 16:11:04 +053035 (webnotes.session['user'], webnotes.session['sid']), as_list=1)
Rushabh Mehta49ebfb62012-01-20 15:32:18 +053036
Anand Doshi1ed4ef12012-04-27 15:30:23 +053037 # check if account is expired
38 check_if_expired()
39
40 if webnotes.session['user'] not in ('Guest', 'demo@webnotestech.com'):
Rushabh Mehta63d669f2012-02-03 12:56:12 +053041 # create feed
42 from webnotes.utils import nowtime
43 home.make_feed('Login', 'Profile', login_manager.user, login_manager.user,
44 '%s logged in at %s' % (login_manager.user_fullname, nowtime()),
45 login_manager.user=='Administrator' and '#8CA2B3' or '#1B750D')
46
Rushabh Mehta533bc772012-02-15 09:35:42 +010047
Rushabh Mehta63d669f2012-02-03 12:56:12 +053048def comment_added(doc):
49 """add comment to feed"""
Rushabh Mehta63d669f2012-02-03 12:56:12 +053050 home.make_feed('Comment', doc.comment_doctype, doc.comment_docname, doc.comment_by,
51 '<i>"' + doc.comment + '"</i>', '#6B24B3')
52
Rushabh Mehta49ebfb62012-01-20 15:32:18 +053053def doclist_all(doc, method):
54 """doclist trigger called from webnotes.model.doclist on any event"""
Rushabh Mehta865c00a2012-01-24 14:33:21 +053055 home.update_feed(doc, method)
56
57def boot_session(bootinfo):
58 """boot session - send website info if guest"""
59 import webnotes
60 import webnotes.model.doc
61
Rushabh Mehtac5471dd2012-02-22 12:07:42 +053062 bootinfo['custom_css'] = webnotes.conn.get_value('Style Settings', None, 'custom_css') or ''
63
Rushabh Mehta865c00a2012-01-24 14:33:21 +053064 if webnotes.session['user']=='Guest':
Rushabh Mehta7018b192012-02-02 13:42:28 +053065 bootinfo['website_settings'] = webnotes.model.doc.getsingle('Website Settings')
Rushabh Mehtaf35992f2012-02-07 10:39:17 +053066 bootinfo['website_menus'] = webnotes.conn.sql("""select label, url, custom_page,
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053067 parent_label, parentfield
Rushabh Mehta7018b192012-02-02 13:42:28 +053068 from `tabTop Bar Item` where parent='Website Settings' order by idx asc""", as_dict=1)
Rushabh Mehta389880d2012-04-27 18:39:14 +053069 bootinfo['startup_code'] = \
70 webnotes.conn.get_value('Website Settings', None, 'startup_code')
Rushabh Mehta865c00a2012-01-24 14:33:21 +053071 else:
72 bootinfo['letter_heads'] = get_letter_heads()
73
Rushabh Mehtaaa999df2012-02-27 12:54:04 +053074 import webnotes.model.doctype
75 bootinfo['docs'] += webnotes.model.doctype.get('Event')
Anand Doshi92da8892012-03-15 11:50:26 +053076 bootinfo['docs'] += webnotes.model.doctype.get('Search Criteria')
Rushabh Mehta17da7642012-02-28 18:56:56 +053077
78 bootinfo['modules_list'] = webnotes.conn.get_global('modules_list')
Rushabh Mehta12852e72012-02-29 15:11:06 +053079
80 # if no company, show a dialog box to create a new company
81 bootinfo['setup_complete'] = webnotes.conn.sql("""select name from
82 tabCompany limit 1""") and 'Yes' or 'No'
Rushabh Mehta412a65c2012-02-29 18:38:18 +053083
84 bootinfo['user_background'] = webnotes.conn.get_value("Profile", webnotes.session['user'], 'background_image') or ''
Anand Doshi1ed4ef12012-04-27 15:30:23 +053085
86 # load subscription info
87 import conf
88 if hasattr(conf, 'max_users'): bootinfo['max_users'] = conf.max_users
89 if hasattr(conf, 'expires_on'): bootinfo['expires_on'] = conf.expires_on
Rushabh Mehtaaa999df2012-02-27 12:54:04 +053090
Rushabh Mehtacabfc652012-04-14 16:08:45 +053091
Rushabh Mehta865c00a2012-01-24 14:33:21 +053092def get_letter_heads():
93 """load letter heads with startup"""
94 import webnotes
Rushabh Mehta63d669f2012-02-03 12:56:12 +053095 ret = webnotes.conn.sql("""select name, content from `tabLetter Head`
96 where ifnull(disabled,0)=0""")
Rushabh Mehta4b1fe052012-01-25 15:06:28 +053097 return dict(ret)
Anand Doshi1ed4ef12012-04-27 15:30:23 +053098
Anand Doshi737c2f92012-02-14 16:16:13 +053099
Anand Doshi1ed4ef12012-04-27 15:30:23 +0530100def check_if_expired():
101 """check if account is expired. If expired, do not allow login"""
102 import conf
103 # check if expires_on is specified
104 if not hasattr(conf, 'expires_on'): return
105
106 # check if expired
107 from datetime import datetime, date
108 expires_on = datetime.strptime(conf.expires_on, '%Y-%m-%d').date()
109 if date.today() <= expires_on: return
110
111 # if expired, stop user from logging in
112 from webnotes.utils import formatdate
113 if 'System Manager' in webnotes.user.roles:
114 webnotes.response['server_messages'] = """Oops! \
115 Your subscription expired on <b>%s</b>.
116
117 Nothing catastrophic.
118
119 Just drop in a mail at <b>support@erpnext.com</b> and \
120 we will guide you to get your account re-activated.""" % formatdate(conf.expires_on)
121 else:
122 webnotes.response['server_messages'] = """Oops! \
123 Your subscription expired on <b>%s</b>.
124
125 Nothing catastrophic.
126
127 Just ask your System Manager to drop in a mail at <b>support@erpnext.com</b> and \
128 we will guide him to get your account re-activated.""" % formatdate(conf.expires_on)
129
130 webnotes.response['message'] = 'Account Expired'
Rushabh Mehta30cb5c22012-04-27 18:39:56 +0530131 raise webnotes.AuthenticationError