2b7948c820e7d38f86bcc199c9ad9355fefb4fd0
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero 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.
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 Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 from email
.MIMEText
import MIMEText
24 from mediagoblin
import globals as mgoblin_globals
28 def _activate_testing():
30 Call this to activate testing in util.py
36 def get_jinja_env(user_template_path
=None):
38 Set up the Jinja environment, possibly allowing for user
41 (In the future we may have another system for providing theming;
42 for now this is good enough.)
44 if user_template_path
:
45 loader
= jinja2
.ChoiceLoader(
46 [jinja2
.FileSystemLoader(user_template_path
),
47 jinja2
.PackageLoader('mediagoblin', 'templates')])
49 loader
= jinja2
.PackageLoader('mediagoblin', 'templates')
51 return jinja2
.Environment(
52 loader
=loader
, autoescape
=True,
53 extensions
=['jinja2.ext.i18n'])
56 def setup_user_in_request(request
):
58 Examine a request and tack on a request.user parameter if that's
61 if not request
.session
.has_key('user_id'):
66 user
= request
.app
.db
.User
.one(
67 {'_id': mongokit
.ObjectId(request
.session
['user_id'])})
70 # Something's wrong... this user doesn't exist? Invalidate
72 request
.session
.invalidate()
77 def import_component(import_string
):
79 Import a module component defined by STRING. Probably a method,
80 class, or global variable.
83 - import_string: a string that defines what to import. Written
84 in the format of "module1.module2:component"
86 module_name
, func_name
= import_string
.split(':', 1)
87 __import__(module_name
)
88 module
= sys
.modules
[module_name
]
89 func
= getattr(module
, func_name
)
93 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94 ### Special email test stuff begins HERE
95 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97 # We have two "test inboxes" here:
101 # If you're writing test views, you'll probably want to check this.
102 # It contains a list of MIMEText messages.
104 # EMAIL_TEST_MBOX_INBOX:
105 # ----------------------
106 # This collects the messages from the FakeMhost inbox. It's reslly
107 # just here for testing the send_email method itself.
109 # Anyway this contains:
111 # - to: a list of email recipient addresses
112 # - message: not just the body, but the whole message, including
117 # Before running tests that call functions which send email, you should
118 # always call _clear_test_inboxes() to "wipe" the inboxes clean.
120 EMAIL_TEST_INBOX
= []
121 EMAIL_TEST_MBOX_INBOX
= []
124 class FakeMhost(object):
126 Just a fake mail host so we can capture and test messages
132 def sendmail(self
, from_addr
, to_addrs
, message
):
133 EMAIL_TEST_MBOX_INBOX
.append(
138 def _clear_test_inboxes():
139 global EMAIL_TEST_INBOX
140 global EMAIL_TEST_MBOX_INBOX
141 EMAIL_TEST_INBOX
= []
142 EMAIL_TEST_MBOX_INBOX
= []
144 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
145 ### </Special email test stuff>
146 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
148 def send_email(from_addr
, to_addrs
, subject
, message_body
):
150 Simple email sending wrapper, use this so we can capture messages
151 for unit testing purposes.
154 - from_addr: address you're sending the email from
155 - to_addrs: list of recipient email addresses
156 - subject: subject of the email
157 - message_body: email body text
159 # TODO: make a mock mhost if testing is enabled
160 if TESTS_ENABLED
or mgoblin_globals
.email_debug_mode
:
162 elif not mgoblin_globals
.email_debug_mode
:
163 mhost
= smtplib
.SMTP()
167 message
= MIMEText(message_body
.encode('utf-8'), 'plain', 'utf-8')
168 message
['Subject'] = subject
169 message
['From'] = from_addr
170 message
['To'] = ', '.join(to_addrs
)
173 EMAIL_TEST_INBOX
.append(message
)
175 elif mgoblin_globals
.email_debug_mode
:
176 print u
"===== Email ====="
177 print u
"From address: %s" % message
['From']
178 print u
"To addresses: %s" % message
['To']
179 print u
"Subject: %s" % message
['Subject']
181 print message
.get_payload(decode
=True)
184 return mhost
.sendmail(from_addr
, to_addrs
, message
.as_string())
192 def locale_to_lower_upper(locale
):
194 Take a locale, regardless of style, and format it like "en-us"
197 lang
, country
= locale
.split('-', 1)
198 return '%s_%s' % (lang
.lower(), country
.upper())
200 lang
, country
= locale
.split('_', 1)
201 return '%s_%s' % (lang
.lower(), country
.upper())
203 return locale
.lower()
206 def locale_to_lower_lower(locale
):
208 Take a locale, regardless of style, and format it like "en_US"
211 lang
, country
= locale
.split('_', 1)
212 return '%s-%s' % (lang
.lower(), country
.lower())
214 return locale
.lower()
217 def get_locale_from_request(request
):
219 Figure out what target language is most appropriate based on the
222 request_form
= request
.GET
or request
.POST
224 if request_form
.has_key('lang'):
225 return locale_to_lower_upper(request_form
['lang'])
227 accept_lang_matches
= request
.accept_language
.best_matches()
229 # Your routing can explicitly specify a target language
230 if request
.matchdict
.has_key('target_lang'):
231 target_lang
= request
.matchdict
['target_lang']
232 elif request
.session
.has_key('target_lang'):
233 target_lang
= request
.session
['target_lang']
234 # Pull the first acceptable language
235 elif accept_lang_matches
:
236 target_lang
= accept_lang_matches
[0]
237 # Fall back to English
241 return make_locale_lower_upper_style(target_lang
)