-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_db.py
More file actions
255 lines (209 loc) · 9.65 KB
/
Copy pathsetup_db.py
File metadata and controls
255 lines (209 loc) · 9.65 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python3
"""
Automatic Database Setup Script for ModernBlog
Detects database type and initializes automatically
"""
import os
import sys
from app import app, db
def setup_database():
"""Automatically detect and setup database"""
print("🔍 Detecting database configuration...")
# Check database configuration
database_url = os.environ.get('DATABASE_URL')
db_host = os.environ.get('DB_HOST')
db_name = os.environ.get('DB_NAME')
db_user = os.environ.get('DB_USER')
db_pass = os.environ.get('DB_PASS')
with app.app_context():
try:
# Test database connection
print("🔗 Testing database connection...")
db.engine.execute('SELECT 1')
# Check if database is PostgreSQL or SQLite
if 'postgresql' in str(db.engine.url):
print("✅ PostgreSQL detected")
db_type = "PostgreSQL"
else:
print("✅ SQLite detected")
db_type = "SQLite"
print(f"🏗️ Setting up {db_type} database...")
# Create all tables
db.create_all()
# Import models for data creation
from app import Category, Tag, Post, Comment, Setting, Page, Contact, User, MenuItem
from slugify import slugify
from datetime import datetime
# Check if database is already initialized
if Setting.query.first():
print("ℹ️ Database already initialized")
return
print("📋 Creating initial data...")
# Create default categories
categories_data = [
'Programming', 'Web Development', 'Python', 'JavaScript',
'Technology', 'Tutorial', 'News', 'Tips & Tricks'
]
for cat_name in categories_data:
if not Category.query.filter_by(name=cat_name).first():
category = Category(name=cat_name, slug=slugify(cat_name))
db.session.add(category)
# Create default tags
tags_data = [
'flask', 'python', 'web-dev', 'tutorial', 'beginner',
'advanced', 'tips', 'coding', 'programming', 'tech'
]
for tag_name in tags_data:
if not Tag.query.filter_by(name=tag_name).first():
tag = Tag(name=tag_name, slug=slugify(tag_name))
db.session.add(tag)
# Create default admin user
if not User.query.filter_by(username='mehedims').first():
admin_user = User(
username='mehedims',
email='admin@modernblog.com',
is_admin=True
)
admin_user.set_password('admin2244')
db.session.add(admin_user)
# Create default settings
default_settings = [
('site_name', 'ModernBlog'),
('gemini_api_key', ''),
('tracking_code', ''),
('ads_header', ''),
('ads_content', ''),
('ads_sidebar', ''),
('ads_footer', ''),
('logo', '')
]
for key, value in default_settings:
if not Setting.query.filter_by(key=key).first():
setting = Setting(key=key, value=value)
db.session.add(setting)
# Create sample pages
if not Page.query.filter_by(slug='about').first():
about_page = Page(
title='About Us',
slug='about',
content='''# About ModernBlog
Welcome to ModernBlog - a modern, feature-rich blogging platform built with Flask!
## Features
- AI-powered content generation with Google Gemini
- Responsive design with light/dark themes
- SEO optimized with complete meta tags
- Mobile-friendly admin panel
- Advertisement integration
- PostgreSQL and SQLite support
## Contact
Feel free to reach out to us for any questions or suggestions.
''',
published=True
)
db.session.add(about_page)
if not Page.query.filter_by(slug='contact').first():
contact_page = Page(
title='Contact',
slug='contact',
content='''# Contact Us
Get in touch with us using the form below:
<form method="POST" class="space-y-4 max-w-md">
<div>
<label for="name" class="block text-sm font-medium mb-1">Name *</label>
<input type="text" id="name" name="name" required class="w-full px-3 py-2 border rounded-lg">
</div>
<div>
<label for="email" class="block text-sm font-medium mb-1">Email *</label>
<input type="email" id="email" name="email" required class="w-full px-3 py-2 border rounded-lg">
</div>
<div>
<label for="subject" class="block text-sm font-medium mb-1">Subject *</label>
<input type="text" id="subject" name="subject" required class="w-full px-3 py-2 border rounded-lg">
</div>
<div>
<label for="message" class="block text-sm font-medium mb-1">Message *</label>
<textarea id="message" name="message" rows="4" required class="w-full px-3 py-2 border rounded-lg"></textarea>
</div>
<button type="submit" class="bg-blue-600 text-white px-6 py-2 rounded-lg hover:bg-blue-700">Send Message</button>
</form>
''',
published=True
)
db.session.add(contact_page)
# Create sample menu items
menu_items = [
('About', '/page/about', 1),
('Contact', '/page/contact', 2)
]
for title, url, order in menu_items:
if not MenuItem.query.filter_by(title=title).first():
menu_item = MenuItem(title=title, url=url, order=order, active=True)
db.session.add(menu_item)
# Commit all changes
db.session.commit()
# Create sample blog post
if not Post.query.first():
programming_cat = Category.query.filter_by(name='Programming').first()
flask_tag = Tag.query.filter_by(name='flask').first()
python_tag = Tag.query.filter_by(name='python').first()
sample_post = Post(
title='Welcome to ModernBlog - Your AI-Powered Blogging Platform',
slug='welcome-to-modernblog',
content='''# Welcome to ModernBlog! 🚀
Welcome to **ModernBlog**, a cutting-edge blogging platform built with Flask that combines modern web technologies with AI-powered content generation.
## What Makes ModernBlog Special?
### 🤖 AI-Powered Content Generation
- Generate high-quality blog posts using Google Gemini 1.5 Pro
- One-click content creation from simple topic prompts
- Markdown-formatted output ready for publishing
### 🎨 Modern Design
- Beautiful responsive UI with Tailwind CSS
- Light/Dark theme toggle with persistent storage
- Mobile-first design approach
- Professional admin interface
### 📱 Mobile-Responsive Admin Panel
- Full admin functionality on mobile devices
- Touch-friendly interface with hamburger navigation
- Responsive dashboard and management tools
### 💰 Advertisement Integration
- Multiple ad zones (header, content, sidebar, footer)
- Google AdSense ready with responsive containers
- Strategic ad placement for optimal revenue
## Database Support
ModernBlog supports both **SQLite** (default) and **PostgreSQL** for production deployments with automatic setup and security features.
Happy blogging! 🎉
''',
excerpt='Welcome to ModernBlog - a modern, AI-powered blogging platform built with Flask. Discover features like AI content generation, responsive design, and mobile admin panel.',
published=True,
category_id=programming_cat.id if programming_cat else None
)
if flask_tag:
sample_post.tags.append(flask_tag)
if python_tag:
sample_post.tags.append(python_tag)
db.session.add(sample_post)
db.session.commit()
print(f"✅ {db_type} database setup completed successfully!")
print("\n🎯 Next Steps:")
print("1. Start the application: python app.py")
print("2. Visit: http://localhost:5000")
print("3. Admin login: http://localhost:5000/admin/login")
print(" Username: mehedims")
print(" Password: admin2244")
print("\n🔧 Important:")
print("- Change the default admin password immediately")
print("- Configure your Gemini API key for AI features")
print("- Set up your site branding and settings")
except Exception as e:
print(f"❌ Database setup failed: {str(e)}")
print("\n🔍 Troubleshooting:")
if 'postgresql' in str(e).lower():
print("- Check PostgreSQL server is running")
print("- Verify database credentials")
print("- Ensure database exists")
else:
print("- Check file permissions")
print("- Ensure SQLite is available")
sys.exit(1)
if __name__ == '__main__':
setup_database()