elukjanovica 2024-04-28 20:04:50 +03:00
parent eb7729c4d4
commit a5317978af
6 changed files with 284 additions and 41 deletions

Binary file not shown.

142
main.py
View File

@ -1,14 +1,25 @@
from flask import Flask, render_template, redirect, request, session, url_for, g
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship
from flask_admin import Admin, AdminIndexView, expose, BaseView
from flask_admin.contrib.sqla import ModelView
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from wtforms import StringField, TextAreaField, SelectField
from wtforms.validators import InputRequired
from werkzeug.utils import secure_filename
from functools import wraps
from datetime import datetime
import os
import logging
app = Flask(__name__)
app.secret_key = 'bebra'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///Picture_Puzzle_web.db'
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif', 'mp4', 'waw'}
db = SQLAlchemy(app)
logging.basicConfig(level=logging.DEBUG)
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
@ -25,29 +36,48 @@ class Post(db.Model):
image = db.Column(db.String(100), nullable=False)
class ForumCategory(db.Model):
__tablename__ = 'forumcategory'
id = db.Column(db.Integer, primary_key=True)
category_name = db.Column(db.String(100), nullable=False)
description = db.Column(db.String(200))
class ForumPost(db.Model):
__tablename__ = 'forumpost'
id = db.Column(db.Integer, primary_key=True)
category_id = db.Column(db.Integer, db.ForeignKey('forum_category.id'), nullable=False)
category_id = db.Column(db.Integer, db.ForeignKey('forumcategory.id'), nullable=False)
post_name = db.Column(db.String(100), nullable=False)
created_by = db.Column(db.String(100), nullable=False)
creation_date = db.Column(db.DateTime, nullable=False)
media = db.Column(db.String(100))
creation_date = db.Column(db.DateTime, default=datetime.now)
media = db.relationship('Media', backref='forumpost', lazy=True)
text = db.Column(db.Text, nullable=False)
edited = db.Column(db.Boolean, default=False)
class ForumComment(db.Model):
__tablename__ = 'forumcomment'
id = db.Column(db.Integer, primary_key=True)
post_id = db.Column(db.Integer, db.ForeignKey('forum_post.id'), nullable=False)
post_id = db.Column(db.Integer, db.ForeignKey('forumpost.id'), nullable=False)
created_by = db.Column(db.String(100), nullable=False)
creation_date = db.Column(db.DateTime, nullable=False)
media = db.Column(db.String(100))
media = db.Column(db.Integer) # Assuming 'media' is a column containing media IDs
text = db.Column(db.Text, nullable=False)
edited = db.Column(db.Boolean, default=False)
# Define a primaryjoin expression
post = relationship("ForumPost", primaryjoin="foreign(ForumComment.post_id) == remote(ForumPost.id)")
class Media(db.Model):
__tablename__ = 'media'
id = db.Column(db.Integer, primary_key=True)
post_id = db.Column(db.Integer, db.ForeignKey('forumpost.id'), nullable=False)
filename = db.Column(db.String(100), nullable=False)
class CreatePostForm(FlaskForm):
category_id = SelectField('Category', validators=[InputRequired()], coerce=int)
post_name = StringField('Post Title', validators=[InputRequired()])
created_by = StringField('Your Name', validators=[InputRequired()])
text = TextAreaField('Post Content', validators=[InputRequired()])
media = FileField('Insert Media', validators=[FileAllowed(app.config['ALLOWED_EXTENSIONS'])])
def admin_login_required(view_func):
@wraps(view_func)
def decorated_function(*args, **kwargs):
@ -64,7 +94,6 @@ class MyAdminIndexView(AdminIndexView):
class UserAdminView(ModelView):
column_exclude_list = ['password']
form_excluded_columns = ['password']
can_export = True
export_types = ['csv']
@ -72,6 +101,18 @@ class PostAdminView(ModelView):
can_export = True
export_types = ['csv']
class ForumCategoryAdminView(ModelView):
can_export = True
export_types = ['csv']
class ForumPostAdminView(ModelView):
can_export = True
export_types = ['csv']
class ForumCommentAdminView(ModelView):
can_export = True
export_types = ['csv']
class LogoutView(BaseView):
@expose('/')
def index(self):
@ -81,6 +122,9 @@ class LogoutView(BaseView):
admin = Admin(app, name='Admin Panel', template_mode='bootstrap3', index_view=MyAdminIndexView())
admin.add_view(UserAdminView(User, db.session))
admin.add_view(PostAdminView(Post, db.session))
admin.add_view(ForumCategoryAdminView(ForumCategory, db.session))
admin.add_view(ForumPostAdminView(ForumPost, db.session))
admin.add_view(ForumCommentAdminView(ForumComment, db.session))
admin.add_view(LogoutView(name='Logout', endpoint='admin_logout'))
@app.before_request
@ -177,47 +221,73 @@ def post(alias):
@app.route('/forums')
def forums():
categories = ForumCategory.query.all()
posts = ForumPost.query.all()
comments = ForumComment.query.all()
return render_template('forums.html', categories=categories, posts=posts, comments=comments)
return render_template('forums.html', categories=categories)
@app.route('/forums/<int:category_id>')
def category(category_id):
@app.route('/forums/<category_name>')
def category(category_name):
category_name = category_name.capitalize()
print("Received category name:", category_name)
category = ForumCategory.query.filter_by(category_name=category_name).first()
if category:
posts = ForumPost.query.filter_by(category_id=category.id).limit(10).all()
return render_template('category.html', category=category, posts=posts)
else:
return "Category not found", 404
@app.route('/forums/<int:category_id>/create_post', methods=['GET', 'POST'])
def create_post(category_id):
form = CreatePostForm()
category = ForumCategory.query.get_or_404(category_id)
posts = ForumPost.query.filter_by(category_id=category_id).all()
return render_template('category.html', category=category, posts=posts)
@app.route('/forums/<int:post_id>')
def new_post(post_id):
post = ForumPost.query.get_or_404(post_id)
comments = ForumComment.query.filter_by(post_id=post_id).all()
return render_template('post.html', post=post, comments=comments)
# Provide choices for category_id field
form.category_id.choices = [(category.id, category.category_name) for category in ForumCategory.query.all()]
@app.route('/forums/create_post', methods=['GET', 'POST'])
def create_post():
if request.method == 'POST':
category_id = request.form['category_id']
post_name = request.form['post_name']
created_by = request.form['created_by']
text = request.form['text']
new_post = ForumPost(category_id=category_id, post_name=post_name, created_by=created_by, text=text) # type: ignore
if form.validate_on_submit():
post_name = form.post_name.data
created_by = form.created_by.data
text = form.text.data
media_files = request.files.getlist('media')
media_filenames = []
for file in media_files:
if file:
filename = secure_filename(file.filename) # type: ignore
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
media_filenames.append(filename)
new_post = ForumPost(
category_id=category_id,
post_name=post_name,
created_by=created_by,
text=text,
creation_date=datetime.now(),
edited=False
) # type: ignore
db.session.add(new_post)
db.session.commit()
return redirect(url_for('forums'))
else:
categories = ForumCategory.query.all()
return render_template('create_post.html', categories=categories)
@app.route('/forums/create_comment', methods=['POST']) # type: ignore
def create_comment():
for filename in media_filenames:
new_media = Media(post_id=new_post.id, filename=filename) # type: ignore
db.session.add(new_media)
db.session.commit()
return redirect(url_for('category', category_id=category_id))
return render_template('create_post.html', form=form, category=category)
@app.route('/forums/<int:post_id>', methods=['GET', 'POST'])
def view_post(post_id):
post = ForumPost.query.get_or_404(post_id)
comments = ForumComment.query.filter_by(post_id=post_id).all()
if request.method == 'POST':
post_id = request.form['post_id']
created_by = request.form['created_by']
text = request.form['text']
new_comment = ForumComment(post_id=post_id, created_by=created_by, text=text) # type: ignore
new_comment = ForumComment(post_id=post_id, created_by=created_by, text=text) # type: ignore
db.session.add(new_comment)
db.session.commit()
return redirect(url_for('post', post_id=post_id))
return redirect(url_for('view_post', post_id=post_id))
return render_template('post.html', post=post, comments=comments)
@app.route('/forums/upvote_post/<int:post_id>')
def upvote_post(post_id):

