CJ 1 år sedan
förälder
incheckning
52f64fc1ca
4 ändrade filer med 148 tillägg och 51 borttagningar
  1. 79 1
      blog/views.py
  2. 1 0
      system/urls.py
  3. 20 12
      templates/_common/base.html
  4. 48 38
      templates/index.html

+ 79 - 1
blog/views.py

@@ -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)

+ 1 - 0
system/urls.py

@@ -32,6 +32,7 @@ urlpatterns = [
     path('index', views.index),
     path('category/', include('blog.router.category_url.category_url')),
     path('article/', include('blog.router.article_url.article_url')),
+    path('search', views.search),
     path('date/<date>', views.current_record),
 
     path('login', login.login),

+ 20 - 12
templates/_common/base.html

@@ -24,7 +24,7 @@
             href="{% static 'semantic.min.css' %}"
             type="text/css"
     />
-<link
+    <link
             rel="stylesheet"
             href="{% static 'photoswipe/photoswipe.css' %}"
             type="text/css"
@@ -137,7 +137,7 @@
             <div class="right menu">
                 <a href="http://git.cybersicko.net" class="item"> <i class="icon gitlab "></i>
                     Git Repository</a>
-                  <a href="/login" class="item"> <i class="icon share square"></i>
+                <a href="/login" class="item"> <i class="icon share square"></i>
                     Sign in</a>
             </div>
         </div>
@@ -145,7 +145,7 @@
 </div>
 <div class="ui mobile only padded grid">
     <div class="ui top fixed borderless huge fluid menu">
-        <a  href="/" class="header item"><img style="width: 7em;" src="{% static '/img.png' %}"/></a>
+        <a href="/" class="header item"><img style="width: 7em;" src="{% static '/img.png' %}"/></a>
         <div class="right menu">
             <div class="item">
                 <button class="ui icon toggle basic button">
@@ -166,8 +166,8 @@
                 {% endif %} item" href="/category/{{ item.name }}?page=1">{{ item.name }}</a>
 
             {% endfor %}
-           <a href="/login" class="item"> <i class="icon share square"></i>
-                    Sign in</a>
+            <a href="/login" class="item"> <i class="icon share square"></i>
+                Sign in</a>
         </div>
     </div>
 </div>
@@ -183,13 +183,21 @@
 <script type="application/javascript" src="{% static 'photoswipe/photoswipe.umd.min.js' %}"></script>
 <script type="application/javascript" src="{% static 'photoswipe/photoswipe-lightbox.umd.min.js' %}"></script>
 <script>
-       var lightbox = new PhotoSwipeLightbox({
-            gallery: '.extra.images',
-            children: 'a',
-            // dynamic import is not supported in UMD version
-            pswpModule: PhotoSwipe
-        });
-        lightbox.init();
+    $('#search')
+        .search({
+            type: 'category',
+            minCharacters: 3,
+            apiSettings: {
+                url: '/search?keywords={query}'
+            }
+        })
+    var lightbox = new PhotoSwipeLightbox({
+        gallery: '.extra.images',
+        children: 'a',
+        // dynamic import is not supported in UMD version
+        pswpModule: PhotoSwipe
+    });
+    lightbox.init();
 
     $(document).ready(function () {
         $(".ui.toggle.button").click(function () {

+ 48 - 38
templates/index.html

@@ -12,48 +12,50 @@
                     <h2>动态</h2>
                     <div class="ui divider"></div>
                     {% for item in articles_new %}
-                    {% if item.status == 1 %}
-                         {% if item.type == 1 %}
-                            <div class="event">
-                                <div class="label">
-                                    <img src="/user_avatar/{{ item.user_id }}">
+                        {% if item.status == 1 %}
+                            {% if item.type == 1 %}
+                                <div class="event">
+                                    <div class="label">
+                                        <img src="/user_avatar/{{ item.user_id }}">
+                                    </div>
+                                    <div class="content">
+                                        <div class="date"
+                                             style="font-size: 1.1em">{{ item.created_time | days_until }}</div>
+                                        <div class="summary"><a>
+                                            {{ item.user__first_name }}{{ item.user__last_name }}</a>
+                                            发布了{{ item.category__name }}:&nbsp;<a
+                                                    href="/article/{{ item.id }}.html">{{ item.title }}</a></div>
+                                        <div class="extra text">{{ item.intro }}
+                                            &nbsp;&nbsp;&nbsp;Tags:
+                                            {% for tag in item.tags %}
+                                                <a href="/tag/{{ tag }}?page=1"><span
+                                                        class="label label-success">{{ tag }}</span></a>
+                                            {% endfor %}</div>
+                                    </div>
                                 </div>
-                                <div class="content">
-                                    <div class="date"
-                                         style="font-size: 1.1em">{{ item.created_time | days_until }}</div>
-                                    <div class="summary"><a>{{ item.user__first_name }}{{ item.user__last_name }}</a>
-                                        发布了{{ item.category__name }}:&nbsp;<a
-                                                href="/article/{{ item.id }}.html">{{ item.title }}</a></div>
-                                    <div class="extra text">{{ item.intro }}
-                                        &nbsp;&nbsp;&nbsp;Tags:
-                                        {% for tag in item.tags %}
-                                            <a href="/tag/{{ tag }}?page=1"><span
-                                                    class="label label-success">{{ tag }}</span></a>
-                                        {% endfor %}</div>
-                                </div>
-                            </div>
-                        {% elif item.type == 2 %}
-                            <div class="event">
-                                <div class="label">
-                                    <img src="/user_avatar/{{ item.user_id }}">
-                                </div>
-                                <div class="content">
-                                    <div class="date"
-                                         style="font-size: 1.1em">{{ item.created_time | days_until }}</div>
-                                    <div class="summary"><a>{{ item.user__first_name }}{{ item.user__last_name }}</a>
-                                        发布了{{ item.category__name }}:&nbsp;<a
-                                                href="/article/{{ item.id }}.html">{{ item.title }}</a></div>
-                                    {% autoescape off %}
-                                        <div class="extra images">
-                                            {{ item.html_text }}
-                                        </div>
-                                    {% endautoescape %}
+                            {% elif item.type == 2 %}
+                                <div class="event">
+                                    <div class="label">
+                                        <img src="/user_avatar/{{ item.user_id }}">
+                                    </div>
+                                    <div class="content">
+                                        <div class="date"
+                                             style="font-size: 1.1em">{{ item.created_time | days_until }}</div>
+                                        <div class="summary"><a>
+                                            {{ item.user__first_name }}{{ item.user__last_name }}</a>
+                                            发布了{{ item.category__name }}:&nbsp;<a
+                                                    href="/article/{{ item.id }}.html">{{ item.title }}</a></div>
+                                        {% autoescape off %}
+                                            <div class="extra images">
+                                                {{ item.html_text }}
+                                            </div>
+                                        {% endautoescape %}
+
+                                    </div>
 
                                 </div>
-
-                            </div>
+                            {% endif %}
                         {% endif %}
-                    {% endif %}
                     {% endfor %}
 
                     {% if is_login %}
@@ -78,6 +80,14 @@
             </div>
 
             <div class="four wide right floated column">
+
+                <h4 class="ui header">搜索</h4>
+                <div id="search" class="ui category search">
+                    <div class="ui icon input">
+                        <input class="prompt" type="text" placeholder="搜索">
+                        <i class="search icon"></i>
+                    </div>
+                </div>
                 <h4 class="ui header">归档</h4>
                 <div class="ui list">
                     {% for record in records %}