sqlite3 with flask web application CRUD pdf

# app/models.py

from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash

from app import db, login_manager

class Employee(UserMixin, db.Model):
    """
    Create an Employee table
    """

    # Ensures table will be named in plural and not in singular
    # as is the name of the model
    __tablename__ = 'employees'

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(60), index=True, unique=True)
    username = db.Column(db.String(60), index=True, unique=True)
    first_name = db.Column(db.String(60), index=True)
    last_name = db.Column(db.String(60), index=True)
    password_hash = db.Column(db.String(128))
    department_id = db.Column(db.Integer, db.ForeignKey('departments.id'))
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
    is_admin = db.Column(db.Boolean, default=False)

    @property
    def password(self):
        """
        Prevent pasword from being accessed
        """
        raise AttributeError('password is not a readable attribute.')

    @password.setter
    def password(self, password):
        """
        Set password to a hashed password
        """
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        """
        Check if hashed password matches actual password
        """
        return check_password_hash(self.password_hash, password)

    def __repr__(self):
        return '<Employee: {}>'.format(self.username)

# Set up user_loader
@login_manager.user_loader
def load_user(user_id):
    return Employee.query.get(int(user_id))

class Department(db.Model):
    """
    Create a Department table
    """

    __tablename__ = 'departments'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(60), unique=True)
    description = db.Column(db.String(200))
    employees = db.relationship('Employee', backref='department',
                                lazy='dynamic')

    def __repr__(self):
        return '<Department: {}>'.format(self.name)

class Role(db.Model):
    """
    Create a Role table
    """

    __tablename__ = 'roles'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(60), unique=True)
    description = db.Column(db.String(200))
    employees = db.relationship('Employee', backref='role',
                                lazy='dynamic')

    def __repr__(self):
        return '<Role: {}>'.format(self.name)

3
1

                                    &lt;!-- app/templates/admin/employees/employee.html --&gt;

{% import &quot;bootstrap/wtf.html&quot; as wtf %}
{% extends &quot;base.html&quot; %}
{% block title %}Assign Employee{% endblock %}
{% block body %}
&lt;div class=&quot;content-section&quot;&gt;
 &lt;div class=&quot;outer&quot;&gt;
    &lt;div class=&quot;middle&quot;&gt;
      &lt;div class=&quot;inner&quot;&gt;
        &lt;div class=&quot;center&quot;&gt;
            &lt;h1&gt; Assign Departments and Roles &lt;/h1&gt;
            &lt;br/&gt;
            &lt;p&gt;
                Select a department and role to assign to
                &lt;span style=&quot;color: #aec251;&quot;&gt;
                    {{ employee.first_name }} {{ employee.last_name }}
                &lt;/span&gt;
            &lt;/p&gt;
            &lt;br/&gt;
            {{ wtf.quick_form(form) }}
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
{% endblock %}

3 (1 Votes)
0
3.2
5
Sheh 80 points

                                    # app/__init__.py

# existing code remains

def create_app(config_name):
    # existing code remains

    # temporary route
    @app.route('/')
    def hello_world():
        return 'Hello, World!'

    return app

3.2 (5 Votes)
0
4
6
Snooper 105 points

                                    # app/__init__.py

# after existing third-party imports
from flask_login import LoginManager

# after the db variable initialization
login_manager = LoginManager()

def create_app(config_name):
    # existing code remains

    login_manager.init_app(app)
    login_manager.login_message = &quot;You must be logged in to access this page.&quot;
    login_manager.login_view = &quot;auth.login&quot;

    return app

4 (6 Votes)
0
3.8
8
MaryW 85 points

                                    &lt;!-- app/templates/home/admin_dashboard.html --&gt;

{% extends &quot;base.html&quot; %}
{% block title %}Admin Dashboard{% endblock %}
{% block body %}
&lt;div class=&quot;intro-header&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;row&quot;&gt;
            &lt;div class=&quot;col-lg-12&quot;&gt;
                &lt;div class=&quot;intro-message&quot;&gt;
                    &lt;h1&gt;Admin Dashboard&lt;/h1&gt;
                    &lt;h3&gt;For administrators only!&lt;/h3&gt;
                    &lt;hr class=&quot;intro-divider&quot;&gt;
                    &lt;/ul&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
{% endblock %}

3.8 (5 Votes)
0
4.5
2

                                    # app/admin/forms.py

# update imports
from wtforms.ext.sqlalchemy.fields import QuerySelectField

from ..models import Department, Role

# existing code remains

