Skip to content

Commit e075da8

Browse files
Merge pull request #9 from Blankphrase/adiela
Adiela
2 parents b6e1b9a + 9d2e876 commit e075da8

24 files changed

+436
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1+
.DS_Store
12
virtual/
3+
*.pyc
4+
.vscode/
5+
venv/
6+
.idea/
7+
config.py
8+
migrations/
9+
start.sh
10+
11+

app/__init__.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from flask import Flask
2+
from flask_bootstrap import Bootstrap
3+
from config import config_options
4+
from flask_sqlalchemy import SQLAlchemy
5+
from flask_login import LoginManager
6+
7+
8+
9+
# Instances of flask extensions
10+
# Instance of LoginManger and using its methods
11+
login_manager = LoginManager()
12+
login_manager.session_protection = 'strong'
13+
login_manager.login_view = 'auth.login'
14+
bootstrap = Bootstrap()
15+
db = SQLAlchemy()
16+
17+
def create_app(config_name):
18+
'''
19+
Function that takes configuration setting key as an argument
20+
21+
Args:
22+
config_name : name of the configuration to be used
23+
'''
24+
25+
# Initialising application
26+
app = Flask(__name__)
27+
28+
# Creating the app configurations
29+
app.config.from_object(config_options[config_name])
30+
config_options[config_name].init_app(app)
31+
32+
33+
# Initialising flask extensions
34+
bootstrap.init_app(app)
35+
db.init_app(app)
36+
login_manager.init_app(app)
37+
38+
# Regestering the main blueprint
39+
from .main import main as main_blueprint
40+
app.register_blueprint(main_blueprint)
41+
42+
# Regestering the auth bluprint
43+
from .auth import auth as auth_blueprint
44+
app.register_blueprint(auth_blueprint, url_prefix='/auth')
45+
46+
# # Setting config when using an API
47+
# from .requests import configure_request
48+
# configure_request(app)
49+
50+
return app

app/auth/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from flask import Blueprint
2+
auth = Blueprint('auth', __name__)
3+
from . import views,forms

app/auth/forms.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from flask_wtf import FlaskForm
2+
from wtforms import StringField,PasswordField,SubmitField, BooleanField
3+
from wtforms.validators import Required,Email,EqualTo
4+
from ..models import User
5+
from wtforms import ValidationError
6+
7+
8+
class RegistrationForm(FlaskForm):
9+
10+
email = StringField('Your Email Address',validators=[Required(),Email()])
11+
username = StringField('Enter your username',validators = [Required()])
12+
password = PasswordField('Password',validators = [Required(),
13+
EqualTo('password_confirm',message = 'Passwords must match')])
14+
password_confirm = PasswordField('Confirm Passwords',validators = [Required()])
15+
submit = SubmitField('Sign Up')
16+
17+
def validate_email(self,data_field):
18+
if User.query.filter_by(email =data_field.data).first():
19+
raise ValidationError('There is an account with that email')
20+
21+
def validate_username(self,data_field):
22+
if User.query.filter_by(username = data_field.data).first():
23+
raise ValidationError('That username is taken')
24+
25+
26+
class LoginForm(FlaskForm):
27+
email = StringField('Your Email Address',validators=[Required(),Email()])
28+
password = PasswordField('Password',validators =[Required()])
29+
remember = BooleanField('Remember me')
30+
submit = SubmitField('Sign In')

app/auth/views.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from flask import render_template, redirect, url_for, flash, request
2+
from . import auth
3+
from ..models import User
4+
from .forms import RegistrationForm, LoginForm
5+
from .. import db
6+
from flask_login import login_user, login_required, logout_user
7+
8+
#....
9+
@auth.route('/login',methods=['GET','POST'])
10+
def login():
11+
login_form = LoginForm()
12+
if login_form.validate_on_submit():
13+
user = User.query.filter_by(email = login_form.email.data).first()
14+
if user is not None and user.verify_password(login_form.password.data):
15+
login_user(user,login_form.remember.data)
16+
return redirect(request.args.get('next') or url_for('main.index'))
17+
18+
flash('Invalid username or Password')
19+
20+
title = "Login House Hounting "
21+
return render_template('auth/login.html',login_form = login_form,title=title)
22+
#....
23+
24+
25+
26+
@auth.route('/register',methods = ["GET","POST"])
27+
def register():
28+
form = RegistrationForm()
29+
if form.validate_on_submit():
30+
user = User(email = form.email.data, username = form.username.data,password = form.password.data)
31+
db.session.add(user)
32+
db.session.commit()
33+
return redirect(url_for('auth.login'))
34+
title = "New Account"
35+
return render_template('auth/register.html',registration_form = form)
36+
37+
38+
@auth.route('/logout')
39+
@login_required
40+
def logout():
41+
logout_user()
42+
return redirect(url_for("main.index"))

