import os

with open("core/views.py", "a") as f:
    f.write("""
import json
from django.http import JsonResponse
from django.views.decorators.http import require_POST

@login_required
@require_POST
def edit_node(request, pk, node_id):
    project = get_object_or_404(Project, pk=pk, user=request.user)
    try:
        node = project.nodes.get(pk=node_id)
        data = json.loads(request.body)
        title = data.get('title', '').strip()
        summary = data.get('summary', '').strip()
        category = data.get('category', '').strip()

        if title:
            node.title = title
        if summary:
            node.summary = summary
        if category:
            node.category = category
            
        node.save()
        return JsonResponse({'status': 'success', 'node': {
            'id': node.pk,
            'title': node.title,
            'summary': node.summary,
            'category': node.category
        }})
    except Exception as e:
        return JsonResponse({'status': 'error', 'message': str(e)}, status=400)
""")
print("Updated views.py!")
