Files
moto-adv-website/app/models.py
ske087 fc463dc69a Migrate from Next.js to Flask: Complete motorcycle adventure community website
- Replace Next.js/React implementation with Python Flask
- Add colorful blue-purple-teal gradient theme replacing red design
- Integrate logo and Transalpina panoramic background image
- Implement complete authentication system with Flask-Login
- Add community features for stories and tracks sharing
- Create responsive design with Tailwind CSS
- Add error handling with custom 404/500 pages
- Include Docker deployment configuration
- Add favicon support and proper SEO structure
- Update content for Pensiune BuonGusto accommodation
- Remove deprecated Next.js files and dependencies

Features:
 Landing page with hero section and featured content
 User registration and login system
 Community section for adventure sharing
 Admin panel for content management
 Responsive mobile-first design
 Docker containerization with PostgreSQL
 Email integration with Flask-Mail
 Form validation with WTForms
 SQLAlchemy database models
 Error pages and favicon handling
2025-07-23 14:42:40 +03:00

127 lines
5.0 KiB
Python

from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
db = SQLAlchemy()
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
nickname = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(255), nullable=False)
is_active = db.Column(db.Boolean, default=True, nullable=False)
is_admin = db.Column(db.Boolean, default=False, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Relationships
posts = db.relationship('Post', backref='author', lazy='dynamic', cascade='all, delete-orphan')
comments = db.relationship('Comment', backref='author', lazy='dynamic', cascade='all, delete-orphan')
likes = db.relationship('Like', backref='user', lazy='dynamic', cascade='all, delete-orphan')
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def __repr__(self):
return f'<User {self.nickname}>'
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(200), nullable=False)
subtitle = db.Column(db.String(300))
content = db.Column(db.Text, nullable=False)
difficulty = db.Column(db.Integer, default=3, nullable=False) # 1-5 scale
published = db.Column(db.Boolean, default=False, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Foreign Keys
author_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
# Relationships
images = db.relationship('PostImage', backref='post', lazy='dynamic', cascade='all, delete-orphan')
gpx_files = db.relationship('GPXFile', backref='post', lazy='dynamic', cascade='all, delete-orphan')
comments = db.relationship('Comment', backref='post', lazy='dynamic', cascade='all, delete-orphan')
likes = db.relationship('Like', backref='post', lazy='dynamic', cascade='all, delete-orphan')
def get_difficulty_label(self):
labels = ['Very Easy', 'Easy', 'Moderate', 'Hard', 'Very Hard']
return labels[self.difficulty - 1] if 1 <= self.difficulty <= 5 else 'Unknown'
def get_like_count(self):
return self.likes.count()
def __repr__(self):
return f'<Post {self.title}>'
class PostImage(db.Model):
__tablename__ = 'post_images'
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(255), nullable=False)
original_name = db.Column(db.String(255), nullable=False)
description = db.Column(db.Text)
size = db.Column(db.Integer, nullable=False)
mime_type = db.Column(db.String(100), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# Foreign Keys
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'), nullable=False)
def __repr__(self):
return f'<PostImage {self.filename}>'
class GPXFile(db.Model):
__tablename__ = 'gpx_files'
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(255), nullable=False)
original_name = db.Column(db.String(255), nullable=False)
size = db.Column(db.Integer, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# Foreign Keys
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'), nullable=False)
def __repr__(self):
return f'<GPXFile {self.filename}>'
class Comment(db.Model):
__tablename__ = 'comments'
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
# Foreign Keys
author_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'), nullable=False)
def __repr__(self):
return f'<Comment {self.id}>'
class Like(db.Model):
__tablename__ = 'likes'
id = db.Column(db.Integer, primary_key=True)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# Foreign Keys
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'), nullable=False)
# Unique constraint
__table_args__ = (db.UniqueConstraint('user_id', 'post_id', name='unique_user_post_like'),)
def __repr__(self):
return f'<Like {self.user_id}-{self.post_id}>'