def get_register(): """ Displays the reg page.""" if 'user_id' in session: return redirect(url_for('home')) elif settings.get('single_user_id') is not None: return redirect(url_for('get_login')) else: return render_template('registration.html')
def home(): """ Shows the user's timeline or if no user is logged in it will redirect to the user's public timeline (their most recent posts) for public viewing. """ # Not logged in and multi-user mode. if 'user_id' not in session and not settings.get('single_user_mode'): return render_template('welcome.html') # Not logged in and single-user mode. elif 'user_id' not in session and settings.get('single_user_mode'): user_id = settings.get('single_user_id') username = settings.get_user(user_id)['username'] return redirect(url_for('get_user_profile', username=username)) # Logged in elif 'user_id' in session: user_id = session['user_id'] # Single-user mode, but no user yet. elif settings.get('single_user_mode') and settings.get('single_user_id') is None: return redirect(url_for('get_register')) # Single-user mode, user exists. else: user_id = settings.get('single_user_id') link = settings.get('registered_users').get(user_id).get('feed_location') user = User(local_url=link) posts = user.home_timeline() auth = True if 'user_id' in session else False return render_template('timeline.html', posts=posts, user=user, page_type='timeline', auth=auth)
def main(): # Check the user's pid file to see if # a crawler already exists. if not os.path.exists(PID_LOCATION): with open(PID_LOCATION, 'w') as f: f.write('working') print 'Starting...' links = fr.get_user_follows_links() links.append(fr.get_user_link()) print links MicroblogFeedCrawler(links, cache_location=SettingsManager.get('cache_location'), start_now=True)
def do_login(): """ Logs the user in. """ error = '' username = request.form['username'] password = request.form['password'] username_and_ids = { ud['username']: uid for uid, ud in settings.get('registered_users').iteritems() } if username not in username_and_ids.keys(): error = 'Invalid username' pwd_hash = settings.get_user(username_and_ids[username]).get('pwd_hash') if not check_password_hash(pwd_hash, password): error = 'Invalid password' else: session['user_id'] = username_and_ids[username] return redirect(url_for('home')) return render_template('login.html', error=error)
def user_for_username(username): for _, user_dict in settings.get('registered_users').iteritems(): if user_dict['username'] == username: return user_dict
def post_register(): """ Registers a new user. """ error = '' user_full_name = request.form['full_name'] username = request.form['username'] password = request.form['password'] password_confirm = request.form['password_confirm'] email = request.form['email'] # User is already logged in. if 'user_id' in session: return redirect(url_for('home')) # No more users can register. elif settings.get('single_user_mode') \ and len(settings.get('registered_users')) > 0: error = 'No more users can register at this time.' # Username is alredy registered. users = settings.get('registered_users') username_taken = True if \ len([uid for uid, ud, in users.iteritems() if ud.get('username') == username]) > 0 \ else False if username_taken: return redirect(url_for('get_login')) # Register the new user. if username is None: error = 'No username provided.' elif len(username) < 8 or len(username) > 25: error = 'Username is not the correct length. \ Please enter a username between 8-25 characters.' elif re.search('[^a-zA-Z0-9\_]', username) is not None: error = 'Usernames can only contain letters an numbers.' elif re.search('\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b', email) is not None: error = 'Please enter a valid email address.' elif password is None or password_confirm is None: error = 'You must fill in your password.' elif password != request.form['password_confirm']: error = 'Passwords do not match.' elif len(password) < 8 or re.search('[a-zA-Z0-9]', password) is None: error = 'Your password must be at least 8 characters long and \ must be a combination of numbers and letters. Special\ characters are allowed and encouraged.' else: new_user, feed_location, blocks_location, follows_location = User.create(username) if settings.get('single_user_mode'): settings.add('single_user_id', new_user.user_id) domain = settings.get('domain') new_user.profile = 'http://{0}/{1}'.format(domain, new_user.username) new_user.link = 'http://{0}/{1}/feed.xml'.format(domain, new_user.username) new_user.follows_url = 'http://{0}/{1}/follows.xml'.format(domain, new_user.username) new_user.blocks_url = 'http://{0}/{1}/blocks.xml'.format(domain, new_user.username) new_user.message_url = 'http://{0}/{1}/message.'.format(domain, new_user.username) new_user.language = 'en' # Update the settings. pwd_hash = generate_password_hash(password) settings.add_user(username=new_user.username, pwd_hash=pwd_hash, user_id=new_user.user_id, feed_location=feed_location, blocks_location=blocks_location, follows_location=follows_location) session['user_id'] = new_user.user_id return redirect(url_for('home')) return render_template('registration.html', error=error)
from werkzeug import check_password_hash, generate_password_hash from flask_limiter import Limiter from cachemanager import CacheManager from settingsmanager import SettingsManager as settings from model.user import User from model.status import Status from model.shared import db # Init the application app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////etc/microblog/microblog.db' limiter = Limiter(app) db.init_app(app) app.debug = True app.secret_key = settings.get('secret') CacheManager(cache_location=settings.get('cache_location')) if not app.debug: import logging from logging import FileHandler file_handler = FileHandler('error.log') file_handler.setLevel(logging.WARNING) from logging import Formatter file_handler.setFormatter(Formatter( '%(asctime)s %(levelname)s: %(message)s ' '[in %(pathname)s:%(lineno)d]')) app.logger.addHandler(file_handler) for handler in app.logger.handlers: limiter.logger.addHandler(handler)