View File

@ -24,8 +24,7 @@
<a href="#" class="dropbtn">Docs</a>
<div class="dropdown-content">
<a href="https://gitea.rkg.lv/elukjanovica/Picture_Puzzle">Source page</a>
<a href="#">Documentation</a>
<a href="#">Tutorials</a>
<a href="gitea.rkg.lv/elukjanovica/Picture_Puzzle/wiki">Documentation</a>
</div>
</li>
<li class="dropdown">

View File

@ -7,6 +7,7 @@
<div class="row">
<div class="col-md-12">
<h1>{{ category.category_name }}</h1>
<a href="{{ url_for('create_post', category_id=category.id) }}" class="btn btn-primary mb-3">Create Post</a>
<div class="card mt-4">
<div class="card-body">
<div class="list-group">

View File

@ -0,0 +1,169 @@
{% extends 'base.html' %}
{% block title %}Create Post{% endblock %}
{% block content %}
<style>
.form-group {
margin-bottom: 20px;
position: relative;
}
.container {
display: flex;
justify-content: center;
}
form {
width: 120%;
padding: 20px;
border-radius: 5px;
background-color: #343a40;
box-shadow: 0 0 10px rgba(255, 255, 255, 0.1);
}
input[type="text"],
textarea {
width: calc(100% - 24px);
padding: 10px;
border-radius: 5px;
cursor: text;
}
input[type="file"] {
width: calc(100% - 24px);
padding: 10px;
border-radius: 5px;
position: absolute;
left: 0;
top: 0;
opacity: 0;
cursor: pointer;
}
.btn-primary {
background-color: #007bff;
border: none;
color: #fff;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
box-shadow: rgba(45, 35, 66, 0.4) 0 2px 4px, rgba(45, 35, 66, 0.3) 0 7px 13px -3px, #0051a8 0 -3px 0 inset;
}
button:focus {
box-shadow: #0051a8 0 0 0 1.5px inset, rgba(45, 35, 66, 0.4) 0 2px 4px, rgba(45, 35, 66, 0.3) 0 7px 13px -3px, #0051a8 0 -3px 0 inset;
}
.btn-primary:hover {
box-shadow: rgba(45, 35, 66, 0.4) 0 4px 8px, rgba(45, 35, 66, 0.3) 0 7px 13px -3px, #0051a8 0 -3px 0 inset;
}
.note {
font-style: italic;
color: #ccc;
}
.custom-file-upload {
border: 1px solid #ccc;
display: inline-block;
padding: 10px;
cursor: pointer;
width: calc(100% - 44px);
border-radius: 5px;
background-color: #fff;
color: #333;
text-align: center;
}
</style>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Create a New Post</h1>
<form method="POST" enctype="multipart/form-data">
<div class="form-group">
<input type="text" class="form-control" id="post_name" name="post_name" placeholder="Title" required>
</div>
<div class="form-group">
<textarea class="form-control" id="text" name="text" rows="6" placeholder="Content" required></textarea>
</div>
<label for="media" class="custom-file-upload" id="drop-area">
Click or drag & drop to upload media
</label>
<input type="file" id="media" name="media" style="display: none;" multiple><br>
<ul id="file-list" class="file-list"></ul>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<p class="note"><strong>Note:</strong> **bold** for bold text, *italic* for italic text</p>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const textarea = document.getElementById('text');
const fileList = document.getElementById('file-list');
const dropArea = document.getElementById('drop-area');
const fileInput = document.getElementById('media');
textarea.addEventListener('input', function() {
const text = textarea.value;
const boldRegex = /\*\*(.*?)\*\*/g;
const italicRegex = /\*(.*?)\*/g;
const newText = text.replace(boldRegex, '<strong>$1</strong>').replace(italicRegex, '<em>$1</em>');
textarea.innerHTML = newText;
});
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false);
});
function highlight() {
dropArea.style.backgroundColor = '#f0f0f0';
}
function unhighlight() {
dropArea.style.backgroundColor = '#fff';
}
dropArea.addEventListener('drop', handleDrop, false);
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
handleFiles(files);
}
function handleFiles(files) {
fileList.innerHTML = '';
for (let i = 0; i < files.length; i++) {
const file = files[i];
const listItem = document.createElement('li');
listItem.textContent = file.name;
listItem.classList.add('file-list-item');
fileList.appendChild(listItem);
}
}
fileInput.addEventListener('change', function() {
handleFiles(this.files);
});
});
</script>
{% endblock %}

View File

@ -12,13 +12,17 @@
<div class="card-header">{{ category.category_name }}</div>
<div class="card-body">
<div class="list-group">
{% set count = 0 %}
{% for post in posts %}
{% if post.category_id == category.id %}
{% if count < 10 %}
<a href="{{ url_for('new_post', post_id=post.id) }}" class="list-group-item list-group-item-action">
<h5 class="mb-1">{{ post.post_name }}</h5>
<p class="mb-1">{{ post.text }}</p>
<small>Created by {{ post.created_by }} - {{ post.creation_date }}</small>
</a>
{% set count = count + 1 %}
{% endif %}
{% endif %}
{% endfor %}
</div>