-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
51 lines (41 loc) · 1.65 KB
/
views.py
File metadata and controls
51 lines (41 loc) · 1.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
from django.shortcuts import render
from datetime import datetime
from pymongo import MongoClient
from .code_engine import generate_code, format_code, analyze_code, execute_code
# MongoDB setup
client = MongoClient("mongodb://localhost:27017/")
db = client["code_assistant"]
collection = db["generated_codes"]
def home(request):
context = {}
if request.method == "POST":
action = request.POST.get("action")
user_input = request.POST.get("user_input", "")
prompt = request.POST.get("prompt", "")
if action == "generate":
code = generate_code(user_input)
context['generated_code'] = code
elif action == "execute":
output = execute_code(user_input)
context['execution_output'] = output
elif action == "format":
formatted = format_code(user_input)
context['formatted_code'] = formatted
elif action == "analyze":
analysis = analyze_code(user_input)
context['analysis'] = analysis
# Simple rating based on presence of errors/warnings
rating = 10
if "error" in analysis.lower() or "fatal" in analysis.lower():
rating -= 5
if "warning" in analysis.lower():
rating -= 2
context['rating'] = max(rating, 1)
elif action == "save":
collection.insert_one({
"prompt": prompt,
"generated_code": user_input,
"timestamp": datetime.now()
})
context['save_status'] = "Code saved to MongoDB."
return render(request, 'assistant/index.html', context)