add repr to class models and exploring relationships
This commit is contained in:
parent
936a88f264
commit
67264b3da6
11 changed files with 155 additions and 17 deletions
Binary file not shown.
|
|
@ -11,5 +11,8 @@ class Campaign(db.Model):
|
||||||
__tablename__ = 'campaign'
|
__tablename__ = 'campaign'
|
||||||
id = Column('id', INTEGER(), primary_key=True)
|
id = Column('id', INTEGER(), primary_key=True)
|
||||||
title = Column('title', TEXT(), nullable=False)
|
title = Column('title', TEXT(), nullable=False)
|
||||||
donation_id = Column(INTEGER, ForeignKey('donation.id'))
|
donations = db.relationship('Donation', backref='campaign', lazy='dynamic')
|
||||||
#ambassadors
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return f"{self.id} - {self.title}"
|
||||||
|
# donation_id = Column(INTEGER, ForeignKey('donation.id'))
|
||||||
Binary file not shown.
|
|
@ -11,5 +11,11 @@ class Donation(db.Model):
|
||||||
id = Column('id', INTEGER(), primary_key=True, autoincrement=True)
|
id = Column('id', INTEGER(), primary_key=True, autoincrement=True)
|
||||||
currency_type = Column('currency_type', TEXT(), nullable=False)
|
currency_type = Column('currency_type', TEXT(), nullable=False)
|
||||||
amount = Column('amount', INTEGER(), nullable=False)
|
amount = Column('amount', INTEGER(), nullable=False)
|
||||||
campaign = db.relationship('Campaign', backref='donation', lazy='dynamic')
|
|
||||||
user = db.relationship('User', backref='donation', lazy='dynamic')
|
user_id = Column(INTEGER, ForeignKey('user.id'))
|
||||||
|
campaign_id = Column(INTEGER, ForeignKey('campaign.id'))
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return f"{self.id} - {self.currency_type} - {self.amount}"
|
||||||
|
# campaign = db.relationship('Campaign', backref='donation', lazy='dynamic')
|
||||||
|
# user = db.relationship('User', backref='donation', lazy='dynamic')
|
||||||
|
|
@ -13,19 +13,20 @@
|
||||||
<body>
|
<body>
|
||||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||||
<a href="{{url_for('main.homepage')}}" style="text-decoration: none; margin-right: 10px;"><p>Pilzno</p></a>
|
<a href="{{url_for('main.homepage')}}" style="text-decoration: none; margin-right: 10px;"><p>Pilzno</p></a>
|
||||||
|
<a href="{{url_for('users.testing_route')}}" style="text-decoration: none; margin-right: 10px;"><p>Test</p></a>
|
||||||
{% if current_user.is_authenticated %}
|
{% if current_user.is_authenticated %}
|
||||||
<a href="{{url_for('users.user_page', user_id=current_user.id)}}" style="text-decoration: none;"><span class="navbar-brand">User Page</span></a>
|
<a href="{{url_for('users.user_page', user_id=current_user.id)}}" style="text-decoration: none;"><span class="navbar-brand">User Page</span></a>
|
||||||
{% if current_user.user_type == 'Admin' %}
|
{% if current_user.user_type == 'Admin' %}
|
||||||
<a href="{{url_for('admin.administration')}}" style="text-decoration: none;"><span class="navbar-brand">Admin Management</span></a>
|
<a href="{{url_for('admin.administration')}}" style="text-decoration: none;"><span class="navbar-brand">Admin Management</span></a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
|
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
|
||||||
<div class="navbar-nav">
|
<div class="navbar-nav">
|
||||||
<a class="nav-item nav-link active" href="{{url_for('users.logout')}}">Logout</a>
|
<a class="nav-item nav-link active" href="{{url_for('users.logout')}}">Logout</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
<a href="{{url_for('users.login')}}" style="text-decoration: none;"><span class="navbar-brand">Login</span></a>
|
<a href="{{url_for('users.login')}}" style="text-decoration: none;"><span class="navbar-brand">Login</span></a>
|
||||||
<a href="{{url_for('users.register_user')}}" style="text-decoration: none;"><span class="navbar-brand">Register</span></a>
|
<a href="{{url_for('users.register_user')}}" style="text-decoration: none;"><span class="navbar-brand">Register</span></a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</nav>
|
</nav>
|
||||||
{% with messages = get_flashed_messages(with_categories=true) %}
|
{% with messages = get_flashed_messages(with_categories=true) %}
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
|
|
@ -14,7 +14,13 @@ class User(db.Model, UserMixin):
|
||||||
email = Column('email', TEXT(), nullable=False, unique=True)
|
email = Column('email', TEXT(), nullable=False, unique=True)
|
||||||
password = Column('password', TEXT(), nullable=False)
|
password = Column('password', TEXT(), nullable=False)
|
||||||
user_type = Column('user_type', TEXT(), nullable=False)
|
user_type = Column('user_type', TEXT(), nullable=False)
|
||||||
donation_id = Column(INTEGER, ForeignKey('donation.id'))
|
donations = db.relationship('Donation', backref='user', lazy='dynamic')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return f"{self.id} - {self.first_name} - {self.last_name}"
|
||||||
|
# donation_id = Column(INTEGER, ForeignKey('donation.id'))
|
||||||
|
|
||||||
def get_reset_token(self, expiration=600):
|
def get_reset_token(self, expiration=600):
|
||||||
reset_token = jwt.encode(
|
reset_token = jwt.encode(
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
from app import db
|
from app import db
|
||||||
from app.users import users
|
from app.users import users
|
||||||
from app.users.models import User
|
from app.users.models import User
|
||||||
|
from app.campaigns.models import Campaign
|
||||||
|
from app.main.models import Donation
|
||||||
from app.users.forms import LoginForm, RegisterUserForm#, RequestResetForm, ResetPasswordForm, EditUserForm, AddUserForm
|
from app.users.forms import LoginForm, RegisterUserForm#, RequestResetForm, ResetPasswordForm, EditUserForm, AddUserForm
|
||||||
from flask import render_template, redirect, url_for, flash, request
|
from flask import render_template, redirect, url_for, flash, request
|
||||||
from flask_login import login_required, login_user, current_user, logout_user
|
from flask_login import login_required, login_user, current_user, logout_user
|
||||||
|
|
@ -66,4 +68,17 @@ def register_user():
|
||||||
login_user(user)
|
login_user(user)
|
||||||
flash('Succesfully Registered!')
|
flash('Succesfully Registered!')
|
||||||
return redirect(url_for('main.homepage'))
|
return redirect(url_for('main.homepage'))
|
||||||
return render_template('register_user.html', form=form)
|
return render_template('register_user.html', form=form)
|
||||||
|
|
||||||
|
@users.route('/test')
|
||||||
|
def testing_route():
|
||||||
|
user = User.query.filter_by(id=1).first()
|
||||||
|
donation = Donation.query.filter_by(id=1).first()
|
||||||
|
campaign = Campaign.query.filter_by(id=1).first()
|
||||||
|
|
||||||
|
print(campaign)
|
||||||
|
|
||||||
|
return render_template('test.html',
|
||||||
|
user=user,
|
||||||
|
donation=donation,
|
||||||
|
campaign=campaign)
|
||||||
10
app/users/templates/test.html
Normal file
10
app/users/templates/test.html
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
{% extends 'base.html' %}
|
||||||
|
{% block title %}User Access{% endblock title %}
|
||||||
|
{% block content %}
|
||||||
|
{{user.first_name}}<br/>
|
||||||
|
{{donation.amount}}<br/>
|
||||||
|
|
||||||
|
{% for c in campaign.donations %}
|
||||||
|
{{c}}
|
||||||
|
{% endfor %}
|
||||||
|
{% endblock %}
|
||||||
|
|
@ -0,0 +1,97 @@
|
||||||
|
from sqlalchemy import create_engine, MetaData, Table, select, insert, func, update, bindparam, delete
|
||||||
|
import os
|
||||||
|
import csv
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from dateutil.parser import parse
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
from werkzeug.security import generate_password_hash
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def insert_users():
|
||||||
|
engine = create_engine('sqlite:///C:/Users/Lenovo/Desktop/Pilzno/instance/site.db')
|
||||||
|
metadata_obj = MetaData()
|
||||||
|
metadata_obj.reflect(bind=engine)
|
||||||
|
user_table = Table("user", metadata_obj, autoload_with=engine)
|
||||||
|
|
||||||
|
with engine.connect() as conn:
|
||||||
|
conn.execute(user_table.insert().values(
|
||||||
|
first_name = "Yisroel",
|
||||||
|
last_name = "Baum",
|
||||||
|
email = "yisroel.d.baum@gmail.com",
|
||||||
|
password = generate_password_hash('12'),
|
||||||
|
user_type = "User"
|
||||||
|
))
|
||||||
|
conn.execute(user_table.insert().values(
|
||||||
|
first_name = "Yoni",
|
||||||
|
last_name = "Gerzi",
|
||||||
|
email = "yoni@gerzi.com",
|
||||||
|
password = generate_password_hash('12'),
|
||||||
|
user_type = "User"
|
||||||
|
))
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
def insert_donations():
|
||||||
|
engine = create_engine('sqlite:///C:/Users/Lenovo/Desktop/Pilzno/instance/site.db')
|
||||||
|
metadata_obj = MetaData()
|
||||||
|
metadata_obj.reflect(bind=engine)
|
||||||
|
user_table = Table("user", metadata_obj, autoload_with=engine)
|
||||||
|
donation_table = Table("donation", metadata_obj, autoload_with=engine)
|
||||||
|
campaign_table = Table("campaign", metadata_obj, autoload_with=engine)
|
||||||
|
|
||||||
|
with engine.connect() as conn:
|
||||||
|
sruli = conn.execute(select(user_table).where(user_table.c.id == 1)).first()
|
||||||
|
yoni = conn.execute(select(user_table).where(user_table.c.id == 2)).first()
|
||||||
|
|
||||||
|
campaign_one = conn.execute(select(campaign_table).where(campaign_table.c.id == 2)).first()
|
||||||
|
|
||||||
|
conn.execute(donation_table.insert().values(
|
||||||
|
currency_type = "shekel",
|
||||||
|
amount = 50,
|
||||||
|
campaign_id=campaign_one.id,
|
||||||
|
user_id=sruli.id
|
||||||
|
))
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
def insert_campaigns():
|
||||||
|
engine = create_engine('sqlite:///C:/Users/Lenovo/Desktop/Pilzno/instance/site.db')
|
||||||
|
metadata_obj = MetaData()
|
||||||
|
metadata_obj.reflect(bind=engine)
|
||||||
|
campaign_table = Table("campaign", metadata_obj, autoload_with=engine)
|
||||||
|
|
||||||
|
with engine.connect() as conn:
|
||||||
|
conn.execute(campaign_table.insert().values(title="general campaign"))
|
||||||
|
conn.execute(campaign_table.insert().values(title="special campaign"))
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
def test_selections():
|
||||||
|
engine = create_engine('sqlite:///C:/Users/Lenovo/Desktop/Pilzno/instance/site.db')
|
||||||
|
metadata_obj = MetaData()
|
||||||
|
metadata_obj.reflect(bind=engine)
|
||||||
|
user_table = Table("user", metadata_obj, autoload_with=engine)
|
||||||
|
donation_table = Table("donation", metadata_obj, autoload_with=engine)
|
||||||
|
campaign_table = Table("campaign", metadata_obj, autoload_with=engine)
|
||||||
|
|
||||||
|
with engine.connect() as conn:
|
||||||
|
campaign_one = conn.execute(select(campaign_table).where(campaign_table.c.id==1)).first()
|
||||||
|
donation_one = conn.execute(select(donation_table).where(donation_table.c.id==1)).first()
|
||||||
|
user_one = conn.execute(select(user_table).where(user_table.c.id==1)).first()
|
||||||
|
print(user_one.donations)
|
||||||
|
|
||||||
|
def delete_all():
|
||||||
|
engine = create_engine('sqlite:///C:/Users/Lenovo/Desktop/Pilzno/instance/site.db')
|
||||||
|
metadata_obj = MetaData()
|
||||||
|
metadata_obj.reflect(bind=engine)
|
||||||
|
user_table = Table("user", metadata_obj, autoload_with=engine)
|
||||||
|
donation_table = Table("donation", metadata_obj, autoload_with=engine)
|
||||||
|
campaign_table = Table("campaign", metadata_obj, autoload_with=engine)
|
||||||
|
|
||||||
|
with engine.connect() as conn:
|
||||||
|
conn.execute(delete(user_table))
|
||||||
|
conn.execute(delete(campaign_table))
|
||||||
|
conn.execute(delete(donation_table))
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_selections()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue