blob: 9cd1af94d43ab227c45a94b9fc8e3f8f6141620c [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
Anand Doshi486f9df2012-07-19 13:40:31 +053017from __future__ import unicode_literals
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053018import webnotes
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
Nabin Hait8bbc5a22012-11-05 18:58:15 +053028 exception_list = ['demo@erpnext.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
Nabin Hait8bbc5a22012-11-05 18:58:15 +053040 if webnotes.session['user'] not in ('Guest', 'demo@erpnext.com'):
Rushabh Mehta63d669f2012-02-03 12:56:12 +053041 # create feed
42 from webnotes.utils import nowtime
Rushabh Mehta91ba3462012-07-13 14:54:40 +053043 from webnotes.profile import get_user_fullname
Rushabh Mehta63d669f2012-02-03 12:56:12 +053044 home.make_feed('Login', 'Profile', login_manager.user, login_manager.user,
Anand Doshiaea82042012-10-15 15:30:56 +053045 '%s logged in at %s' % (get_user_fullname(login_manager.user), nowtime()),
Rushabh Mehta63d669f2012-02-03 12:56:12 +053046 login_manager.user=='Administrator' and '#8CA2B3' or '#1B750D')
47
Rushabh Mehta533bc772012-02-15 09:35:42 +010048
Rushabh Mehta63d669f2012-02-03 12:56:12 +053049def comment_added(doc):
50 """add comment to feed"""
Rushabh Mehta63d669f2012-02-03 12:56:12 +053051 home.make_feed('Comment', doc.comment_doctype, doc.comment_docname, doc.comment_by,
52 '<i>"' + doc.comment + '"</i>', '#6B24B3')
53
Rushabh Mehta49ebfb62012-01-20 15:32:18 +053054def doclist_all(doc, method):
Rushabh Mehta78d4b492012-11-23 17:53:43 +053055 """doclist trigger called from webnotes.model.wrapper on any event"""
Rushabh Mehta865c00a2012-01-24 14:33:21 +053056 home.update_feed(doc, method)
57
58def boot_session(bootinfo):
59 """boot session - send website info if guest"""
60 import webnotes
61 import webnotes.model.doc
62
Rushabh Mehtac5471dd2012-02-22 12:07:42 +053063 bootinfo['custom_css'] = webnotes.conn.get_value('Style Settings', None, 'custom_css') or ''
Rushabh Mehta30e3f152012-05-01 14:07:41 +053064 bootinfo['website_settings'] = webnotes.model.doc.getsingle('Website Settings')
Rushabh Mehtac5471dd2012-02-22 12:07:42 +053065
Rushabh Mehta865c00a2012-01-24 14:33:21 +053066 if webnotes.session['user']=='Guest':
Rushabh Mehtaf35992f2012-02-07 10:39:17 +053067 bootinfo['website_menus'] = webnotes.conn.sql("""select label, url, custom_page,
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053068 parent_label, parentfield
Rushabh Mehta7018b192012-02-02 13:42:28 +053069 from `tabTop Bar Item` where parent='Website Settings' order by idx asc""", as_dict=1)
Rushabh Mehta389880d2012-04-27 18:39:14 +053070 bootinfo['startup_code'] = \
Rushabh Mehtaa4be4d32012-04-30 14:22:02 +053071 webnotes.conn.get_value('Website Settings', None, 'startup_code')
Rushabh Mehta865c00a2012-01-24 14:33:21 +053072 else:
73 bootinfo['letter_heads'] = get_letter_heads()
74
Rushabh Mehtaaa999df2012-02-27 12:54:04 +053075 import webnotes.model.doctype
76 bootinfo['docs'] += webnotes.model.doctype.get('Event')
Anand Doshi92da8892012-03-15 11:50:26 +053077 bootinfo['docs'] += webnotes.model.doctype.get('Search Criteria')
Rushabh Mehta35c017a2012-11-30 10:57:28 +053078 bootinfo['notification_settings'] = webnotes.doc("Notification Control",
79 "Notification Control").get_values()
Rushabh Mehta17da7642012-02-28 18:56:56 +053080
81 bootinfo['modules_list'] = webnotes.conn.get_global('modules_list')
Rushabh Mehta12852e72012-02-29 15:11:06 +053082
83 # if no company, show a dialog box to create a new company
84 bootinfo['setup_complete'] = webnotes.conn.sql("""select name from
85 tabCompany limit 1""") and 'Yes' or 'No'
Anand Doshi1ed4ef12012-04-27 15:30:23 +053086
87 # load subscription info
88 import conf
Rushabh Mehta41565232012-09-17 19:10:36 +053089 for key in ['max_users', 'expires_on', 'max_space', 'status', 'developer_mode']:
Anand Doshi08c08692012-06-08 16:07:01 +053090 if hasattr(conf, key): bootinfo[key] = getattr(conf, key)
Rushabh Mehtaaa999df2012-02-27 12:54:04 +053091
Anand Doshidf74d302012-05-10 19:19:31 +053092 company = webnotes.conn.sql("select name, default_currency from `tabCompany`", as_dict=1)
93 company_dict = {}
94 for c in company:
95 company_dict.setdefault(c['name'], {}).update(c)
96
97 bootinfo['company'] = company_dict
Rushabh Mehta41565232012-09-17 19:10:36 +053098
Rushabh Mehta865c00a2012-01-24 14:33:21 +053099def get_letter_heads():
100 """load letter heads with startup"""
101 import webnotes
Rushabh Mehta63d669f2012-02-03 12:56:12 +0530102 ret = webnotes.conn.sql("""select name, content from `tabLetter Head`
103 where ifnull(disabled,0)=0""")
Rushabh Mehta4b1fe052012-01-25 15:06:28 +0530104 return dict(ret)
Anand Doshi1ed4ef12012-04-27 15:30:23 +0530105
Anand Doshi737c2f92012-02-14 16:16:13 +0530106
Anand Doshi1ed4ef12012-04-27 15:30:23 +0530107def check_if_expired():
108 """check if account is expired. If expired, do not allow login"""
109 import conf
110 # check if expires_on is specified
111 if not hasattr(conf, 'expires_on'): return
112
113 # check if expired
114 from datetime import datetime, date
115 expires_on = datetime.strptime(conf.expires_on, '%Y-%m-%d').date()
116 if date.today() <= expires_on: return
117
118 # if expired, stop user from logging in
119 from webnotes.utils import formatdate
Anand Doshidcb6c2c2012-09-28 16:09:49 +0530120 msg = """Oops! Your subscription expired on <b>%s</b>.<br>""" % formatdate(conf.expires_on)
Anand Doshi62091db2012-09-28 16:06:06 +0530121
Anand Doshi1ed4ef12012-04-27 15:30:23 +0530122 if 'System Manager' in webnotes.user.roles:
Anand Doshi62091db2012-09-28 16:06:06 +0530123 msg += """Just drop in a mail at <b>support@erpnext.com</b> and
124 we will guide you to get your account re-activated."""
Anand Doshi1ed4ef12012-04-27 15:30:23 +0530125 else:
Anand Doshi62091db2012-09-28 16:06:06 +0530126 msg += """Just ask your System Manager to drop in a mail at <b>support@erpnext.com</b> and
127 we will guide him to get your account re-activated."""
128
129 webnotes.msgprint(msg)
Anand Doshi1ed4ef12012-04-27 15:30:23 +0530130
131 webnotes.response['message'] = 'Account Expired'
Rushabh Mehta30cb5c22012-04-27 18:39:56 +0530132 raise webnotes.AuthenticationError
Rushabh Mehtaa9209432012-05-07 18:00:57 +0530133
134#### website
135
136def get_web_script():
137 """returns web startup script"""
138 return webnotes.conn.get_value('Website Settings', None, 'startup_code') or ''
139
140def get_web_style():
141 """returns web css"""
142 return webnotes.conn.get_value('Style Settings', None, 'custom_css') or ''
143
Rushabh Mehta458ca8e2012-05-09 10:17:08 +0530144def get_web_header(page_name):
Rushabh Mehtaa9209432012-05-07 18:00:57 +0530145 """get website header"""
146 from website.utils import get_header
Rushabh Mehta458ca8e2012-05-09 10:17:08 +0530147 return get_header(page_name)
Rushabh Mehtaa9209432012-05-07 18:00:57 +0530148
Rushabh Mehta458ca8e2012-05-09 10:17:08 +0530149def get_web_footer(page_name):
Rushabh Mehtaa9209432012-05-07 18:00:57 +0530150 """get website footer"""
151 from website.utils import get_footer
Rushabh Mehta458ca8e2012-05-09 10:17:08 +0530152 return get_footer(page_name)