Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions back-end-python-week/flaskr/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
graft flaskr/templates
graft flaskr/static
include flaskr/schema.sql
10 changes: 10 additions & 0 deletions back-end-python-week/flaskr/flaskr.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Metadata-Version: 1.0
Name: flaskr
Version: 0.0.0
Summary: UNKNOWN
Home-page: UNKNOWN
Author: UNKNOWN
Author-email: UNKNOWN
License: UNKNOWN
Description: UNKNOWN
Platform: UNKNOWN
15 changes: 15 additions & 0 deletions back-end-python-week/flaskr/flaskr.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
MANIFEST.in
setup.cfg
setup.py
flaskr/__init__.py
flaskr/flaskr.py
flaskr/schema.sql
flaskr.egg-info/PKG-INFO
flaskr.egg-info/SOURCES.txt
flaskr.egg-info/dependency_links.txt
flaskr.egg-info/top_level.txt
flaskr/static/.style.css.swp
flaskr/static/style.css
flaskr/templates/layout.html
flaskr/templates/login.html
flaskr/templates/show_notes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions back-end-python-week/flaskr/flaskr.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flaskr
1 change: 1 addition & 0 deletions back-end-python-week/flaskr/flaskr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .flaskr import app
Binary file added back-end-python-week/flaskr/flaskr/__init__.pyc
Binary file not shown.
Binary file added back-end-python-week/flaskr/flaskr/flaskr.db
Binary file not shown.
123 changes: 123 additions & 0 deletions back-end-python-week/flaskr/flaskr/flaskr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import os
import sqlite3
import unittest

from flask import (Flask, request, session, g, redirect, url_for, abort, render_template, flash)

app = Flask(__name__) # Start the instance
app.config.from_object(__name__) # Load config

# Load in default
app.config.update(
DATABASE = os.path.join(app.root_path, "flaskr.db"),
SECRET_KEY = '_5#y2L"F4Q8z\n\xec/',
USERNAME = "Admin",
PASSWORD = "Default"
)
app.config.from_envvar("FLASR_SETTINGS", silent=True)

# Connect to the database
def connect_db():
""" Connect to the databse """
rv = sqlite3.connect(app.config['DATABASE'])
rv.row_factory = sqlite3.Row
return rv

# Prepare db for reading
def init_db():
db = get_db()

with app.open_resource("schema.sql",mode='r') as f:
db.cursor().executescript(f.read())

db.commit()

# Confirm db init
@app.cli.command("initdb")
def initdb_command():
""" Initializes the database """
init_db()
print("Initialized the databse")

# Grab that database
def get_db():
""" Opens a new database connectionn if
none exists for current app context"""
if not hasattr(g, "sqlite_db"):
g.sqlite_db = connect_db()
return g.sqlite_db

# Handle the end of a request
@app.teardown_appcontext
def close_db(error):
""" Closes the database once a request finishes """
if hasattr(g, "sqlite_db"):
g.sqlite_db.close()

# Route for "home"
@app.route('/')
@app.route('/api')
def show_notes():
db = get_db()
cur = db.execute('select title, text from notes order by id desc')
notes = cur.fetchall()
return render_template("show_notes.html", notes=notes)

# Route for adding notes
@app.route('/api/add', methods=['POST'])
def add_note():
if not session.get('logged_in'):
abort(401)
db = get_db()
db.execute('INSERT INTO notes (title, text) VALUES (?, ?)',
[request.form['title'], request.form['text']])
db.commit()
flash("Note successfully")
error = "lel"
return redirect(url_for('show_notes'))

# Route for delete
@app.route('/api/delete/<int:id>', methods=['DELETE'])
def delete_note(id):
if not session.get('logged_in'):
abort(401)
db = get_db()
db.execute('DELETE FROM notes WHERE id = ?',request.form['id'])
db.commit()
return redirect(url_for("show_notes.html",notes=notes))

