Skip to content

Commit

Permalink
Merge pull request #5 from Anano24/main
Browse files Browse the repository at this point in the history
Add CKEditor for term fields and update Term model structure
  • Loading branch information
RegiusPythonidae authored Sep 23, 2024
2 parents 41934e3 + 22f72a1 commit f68e7ff
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 32 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ SQLAlchemy==2.0.32
typing_extensions==4.12.2
Werkzeug==3.0.3
WTForms==3.1.2
Flask-CKEditor==1.0.0
Flask-WTF==1.2.1
13 changes: 9 additions & 4 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from flask_admin.contrib.sqla import ModelView

from src.config import Config
from src.extensions import db, migrate, login_manager
from src.extensions import db, migrate, login_manager, ckeditor
from src.views import main_blueprint
from src.admin import admin
from src.admin.term import TermView, ConnectedTermView
from src.admin.term import TermView, CategoryView
from src.admin.user import UserView
from src.commands import init_db, populate_db
from src.models import User, Term, ConnectedTerm
from src.models import User, Term, Category


COMMANDS = [
Expand All @@ -19,6 +19,8 @@
def create_app():
app = Flask(__name__, template_folder="templates")
app.config.from_object(Config)
app.config['CKEDITOR_PKG_TYPE'] = 'basic'


register_extensions(app)
app.register_blueprint(main_blueprint)
Expand All @@ -39,14 +41,17 @@ def register_extensions(app):
# Login-Manager
login_manager.init_app(app)

# CKEditor
ckeditor.init_app(app)

@login_manager.user_loader
def load_user(user_id):
return User.query.get(user_id)

# Flask-Admin
admin.init_app(app)
admin.add_view(TermView(Term, db.session, endpoint="term_panel", name="Terms"))
admin.add_view(ConnectedTermView(ConnectedTerm, db.session, endpoint="connected_term", name="Connected terms"))
admin.add_view(CategoryView(Category, db.session, endpoint="category", name="categories"))
admin.add_view(UserView(User, db.session))


Expand Down
88 changes: 75 additions & 13 deletions src/admin/term.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
from src.admin.base import SecureModelView
from src.models import Category
from src.models import ConnectedTerm
from flask_ckeditor import CKEditorField
from flask_admin.form.widgets import Select2Widget



class ConnectedTermView(SecureModelView):
class CategoryView(SecureModelView):
can_view_details = True
edit_modal = True
create_modal = True
can_create = True
can_edit = True
can_export = True

# Exclude the relationship fields
column_exclude_list = ['term1', 'term2']
# Display foreign keys instead
column_list = ['term1_id', 'term2_id', 'is_synonym']

form_columns = ['name', 'parent']
column_list = ['name', 'parent']

column_labels = {
"name": "კატეგორია",
"parent": "მშობელი კატეგორია",
}




class TermView(SecureModelView):
can_view_details = True
edit_modal = True
create_modal = True
can_create = True
can_edit = True
can_export = True

inline_models = (Category,)

form_overrides = {
'context_source': CKEditorField,
'term_source': CKEditorField,
'definition_source': CKEditorField,

}
create_template = 'admin/edit.html'
edit_template = 'admin/edit.html'


column_filters = ["geo_word", "eng_word"]

Expand All @@ -38,7 +56,9 @@ class TermView(SecureModelView):
"context",
"context_source",
"comment",
"category"
"category",
"synonyms",
"connected_terms"
]

form_columns = [
Expand All @@ -52,10 +72,11 @@ class TermView(SecureModelView):
"context",
"context_source",
"comment",
"category"
"category",
"synonyms",
"connected_terms"
]


column_sortable_list = [
"geo_word",
"eng_word"
Expand All @@ -78,4 +99,45 @@ class TermView(SecureModelView):
"context_source": "კონტექსტის წყარო",
"comment": "კომენტარი",
"category": "კატეგორია",
}
"synonyms": "სინონიმები",
"connected_terms": "დაკავშირებული სიტყვები"
}


form_args = {
'synonyms': {
'widget': Select2Widget(multiple=True)
},
'connected_terms': {
'widget': Select2Widget(multiple=True)
}
}


def on_model_change(self, form, model, is_created):

# Add synonyms
if form.synonyms.data:
for synonym_id in form.synonyms.data:
if synonym_id != model.id:
ConnectedTerm(term1_id=model.id, term2_id=synonym_id, is_synonym=True).save()

# Add connected terms
if form.connected_terms.data:
for connected_id in form.connected_terms.data:
if connected_id != model.id:
ConnectedTerm(term1_id=model.id, term2_id=connected_id, is_synonym=False).save()

super().on_model_change(form, model, is_created)


def on_model_delete(self, model):
# Manually delete connections (ConnectedTerm records) referencing the term
ConnectedTerm.query.filter(
(ConnectedTerm.term1_id == model.id) | (ConnectedTerm.term2_id == model.id)
).delete(synchronize_session=False)

# Proceed with the actual term deletion
super().on_model_delete(model)


Binary file modified src/database.db
Binary file not shown.
4 changes: 3 additions & 1 deletion src/extensions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_ckeditor import CKEditor


db = SQLAlchemy()
migrate = Migrate()
login_manager = LoginManager()
login_manager = LoginManager()
ckeditor = CKEditor()
2 changes: 1 addition & 1 deletion src/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def save(self):


def delete(self):
db.session.remove(self)
db.session.delete(self)
db.session.commit()
31 changes: 26 additions & 5 deletions src/models/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,39 @@ class Term(BaseModel):
geo_word = db.Column(db.String(100), nullable=False)
eng_word = db.Column(db.String(100), nullable=False)
grammar_form = db.Column(db.String(50), nullable=True)
term_source = db.Column(db.String(100), nullable=False)
term_source = db.Column(db.Text, nullable=False)
definition = db.Column(db.Text, nullable=False)
definition_source = db.Column(db.String(100), nullable=False)
definition_source = db.Column(db.Text, nullable=False)
term_type = db.Column(db.String(50), nullable=True)
context = db.Column(db.Text, nullable=True)
context_source = db.Column(db.String(100), nullable=True)
context_source = db.Column(db.Text, nullable=True)
comment = db.Column(db.Text, nullable=True)

category = db.relationship("Category", secondary="terms_categories", backref="terms")


# Relationships for synonyms
synonyms = db.relationship(
"Term",
secondary="connected_terms",
primaryjoin="and_(Term.id == ConnectedTerm.term1_id, ConnectedTerm.is_synonym == True)",
secondaryjoin="and_(Term.id == ConnectedTerm.term2_id, ConnectedTerm.is_synonym == True)",
backref="synonym_for"
)

# Relationships for general connected terms
connected_terms = db.relationship(
"Term",
secondary="connected_terms",
primaryjoin="and_(Term.id == ConnectedTerm.term1_id, ConnectedTerm.is_synonym == False)",
secondaryjoin="and_(Term.id == ConnectedTerm.term2_id, ConnectedTerm.is_synonym == False)",
backref="connected_for"
)


def __repr__(self):
return f"({self.eng_word} - {self.geo_word})"




Expand All @@ -32,14 +52,15 @@ class ConnectedTerm(BaseModel):
id = db.Column(db.Integer, primary_key=True)
term1_id = db.Column(db.Integer, db.ForeignKey('terms.id'), nullable=True)
term2_id = db.Column(db.Integer, db.ForeignKey('terms.id'), nullable=True)
is_synonym = db.Column(db.Boolean, nullable=False)
is_synonym = db.Column(db.Boolean, nullable=False, default=False)


term1 = db.relationship('Term', foreign_keys=[term1_id], backref='term1_connections')
term2 = db.relationship('Term', foreign_keys=[term2_id], backref='term2_connections')


def __repr__(self):
return f"ConnectedTerm('{self.term1_id}', '{self.term2_id}', synonym={self.is_synonym})"
return f"{self.term1} - {self.term2}"



Expand Down
2 changes: 1 addition & 1 deletion src/templates/admin/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
{% endblock %}
{% block head_tail %}
{% endblock %}
{{ ckeditor.load() }}
</head>
<body>
{% block page_body %}

<nav class="navbar navbar-expand-lg navbar-dark bg-dark mb-2" role="navigation">

<!-- Brand and toggle get grouped for better mobile display -->
Expand Down
10 changes: 10 additions & 0 deletions src/templates/admin/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends 'admin/model/edit.html' %}

{% block tail %}
{{ super() }} <!-- Ensures other admin scripts are loaded -->
{{ ckeditor.load() }} <!-- Loads CKEditor scripts -->

{{ ckeditor.config(name='context_source') }}
{{ ckeditor.config(name='term_source') }}
{{ ckeditor.config(name='definition_source') }}
{% endblock %}
27 changes: 20 additions & 7 deletions src/templates/main/term_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{% endblock %}



{% block content %}

<div class="main-outer edge-padding">
Expand Down Expand Up @@ -34,8 +35,7 @@ <h1 class="term-word-detail">{{term.eng_word}} - {{ term.geo_word }}</h1>
<section class="term-source">
<h2>ტერმინის წყარო:</h2>
<p>
{{term.term_source}}
Smith, J. (2020). <a href="{{term.term_source}}" previewlistener="true">The art of coding</a>. TechBooks Publishing.
{{ term.term_source|safe }}
</p>
</section>
<section class="term-definition">
Expand All @@ -47,7 +47,7 @@ <h2>განმარტება:</h2>
<section class="term-definition-source">
<h2>განმარტების წყარო:</h2>
<p>
Smith, J. (2020). <a href="{{term.definition_source}}" previewlistener="true">The art of coding</a>. TechBooks Publishing.
{{term.definition_source|safe}}
</p>
</section>
<section class="term-type">
Expand All @@ -63,32 +63,45 @@ <h2>კონტექსტი:</h2>
<section class="term-context-source">
<h2>კონტექსტის წყარო:</h2>
<p>
Smith, J. (2020). <a href="{{term.context_source}}" previewlistener="true">The art of coding</a>. TechBooks Publishing.
{{term.context_source|safe}}
</p>
</section>
<section class="term-synonyms">
<h2>სინონიმები:</h2>
{% if synonyms %}
<ul>
{% for synonym in synonyms %}
{% if synonym.term1 and synonym.term2 %}
{% set related_term = synonym.term1 if synonym.term2.id == term.id else synonym.term2 %}
<li><a href="{{ url_for('main.term_detail', term_id=related_term.id) }}">{{ related_term.geo_word }}</a></li>
{% else %}
<li>Invalid synonym data</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p>No synonyms available</p>
{% endif %}

</section>

<section class="term-related">
<h2>დაკავშირებული სიტყვები:</h2>
{% if connected_terms %}
<ul>
{% for connected_term in connected_terms %}
{% set related_term = connected_term.term1 if connected_term.term2.id == term.id else connected_term.term2 %}
<li><a href="{{ url_for('main.term_detail', term_id=related_term.id) }}">{{ related_term.geo_word }}</a></li>
{% if connected_term.term1 and connected_term.term2 %}
{% set related_term = connected_term.term1 if connected_term.term2.id == term.id else connected_term.term2 %}
<li><a href="{{ url_for('main.term_detail', term_id=related_term.id) }}">{{ related_term.geo_word }}</a></li>
{% else %}
<li>Invalid connection data</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p>No related terms available</p>
{% endif %}
</section>

<section class="term-comment">
<h2>კომენტარი:</h2>
<p>
Expand Down

0 comments on commit f68e7ff

Please sign in to comment.