add repr to class models and exploring relationships

This commit is contained in:
Yisroel Baum 2024-09-15 15:25:27 +03:00
parent 936a88f264
commit 67264b3da6
11 changed files with 155 additions and 17 deletions

View file

@ -11,5 +11,8 @@ class Campaign(db.Model):
__tablename__ = 'campaign'
id = Column('id', INTEGER(), primary_key=True)
title = Column('title', TEXT(), nullable=False)
donation_id = Column(INTEGER, ForeignKey('donation.id'))
#ambassadors
donations = db.relationship('Donation', backref='campaign', lazy='dynamic')
def __repr__(self) -> str:
return f"{self.id} - {self.title}"
# donation_id = Column(INTEGER, ForeignKey('donation.id'))

View file

@ -11,5 +11,11 @@ class Donation(db.Model):
id = Column('id', INTEGER(), primary_key=True, autoincrement=True)
currency_type = Column('currency_type', TEXT(), 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')

View file

@ -13,19 +13,20 @@
<body>
<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('users.testing_route')}}" style="text-decoration: none; margin-right: 10px;"><p>Test</p></a>
{% 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>
{% if current_user.user_type == 'Admin' %}
<a href="{{url_for('admin.administration')}}" style="text-decoration: none;"><span class="navbar-brand">Admin Management</span></a>
{% endif %}
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link active" href="{{url_for('users.logout')}}">Logout</a>
</div>
</div>
<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' %}
<a href="{{url_for('admin.administration')}}" style="text-decoration: none;"><span class="navbar-brand">Admin Management</span></a>
{% endif %}
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link active" href="{{url_for('users.logout')}}">Logout</a>
</div>
</div>
{% else %}
<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.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>
{% endif %}
</nav>
{% with messages = get_flashed_messages(with_categories=true) %}

View file

@ -14,7 +14,13 @@ class User(db.Model, UserMixin):
email = Column('email', TEXT(), nullable=False, unique=True)
password = Column('password', 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):
reset_token = jwt.encode(

View file

@ -1,6 +1,8 @@
from app import db
from app.users import users
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 flask import render_template, redirect, url_for, flash, request
from flask_login import login_required, login_user, current_user, logout_user
@ -66,4 +68,17 @@ def register_user():
login_user(user)
flash('Succesfully Registered!')
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)

View 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 %}