# Login
@app.route('/api/login', methods=['GET','POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME']:
error = "Bad Username"
elif request.form['password'] != app.config['PASSWORD']:
error = "Bad Password"
else:
session['logged_in'] = True
flash("Successfully Logged In")
return redirect(url_for('show_notes'))
return render_template('login.html', error=error)

# Logout
@app.route('/api/logout')
def logout():
session.pop('logged_in', None)
flash("Logged Out")
return redirect(url_for('show_notes'))

# Display Profiles
@app.route('/api/<username>')
def show_profile(username):
flash("User Profile")
return redirect(url_for('show_notes'))

# Test
@app.route('/api/test')
def show_test():
flash("TEST")
return "<h1> TEST </h1>"


Binary file added back-end-python-week/flaskr/flaskr/flaskr.pyc
Binary file not shown.
6 changes: 6 additions & 0 deletions back-end-python-week/flaskr/flaskr/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
drop table if exists notes;
create table notes(
id integer primary key autoincrement,
title text not null,
'text' text not null
);
45 changes: 45 additions & 0 deletions back-end-python-week/flaskr/flaskr/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
body { font-family: sans-serif; background: #eee;}
a,h1,h2 { color: #377ba8; }
h1, h2 { font-family: "Georgia", serif; margin: 0; }
h1 { border-bottom: 2px solid #eee; }
h2 { font-size: 1.2em; }

.page { margin: 2em auto; width 35em; border: 5px solid #ccc;
padding: 0.8em; background: lightgrey; }

.notes {
display:flex;
flex-direction:row;
flex-wrap:wrap;
justify-content:space-evenly;
list-style: none;
margin: 0;
padding: 0;
}
.notes li {
margin: 0.8em 1.2em;
border:5px solid darkgrey;
max-width:550px;
padding:10px;
}

.notes button {
height:30px;
background:darkgrey;
color:light-grey;
}

.notes button:hover {
color:white;
}

.notes-body { border:5px solid #ccc; }

.add-note { font-size: 0.9em; border-bottom: 1px solid #ccc; }
.add-note dl { font-weight: bold; }
.metanav { text-align: right; font-size: 0.8em; padding: 0.3em;
margin-bottom: 1em; background: #fafafa; }
.flash { background: #cee5F5; padding: 0.5em;
border: 1px solid #aacbe2; }
.error { background: #f0d6d6; padding: 0.5em; }

19 changes: 19 additions & 0 deletions back-end-python-week/flaskr/flaskr/templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<title> Lambda Notes.py </title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">

<div class=page>
<h1> Lambda Notes.py </h1>
<div class=metanav>
{% if not session.logged_in %}
<a href="{{ url_for('login') }}">Log In</a>
{% else %}
<a href="{{ url_for('logout') }}">Log Out</a>
{% endif %}
</div>

{% for message in get_flashed_messages() %}
</div class=flash>{{ message }}</div>
{% endfor %}
{% block body %}{% endblock %}
</div>
15 changes: 15 additions & 0 deletions back-end-python-week/flaskr/flaskr/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "layout.html" %}
{% block body %}
<h2>Login</h2>
{% if error %}<p class=error><strong>Error:</strong>{{ error}}{% endif %}
<form action="{{ url_for('login') }}" method=post>
<dl>
<dt>Username:
<dd><input type=text name=username>
<dt>Password:
<dd><input type=password name=password>
<dd><input type=submit value=Login>
</dl>
</form>
{% endblock %}

23 changes: 23 additions & 0 deletions back-end-python-week/flaskr/flaskr/templates/show_notes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends "layout.html" %}
{% block body %}
{% if session.logged_in %}
<form action="{{ url_for('add_note') }}" method=post class=add-entry>
<dl>
<dt>Title:
<dd><input type=text size=30 name=title>
<dt>Text:
<dd><textarea name=text rows=5 cols=40></textarea>
<dd><input type=submit value=Share>
</dl>
</form>
{% endif %}
<div class="notes-body">
<ul class=notes>
{% for note in notes %}
<li><button>Edit</button><h2>{{ note.title }}</h2>{{ note.text|safe }}</li>
{% else %}
<li><em>No Notes!!!</em></li>
{% endfor %}
</ul>
</div>
{% endblock %}
Empty file.
11 changes: 11 additions & 0 deletions back-end-python-week/flaskr/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from setuptools import setup

setup(
name = "flaskr",
packages = ["flaskr"],
include_package_data = True,
intall_requires = [
"flask",
],

)
23 changes: 23 additions & 0 deletions lambda-notes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
68 changes: 68 additions & 0 deletions lambda-notes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.<br>
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br>
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.<br>
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.<br>
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br>
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

### Analyzing the Bundle Size

This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size

### Making a Progressive Web App

This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

### Advanced Configuration

This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration

### Deployment

This section has moved here: https://facebook.github.io/create-react-app/docs/deployment

### `npm run build` fails to minify

This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
Loading