Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

# Copyright (C) 2015 Chintalagiri Shashank 

# 

# This file is part of Tendril. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU Affero General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU Affero General Public License for more details. 

# 

# You should have received a copy of the GNU Affero General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

 

 

import logging 

from logging.handlers import SMTPHandler 

from urllib2 import quote, unquote 

 

from flask_mail import Mail 

from flask_user import UserManager, SQLAlchemyAdapter 

 

 

def init_app(app, db): 

    """ 

    Initialize Flask applicaton 

    """ 

    # Initialize app config settings 

    app.config.from_object('tendril.frontend.startup.settings') 

    if app.testing: 

        # Disable CSRF checks while testing 

        app.config['WTF_CSRF_ENABLED'] = False 

 

    # Initialize Assets 

    from tendril.frontend.startup import assets  # noqa 

 

    # Create Filters 

 

    def unicode_filter(s): 

        return unicode(s, 'utf-8') 

    app.jinja_env.filters['unicode'] = unicode_filter 

 

    def quote_filter(s): 

        return quote(s) 

    app.jinja_env.filters['quote'] = quote_filter 

 

    def unquote_filter(s): 

        return unquote(s) 

    app.jinja_env.filters['unquote'] = unquote_filter 

 

    def strip(s): 

        return s.strip() 

    app.jinja_env.filters['strip'] = strip 

 

    # Setup Flask-Mail 

    mail = Mail(app)  # noqa 

 

    # Setup an error-logger to send emails to app.config.ADMINS 

    init_error_logger_with_email_handler(app) 

 

    # Setup Flask-User to handle user account related forms 

    from tendril.frontend.users.models import UserAuth, User 

    from tendril.frontend.users.forms import MyRegisterForm 

    from tendril.frontend.users.views import user_profile_page 

    db_adapter = SQLAlchemyAdapter(db, User, 

                                   UserAuthClass=UserAuth) 

    user_manager = UserManager(db_adapter, app,  # noqa 

                               register_form=MyRegisterForm, 

                               user_profile_view_function=user_profile_page, 

                               ) 

 

    # Load all models.py files to register db.Models with SQLAlchemy 

    from tendril.frontend.users import models  # noqa 

 

    # Load all views.py files to register @app.routes() with Flask 

    from tendril.frontend.pages import views  # noqa 

    from tendril.frontend.users import views  # noqa 

 

    # Configure send_file 

    from tendril.utils.config import USE_X_SENDFILE 

    app.use_x_sendfile = USE_X_SENDFILE 

 

    # Register blueprints 

    from tendril.frontend.blueprints.expose import expose 

    app.register_blueprint(expose, url_prefix='/expose') 

 

    from tendril.frontend.blueprints.doc import doc 

    app.register_blueprint(doc, url_prefix='/doc') 

 

    from tendril.frontend.blueprints.gsymlib import gsymlib 

    app.register_blueprint(gsymlib, url_prefix='/gsymlib') 

 

    from tendril.frontend.blueprints.entityhub import entityhub 

    app.register_blueprint(entityhub, url_prefix='/entityhub') 

 

    from tendril.frontend.blueprints.conventions import conventions 

    app.register_blueprint(conventions, url_prefix='/conventions') 

 

    from tendril.frontend.blueprints.customs import customs 

    app.register_blueprint(customs, url_prefix='/sourcing/customs') 

 

    from tendril.frontend.blueprints.vendors import vendors 

    app.register_blueprint(vendors, url_prefix='/sourcing/vendors') 

 

    from tendril.frontend.blueprints.testing import testing 

    app.register_blueprint(testing, url_prefix='/testing') 

 

    from tendril.frontend.blueprints.production import production 

    app.register_blueprint(production, url_prefix='/production') 

 

    from tendril.frontend.blueprints.indent import indent 

    app.register_blueprint(indent, url_prefix='/inventory/indent') 

 

    from tendril.frontend.blueprints.inventory import inventory 

    app.register_blueprint(inventory, url_prefix='/inventory/location') 

 

    return app 

 

 

def init_error_logger_with_email_handler(app): 

    """ 

    Initialize a logger to send emails on error-level messages. 

    Unhandled exceptions will now send an email message to app.config.ADMINS. 

    """ 

    if app.debug: 

        # Do not send error emails while developing 

        return 

 

    # Retrieve email settings from app.config 

    host = app.config['MAIL_SERVER'] 

    port = app.config['MAIL_PORT'] 

    from_addr = app.config['MAIL_DEFAULT_SENDER'] 

    username = app.config['MAIL_USERNAME'] 

    password = app.config['MAIL_PASSWORD'] 

    secure = () if app.config.get('MAIL_USE_TLS') else None 

 

    # Retrieve app settings from app.config 

    to_addr_list = app.config['ADMINS'] 

    subject = app.config.get('APP_SYSTEM_ERROR_SUBJECT_LINE', 'System Error') 

 

    # Setup an SMTP mail handler for error-level messages 

    mail_handler = SMTPHandler( 

        mailhost=(host, port),                  # Mail host and port 

        fromaddr=from_addr,                     # From address 

        toaddrs=to_addr_list,                   # To address 

        subject=subject,                        # Subject line 

        credentials=(username, password),       # Credentials 

        secure=secure, 

    ) 

    mail_handler.setLevel(logging.ERROR) 

    app.logger.addHandler(mail_handler) 

 

    # Log errors using: app.logger.error('Some error message')