class EmployeeAssignForm(FlaskForm):
    &quot;&quot;&quot;
    Form for admin to assign departments and roles to employees
    &quot;&quot;&quot;
    department = QuerySelectField(query_factory=lambda: Department.query.all(),
                                  get_label=&quot;name&quot;)
    role = QuerySelectField(query_factory=lambda: Role.query.all(),
                            get_label=&quot;name&quot;)
    submit = SubmitField('Submit')

4.5 (2 Votes)
0
0
0
BrEaK 65 points

                                    /* app/static/css/style.css */

body, html {
    width: 100%;
    height: 100%;
}

body, h1, h2, h3 {
    font-family: &quot;Lato&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;
    font-weight: 700;
}

a, .navbar-default .navbar-brand, .navbar-default .navbar-nav&gt;li&gt;a {
  color: #aec251;
}

a:hover, .navbar-default .navbar-brand:hover, .navbar-default .navbar-nav&gt;li&gt;a:hover {
  color: #687430;
}

footer {
    padding: 50px 0;
    background-color: #f8f8f8;
}

p.copyright {
    margin: 15px 0 0;
}

.alert-info {
    width: 50%;
    margin: auto;
    color: #687430;
    background-color: #e6ecca;
    border-color: #aec251;
}

.btn-default {
    border-color: #aec251;
    color: #aec251;
}

.btn-default:hover {
    background-color: #aec251;
}

.center {
    margin: auto;
    width: 50%;
    padding: 10px;
}

.content-section {
    padding: 50px 0;
    border-top: 1px solid #e7e7e7;
}

.footer, .push {
  clear: both;
  height: 4em;
}

.intro-divider {
    width: 400px;
    border-top: 1px solid #f8f8f8;
    border-bottom: 1px solid rgba(0,0,0,0.2);
}

.intro-header {
    padding-top: 50px;
    padding-bottom: 50px;
    text-align: center;
    color: #f8f8f8;
    background: url(../img/intro-bg.jpg) no-repeat center center;
    background-size: cover;
    height: 100%;
}

.intro-message {
    position: relative;
    padding-top: 20%;
    padding-bottom: 20%;
}

.intro-message &gt; h1 {
    margin: 0;
    text-shadow: 2px 2px 3px rgba(0,0,0,0.6);
    font-size: 5em;
}

.intro-message &gt; h3 {
    text-shadow: 2px 2px 3px rgba(0,0,0,0.6);
}

.lead {
    font-size: 18px;
    font-weight: 400;
}

.topnav {
    font-size: 14px;
}

.wrapper {
  min-height: 100%;
  height: auto !important;
  height: 100%;
  margin: 0 auto -4em;
}

0
0
4.13
8
Eric Vought 115 points

                                    &lt;!-- app/templates/admin/departments/department.html --&gt;

{% import &quot;bootstrap/wtf.html&quot; as wtf %}
{% extends &quot;base.html&quot; %}
{% block title %}
    {% if add_department %}
        Add Department
    {% else %}
        Edit Department
    {% endif %}
{% endblock %}
{% block body %}
&lt;div class=&quot;content-section&quot;&gt;
 &lt;div class=&quot;outer&quot;&gt;
    &lt;div class=&quot;middle&quot;&gt;
      &lt;div class=&quot;inner&quot;&gt;
        &lt;div class=&quot;center&quot;&gt;
            {% if add_department %}
                &lt;h1&gt;Add Department&lt;/h1&gt;
            {% else %}
                &lt;h1&gt;Edit Department&lt;/h1&gt;
            {% endif %}
            &lt;br/&gt;
            {{ wtf.quick_form(form) }}
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
{% endblock %}

4.13 (8 Votes)
0
4
5
Savethelife 200 points

                                    $ flask db init

4 (5 Votes)
0
3.83
6
Thanh 60 points

                                    &lt;!-- app/templates/home/dashboard.html --&gt;

{% extends &quot;base.html&quot; %}
{% block title %}Dashboard{% endblock %}
{% block body %}
&lt;div class=&quot;intro-header&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;row&quot;&gt;
            &lt;div class=&quot;col-lg-12&quot;&gt;
                &lt;div class=&quot;intro-message&quot;&gt;
                    &lt;h1&gt;The Dashboard&lt;/h1&gt;
                    &lt;h3&gt;We made it here!&lt;/h3&gt;
                    &lt;hr class=&quot;intro-divider&quot;&gt;
                    &lt;/ul&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
{% endblock %}

3.83 (6 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source