|
@@ -1,11 +1,88 @@
|
|
|
from django.db import connection
|
|
|
-from django.http import HttpResponseRedirect
|
|
|
+from django.http import HttpResponseRedirect, JsonResponse
|
|
|
from django.shortcuts import render
|
|
|
from django.views.decorators.clickjacking import xframe_options_sameorigin
|
|
|
|
|
|
from blog import models
|
|
|
from blog.controller import article
|
|
|
|
|
|
+json_str = '''{
|
|
|
+ "results": {
|
|
|
+ "category1": {
|
|
|
+ "name": "Category 1",
|
|
|
+ "results": [
|
|
|
+ {
|
|
|
+ "title": "Result Title",
|
|
|
+ "url": "/optional/url/on/click",
|
|
|
+ "image": "optional-image.jpg",
|
|
|
+ "price": "Optional Price",
|
|
|
+ "description": "Optional Description"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "title": "Result Title",
|
|
|
+ "url": "/optional/url/on/click",
|
|
|
+ "image": "optional-image.jpg",
|
|
|
+ "price": "Optional Price",
|
|
|
+ "description": "Optional Description"
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ "category2": {
|
|
|
+ "name": "Category 2",
|
|
|
+ "results": [
|
|
|
+ {
|
|
|
+ "title": "Result Title",
|
|
|
+ "url": "/optional/url/on/click",
|
|
|
+ "image": "optional-image.jpg",
|
|
|
+ "price": "Optional Price",
|
|
|
+ "description": "Optional Description"
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // optional action below results
|
|
|
+ "action": {
|
|
|
+ "url": '/path/to/results',
|
|
|
+ "text": "View all 202 results"
|
|
|
+ }
|
|
|
+}'''
|
|
|
+
|
|
|
+
|
|
|
+def search(request):
|
|
|
+ if request.method == 'GET':
|
|
|
+ keywords = request.GET.get('keywords')
|
|
|
+ if keywords is not None:
|
|
|
+ search_dict = dict()
|
|
|
+ search_dict['title__contains'] = keywords
|
|
|
+ search_dict['markdown_text__contains'] = keywords
|
|
|
+ search_dict['status'] = 1
|
|
|
+
|
|
|
+ result = models.Article.objects.filter(**search_dict).values('id', 'intro', 'title', 'category__name')
|
|
|
+ result_list = {}
|
|
|
+
|
|
|
+ for item in result:
|
|
|
+ if result_list.get(item['category__name']) == None:
|
|
|
+ result_list[item['category__name']] = {}
|
|
|
+ result_list[item['category__name']]['title'] = item['category__name']
|
|
|
+ result_list[item['category__name']]['results'] = [
|
|
|
+ {
|
|
|
+ 'title': item['title'],
|
|
|
+ 'description': item['intro'],
|
|
|
+ 'url': '/article/{0}.html'.format(item['id'])
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ else:
|
|
|
+ result_list[item['category__name']]['results'].append({
|
|
|
+ 'title': item['title'],
|
|
|
+ 'description': item['intro'],
|
|
|
+ 'url': '/article/{0}.html'.format(item['id'])
|
|
|
+ })
|
|
|
+ return JsonResponse({'results':result_list}, safe=False)
|
|
|
+ else:
|
|
|
+ return JsonResponse({'results': []}, safe=False)
|
|
|
+ else:
|
|
|
+ return JsonResponse({"success": False, "message": "FUCK YOU"}, safe=False)
|
|
|
+
|
|
|
|
|
|
def current_record(request, date):
|
|
|
page = request.GET.get('page')
|
|
@@ -20,6 +97,7 @@ def current_record(request, date):
|
|
|
|
|
|
return render(request, 'category.html', context=context)
|
|
|
|
|
|
+
|
|
|
def avatar(request, pk):
|
|
|
sql = '''select avatar from auth_user au left join avatar_avatar aa on au.id = aa.user_id where au.id = {0}'''.format(
|
|
|
pk)
|