Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. from flask import Flask, render_template, request
  2. import psycopg2
  3. import traceback
  4.  
  5. app = Flask(__name__)
  6. app.url_map.strict_slashes = False
  7. #login_manager = LoginManager()
  8. #login_manager.init_app(app)
  9.  
  10. try:
  11.     conn = psycopg2.connect(dbname='listings', user='matthew', host='103.16.128.240', password='zx34ghnm912')
  12.     print("im connected")
  13. except psycopg2.Error as e:
  14.     print("I am unable to connect to the database")
  15.     print(e)
  16.     print(e.pgcode)
  17.     print(e.pgerror)
  18.     print(traceback.format_exc())
  19.  
  20. cursor = conn.cursor()
  21. templates = {"hairdresser":"demo-barber.html", "restaurants":"demo-restaurant.html", "engineering":"demo-engineering.html", "Accommodation":"accommodation.html"}
  22.  
  23. @app.route('/<url_name>', subdomain="demo")
  24. def choose_template(url_name):
  25.     business_data = get_business_data(url_name, cursor)
  26.     template = templates[business_data['type']]
  27.     print(business_data['logo'])
  28.     return render_template(template,  business_name=business_data['business_name'], email=business_data['email'], phone=business_data['phone'], location=business_data['location'], logo=business_data['logo'], url=url_name)
  29.  
  30. def get_business_data(url_name, cursor):
  31.     conn = psycopg2.connect(dbname='listings', user='matthew', host='103.16.128.240', password='zx34ghnm912')
  32.     cursor = conn.cursor()
  33.     business_data = {}
  34.     cursor.execute("SELECT * FROM listings WHERE url=%s;", (url_name,))
  35.     fetched_listings = cursor.fetchone()
  36.     business_data['business_name'] = fetched_listings[0]
  37.     business_data['email'] = fetched_listings[2]
  38.     business_data['phone'] = fetched_listings[3]
  39.     business_data['location'] = fetched_listings[4]
  40.     business_data['type'] = fetched_listings[5]
  41.     business_data['logo'] = fetched_listings[6]
  42.     business_data['url'] = fetched_listings[7]
  43.  
  44.     return business_data
  45.  
  46. @app.route('/<url_name>/<subpage>', subdomain="demo")
  47. def render_subpage(url_name, subpage):
  48.     business_data = get_business_data(url_name, cursor)
  49.     subpage_file = "%s.html" % subpage
  50.     return render_template(subpage_file,  business_name=business_data['business_name'], email=business_data['email'], phone=business_data['phone'], location=business_data['location'], logo=business_data['logo'], url=url_name)
  51.    
  52.  
  53.  
  54. #@login_manager.user_loader
  55. #def load_user(user_id):
  56. #    return User.get(user_id)
  57.  
  58. #@app.route("/", subdomain="admin")
  59. #def login():
  60. #    # Here we use a class of some kind to represent and validate our
  61. #    # client-side form data. For example, WTForms is a library that will
  62. #    # handle this for us, and we use a custom LoginForm to validate.
  63. #    form = LoginForm()
  64. #    if form.validate_on_submit():
  65. #        # Login and validate the user.
  66. #        # user should be an instance of your `User` class
  67. #        login_user(user)
  68. #        
  69. #        flask.flash('Logged in successfully.')
  70. #        
  71. #        next = flask.request.args.get('next')
  72. #        # is_safe_url should check if the url is safe for redirects.
  73. #        # See http://flask.pocoo.org/snippets/62/ for an example.
  74. #        if not is_safe_url(next):
  75. #            return flask.abort(400)
  76. #        
  77. #        return flask.redirect(next or flask.url_for('index'))
  78. #    return flask.render_template('admin.html', form=form)
  79.  
  80.  
  81. if __name__ == "__main__":
  82.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement