-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
170 lines (140 loc) · 6.28 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from celery import Celery
from datetime import datetime, timedelta
from models import db, Book, Request, Section, User,Feedback
import csv
import os
from flask import Flask
from smtplib import SMTP
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from celeryconfig import timezone
from celery.schedules import crontab
from flask import render_template
from jinja2 import Template
# Create a Celery instance
celery = Celery(__name__, broker='redis://localhost:6379/0')
def create_app():
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///library.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
return app
app = create_app()
@celery.task(bind=True)
def export_books_csv(self, librarian_id):
app = create_app()
with app.app_context():
books = db.session.query(
Book.name.label('book_name'),
Book.content.label('content'),
Book.author.label('author'),
Request.date_requested.label('date_issued'),
Request.date_returned.label('return_date'),
Section.name.label('section_name')
).join(Request, Request.book_id == Book.id).join(Section, Book.section_id == Section.id).filter(
Request.status.in_(['approved', 'returned', 'revoked'])
).all()
if not books:
return 'No books found to export.'
file_path = os.path.join(app.root_path, 'exports', f'books_export_{librarian_id}_{datetime.now().strftime("%Y%m%d%H%M%S")}.csv')
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, mode='w', newline='', encoding='utf-8') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['Book Name', 'Content', 'Author', 'Date Issued', 'Return Date', 'Section Name'])
for book in books:
writer.writerow([book.book_name, book.content, book.author, book.date_issued, book.return_date, book.section_name])
return file_path
# SMTP configuration
SMTP_HOST = "localhost"
SMTP_PORT = 1025
SENDER_EMAIL = '[email protected]'
SENDER_PASSWORD = ''
def send_message(to, subject, content_body):
msg = MIMEMultipart()
msg["To"] = to
msg["Subject"] = subject
msg["From"] = SENDER_EMAIL
msg.attach(MIMEText(content_body, 'html'))
with SMTP(host=SMTP_HOST, port=SMTP_PORT) as client:
client.send_message(msg)
@celery.task
def send_daily_reminders():
app = create_app()
with app.app_context():
now = datetime.now()
reminder_time = now.replace(hour=12, minute=0, second=0, microsecond=0)
if now > reminder_time:
reminder_time += timedelta(days=1)
users = db.session.query(User).all()
for user in users:
if user.last_login is None or (now - user.last_login).days > 1:
subject = 'Reminder to Visit Library'
body = f'Hello {user.username},<br><br>Please visit the library to return or borrow books.<br><br>Best regards,<br>Library Management System'
send_message(user.email, subject, body)
books_due_soon = db.session.query(Request).join(Book).filter(
Request.user_id == user.id,
Request.status == 'approved',
Book.return_date <= reminder_time
).all()
if books_due_soon:
subject = 'Reminder: Book Return Due Soon'
body = f'Hello {user.username},<br><br>You have books that need to be returned soon. Please return them by the due date.<br><br>Best regards,<br>Library Management System'
send_message(user.email, subject, body)
@celery.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(
crontab(hour=12, minute=0, second=0, microsecond=0),
send_daily_reminders.s(),
name='daily'
)
@celery.task
def generate_monthly_report():
app = create_app()
with app.app_context():
now = datetime.now()
start_date = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
end_date = (now + timedelta(days=31)).replace(day=1, hour=0, minute=0, second=0, microsecond=0)
# Fetch sections
sections = db.session.query(Section).all()
# Fetch issued books with their sections
issued_books = db.session.query(
Book.name.label('book_name'),
Book.content.label('content'),
Book.author.label('author'),
Request.date_requested.label('date_issued'),
Request.date_returned.label('return_date'),
Section.name.label('section_name')
).join(Request, Request.book_id == Book.id).join(Section, Book.section_id == Section.id).filter(
Request.date_requested.between(start_date, end_date)
).all()
# Fetch ratings
ratings = db.session.query(
Book.name.label('book_name'),
Feedback.rating.label('rating'),
Feedback.comment.label('comment'),
Feedback.date_created.label('date_created')
).join(Book, Feedback.book_id == Book.id).filter(
Feedback.date_created.between(start_date, end_date)
).all()
# Count users who logged in this month
user_logins = db.session.query(User).filter(
User.last_login.between(start_date, end_date)
).count()
# Generate HTML report
html_content = render_template('monthly_report.html',
sections=sections,
issued_books=issued_books,
ratings=ratings,
user_logins=user_logins-1)
subject = 'Monthly Activity Report'
# Define the recipient, e.g., librarian's email
recipient_email = '[email protected]'
send_message(recipient_email, subject, html_content)
return 'Monthly report sent successfully'
@celery.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(
crontab(day_of_month='1', hour=0, minute=0),
generate_monthly_report.s(),
name='monthly_report'
)