Lavlu118557 commited on
Commit
5da618c
Β·
verified Β·
1 Parent(s): 82fff72

Create routes.py

Browse files
Files changed (1) hide show
  1. routes.py +252 -0
routes.py ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import render_template, request, redirect, url_for, session, flash, jsonify
2
+ from flask_babel import _, ngettext
3
+ from app import app, db
4
+ from models import DataTable, DataPipeline, DataRecord
5
+ from utils import parse_yaml, fuzzy_search, render_markdown, create_dynamic_table
6
+ import json
7
+ import yaml
8
+ import logging
9
+
10
+ @app.route('/')
11
+ def index():
12
+ """Main dashboard"""
13
+ tables = DataTable.query.filter_by(is_active=True).all()
14
+ pipelines = DataPipeline.query.filter_by(is_active=True).all()
15
+ return render_template('index.html', tables=tables, pipelines=pipelines)
16
+
17
+ @app.route('/language/<language>')
18
+ def set_language(language=None):
19
+ """Set the language preference"""
20
+ if language in app.config['LANGUAGES']:
21
+ session['language'] = language
22
+ flash(_('Language changed successfully'), 'success')
23
+ else:
24
+ flash(_('Invalid language selection'), 'error')
25
+
26
+ # Redirect to the referring page or index
27
+ return redirect(request.referrer or url_for('index'))
28
+
29
+ @app.route('/create_table', methods=['GET', 'POST'])
30
+ def create_table():
31
+ """Create a new data table"""
32
+ if request.method == 'POST':
33
+ try:
34
+ name = request.form.get('name')
35
+ description = request.form.get('description')
36
+ schema_text = request.form.get('schema')
37
+
38
+ if not name or not schema_text:
39
+ flash(_('Table name and schema are required'), 'error')
40
+ return redirect(url_for('create_table'))
41
+
42
+ # Parse schema (can be JSON or YAML)
43
+ try:
44
+ if schema_text.strip().startswith('{'):
45
+ schema = json.loads(schema_text)
46
+ else:
47
+ schema = yaml.safe_load(schema_text)
48
+ except (json.JSONDecodeError, yaml.YAMLError) as e:
49
+ flash(_('Invalid schema format: {}').format(str(e)), 'error')
50
+ return redirect(url_for('create_table'))
51
+
52
+ # Check if table already exists
53
+ existing_table = DataTable.query.filter_by(name=name).first()
54
+ if existing_table:
55
+ flash(_('Table with this name already exists'), 'error')
56
+ return redirect(url_for('create_table'))
57
+
58
+ # Create table record
59
+ new_table = DataTable()
60
+ new_table.name = name
61
+ new_table.description = description
62
+ new_table.schema = schema
63
+ db.session.add(new_table)
64
+ db.session.commit()
65
+
66
+ flash(_('Table created successfully! πŸŽ‰'), 'success')
67
+ return redirect(url_for('table_view', table_name=name))
68
+
69
+ except Exception as e:
70
+ logging.error(f"Error creating table: {str(e)}")
71
+ flash(_('Error creating table: {}').format(str(e)), 'error')
72
+ return redirect(url_for('create_table'))
73
+
74
+ return render_template('create_table.html')
75
+
76
+ @app.route('/table/<table_name>')
77
+ def table_view(table_name):
78
+ """View and manage table data"""
79
+ table = DataTable.query.filter_by(name=table_name, is_active=True).first()
80
+ if not table:
81
+ flash(_('Table not found'), 'error')
82
+ return redirect(url_for('index'))
83
+
84
+ # Get records for this table
85
+ records = DataRecord.query.filter_by(table_name=table_name).order_by(DataRecord.created_at.desc()).all()
86
+
87
+ # Convert records to JSON-serializable format
88
+ records_data = []
89
+ for record in records:
90
+ records_data.append({
91
+ 'id': record.id,
92
+ 'data': record.data,
93
+ 'created_at': record.created_at.strftime('%Y-%m-%d %H:%M:%S') if record.created_at else None,
94
+ 'updated_at': record.updated_at.strftime('%Y-%m-%d %H:%M:%S') if record.updated_at else None
95
+ })
96
+
97
+ return render_template('table_view.html', table=table, records=records, records_json=records_data)
98
+
99
+ @app.route('/table/<table_name>/add', methods=['POST'])
100
+ def add_record(table_name):
101
+ """Add a new record to a table"""
102
+ table = DataTable.query.filter_by(name=table_name, is_active=True).first()
103
+ if not table:
104
+ flash(_('Table not found'), 'error')
105
+ return redirect(url_for('index'))
106
+
107
+ try:
108
+ # Get form data
109
+ data = {}
110
+ for field in table.schema.get('fields', []):
111
+ field_name = field.get('name')
112
+ field_value = request.form.get(field_name)
113
+ if field_value:
114
+ data[field_name] = field_value
115
+
116
+ # Create new record
117
+ new_record = DataRecord()
118
+ new_record.table_name = table_name
119
+ new_record.data = data
120
+ db.session.add(new_record)
121
+ db.session.commit()
122
+
123
+ flash(_('Record added successfully! βœ…'), 'success')
124
+
125
+ except Exception as e:
126
+ logging.error(f"Error adding record: {str(e)}")
127
+ flash(_('Error adding record: {}').format(str(e)), 'error')
128
+
129
+ return redirect(url_for('table_view', table_name=table_name))
130
+
131
+ @app.route('/data_pipeline', methods=['GET', 'POST'])
132
+ def data_pipeline():
133
+ """Create and manage data pipelines"""
134
+ if request.method == 'POST':
135
+ try:
136
+ name = request.form.get('name')
137
+ description = request.form.get('description')
138
+ source_table = request.form.get('source_table')
139
+ target_table = request.form.get('target_table')
140
+ yaml_config = request.form.get('yaml_config')
141
+
142
+ if not name or not source_table or not yaml_config:
143
+ flash(_('Pipeline name, source table, and YAML configuration are required'), 'error')
144
+ return redirect(url_for('data_pipeline'))
145
+
146
+ # Parse YAML configuration
147
+ try:
148
+ pipeline_config = yaml.safe_load(yaml_config)
149
+ except yaml.YAMLError as e:
150
+ flash(_('Invalid YAML configuration: {}').format(str(e)), 'error')
151
+ return redirect(url_for('data_pipeline'))
152
+
153
+ # Check if pipeline already exists
154
+ existing_pipeline = DataPipeline.query.filter_by(name=name).first()
155
+ if existing_pipeline:
156
+ flash(_('Pipeline with this name already exists'), 'error')
157
+ return redirect(url_for('data_pipeline'))
158
+
159
+ # Create pipeline record
160
+ new_pipeline = DataPipeline()
161
+ new_pipeline.name = name
162
+ new_pipeline.description = description
163
+ new_pipeline.source_table = source_table
164
+ new_pipeline.target_table = target_table
165
+ new_pipeline.pipeline_config = pipeline_config
166
+ new_pipeline.yaml_config = yaml_config
167
+ db.session.add(new_pipeline)
168
+ db.session.commit()
169
+
170
+ flash(_('Data pipeline created successfully! πŸš€'), 'success')
171
+ return redirect(url_for('index'))
172
+
173
+ except Exception as e:
174
+ logging.error(f"Error creating pipeline: {str(e)}")
175
+ flash(_('Error creating pipeline: {}').format(str(e)), 'error')
176
+ return redirect(url_for('data_pipeline'))
177
+
178
+ # Get available tables for dropdown
179
+ tables = DataTable.query.filter_by(is_active=True).all()
180
+ return render_template('data_pipeline.html', tables=tables)
181
+
182
+ @app.route('/search')
183
+ def search():
184
+ """Fuzzy search across all tables"""
185
+ query = request.args.get('q', '')
186
+ if not query:
187
+ return jsonify([])
188
+
189
+ results = []
190
+
191
+ # Search through all data records
192
+ records = DataRecord.query.all()
193
+ for record in records:
194
+ # Perform fuzzy search on record data
195
+ matches = fuzzy_search(query, record.data)
196
+ if matches:
197
+ results.append({
198
+ 'table_name': record.table_name,
199
+ 'record_id': record.id,
200
+ 'matches': matches,
201
+ 'data': record.data
202
+ })
203
+
204
+ return jsonify(results)
205
+
206
+ @app.route('/api/markdown', methods=['POST'])
207
+ def api_markdown():
208
+ """Render markdown text"""
209
+ json_data = request.get_json() or {}
210
+ text = json_data.get('text', '')
211
+ html = render_markdown(text)
212
+ return jsonify({'html': html})
213
+
214
+ @app.route('/delete_table/<table_name>', methods=['POST'])
215
+ def delete_table(table_name):
216
+ """Soft delete a table"""
217
+ table = DataTable.query.filter_by(name=table_name).first()
218
+ if table:
219
+ table.is_active = False
220
+ db.session.commit()
221
+ flash(_('Table deleted successfully'), 'success')
222
+ else:
223
+ flash(_('Table not found'), 'error')
224
+
225
+ return redirect(url_for('index'))
226
+
227
+ @app.route('/delete_pipeline/<int:pipeline_id>', methods=['POST'])
228
+ def delete_pipeline(pipeline_id):
229
+ """Soft delete a pipeline"""
230
+ pipeline = DataPipeline.query.get(pipeline_id)
231
+ if pipeline:
232
+ pipeline.is_active = False
233
+ db.session.commit()
234
+ flash(_('Pipeline deleted successfully'), 'success')
235
+ else:
236
+ flash(_('Pipeline not found'), 'error')
237
+
238
+ return redirect(url_for('index'))
239
+
240
+ @app.route('/delete_record/<int:record_id>', methods=['POST'])
241
+ def delete_record(record_id):
242
+ """Delete a data record"""
243
+ record = DataRecord.query.get(record_id)
244
+ if record:
245
+ table_name = record.table_name
246
+ db.session.delete(record)
247
+ db.session.commit()
248
+ flash(_('Record deleted successfully'), 'success')
249
+ return redirect(url_for('table_view', table_name=table_name))
250
+ else:
251
+ flash(_('Record not found'), 'error')
252
+ return redirect(url_for('index'))