app/main/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from flask import Blueprint
2+
main = Blueprint('main',__name__)
3+
from . import views,errors

app/main/errors.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask import render_template
2+
from . import main
3+
4+
# Error handler decorator
5+
@main.app_errorhandler(404)
6+
def four_Ow_four(error):
7+
'''
8+
Function to render the 404 error page
9+
'''
10+
title = '404 page'
11+
return render_template('fourOwfour.html', title=title),404

app/main/forms.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from flask_wtf import FlaskForm
2+
from wtforms import StringField, SubmitField, MultipleFileField, SelectField, IntegerField, DateField, TimeField, TextAreaField
3+
from wtforms.validators import Required
4+
from ..models import User
5+
from wtforms import ValidationError
6+
7+
8+
class ListingForm(FlaskForm):
9+
images = MultipleFileField('Upload a few images',validators=[FileAllowed(['jpg', 'png']), Required])
10+
location = SelectField('Neighbourhood', choices=[('eastleigh', 'Eastleigh'), ('karen', 'Karen'),
11+
('kileleshwa', 'Kileleshwa'), ('Langata', 'Langata'),
12+
('lavington', 'Lavington'), ('muthaiga', 'Muthaiga'),
13+
('ngara', 'Ngara'), ('runda', 'Runda'), ('donholm', 'Donholm'),
14+
('south-B', 'South-B'), ('south-C', 'South-C'),
15+
('upperhill', 'Upperhill'), ('westlands', 'Westlands')])
16+
category = SelectField('Type', validators=[Required], choices=[('apartment', 'Apartment'),
17+
('bungalow', 'Bungalow'),
18+
('maisonette', 'Maisonette')])
19+
bedrooms = SelectField('Size', validators=[Required], choices=[('bedsitter', 'Bedsitter'), ('1 Bedroom', '1 Bedroom'),
20+
('2 Bedroom', '2 Bedroom'), ('3 Bedroom', '3 Bedroom'),
21+
('4 Bedroom', '4 Bedroom')])
22+
pricing = IntegerField('Price', validators=[Required])
23+
description = TextAreaField('Description', validators=[Required])
24+
view_date = DateField('Date', validators=[Required])
25+
view_start_time = TimeField('From', min="9:00", max="18:00", validators=[Required])
26+
view_end_time = TimeField('To', validators=[Required])
27+
submit = SubmitField('Submit')

app/main/views.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from flask import render_template,request,redirect,url_for,abort
2+
from . import main
3+
4+
# Views
5+
@main.route('/')
6+
def index():
7+
8+
'''
9+
View root page function that returns the index page and its data
10+
'''
11+
12+
title = 'Home'
13+
14+
return render_template('index.html', title = title )
15+
16+

app/models.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# File for models/classes
2+
from . import db
3+
from . import login_manager
4+
from flask_login import UserMixin, current_user
5+
from werkzeug.security import generate_password_hash,check_password_hash
6+
7+
8+
9+
@login_manager.user_loader
10+
def load_user(user_id):
11+
return User.query.get(int(user_id))
12+
13+
14+
15+
class User(UserMixin,db.Model):
16+
17+
__tablename__ = 'users'
18+
id = db.Column(db.Integer, primary_key = True)
19+
username = db.Column(db.String(255))
20+
email = db.Column(db.String(255),unique = True,index = True)
21+
pass_secure = db.Column(db.String(255))
22+
23+
@property
24+
def password(self):
25+
raise AttributeError('You cannot read the password attribute')
26+
27+
@password.setter
28+
def password(self, password):
29+
self.pass_secure = generate_password_hash(password)
30+
31+
def verify_password(self,password):
32+
return check_password_hash(self.pass_secure,password)
33+
34+
def __repr__(self):
35+
return f'{self.username}'
36+
37+
38+
class House(db.Model):
39+
40+
__tablename__ = 'houses'
41+
id = db.Column(db.Integer, primary_key = True)
42+
43+

0 commit comments

Comments
 (0)