phantasmobile/app/item/views.py
2023-07-15 13:00:36 +05:30

51 lines
1.3 KiB
Python

from datetime import date, datetime, timedelta
from django.shortcuts import render
from . import models
def index(request):
context = {}
week, archive = models.Item.public()
context['items'] = week
context['archive'] = archive.exists()
return render(request, 'index.html', context)
def archive(request):
context = {}
qs = models.Item.public()
week, archive = models.Item.public()
context['items'] = archive
return render(request, 'archive.html', context)
def item(request, id):
context = {}
item = models.Item.objects.get(id=id)
context['item'] = item
return render(request, 'item.html', context)
import json
from django.http import Http404, HttpResponse
def render_to_json(response):
content = json.dumps(response)
return HttpResponse(content, 'application/json; charset=utf-8')
def comment(request):
response = {}
data = json.loads(request.body)
print(data)
comment = models.Comment()
comment.item = models.Item.objects.get(id=data['item'])
if request.user.is_authenticated:
comment.user = request.user
comment.published = datetime.now()
else:
comment.name = data['name']
comment.email = data['email']
comment.text = data['text']
comment.save()
response = comment.json()
return render_to_json(response)