瀏覽代碼

210903 caturl

yukyo0821 3 年之前
父節點
當前提交
02ba05432f

+ 2 - 1
backstage/blogs/forms.py

@@ -16,7 +16,8 @@ class BlogCreateForm(FlaskForm):
     title = StringField('標題', validators=[DataRequired()])
     image = FileField('圖片', validators=[
         FileRequired(), FileAllowed(['jpg', 'png', 'gif', 'webp'], 'Images only!')])
-    categories = SelectField('主題類別', choices=selected_choices)
+    #categories = SelectField('主題類別', choices=selected_choices)
+    categories = StringField('類別', validators=[DataRequired()])
 
 
 

+ 24 - 4
backstage/blogs/routes.py

@@ -4,10 +4,11 @@ import requests
 import os
 import fnmatch
 from backstage.blogs.forms import BlogCreateForm
-from backstage.utils import get_now_time
+from backstage.utils import get_now_time, translate
 from backstage.utils.routes import create_content, remove_content, get_trans_title_url_name
 from backstage.config import PORTAL_SERVER, UPLOAD_PATH_MAP
 
+
 blogs_app = Blueprint('blogs', __name__)
 
 
@@ -27,6 +28,15 @@ def blog_list():
 @blogs_app.route('/backstage/blog/create/', methods=['POST'])
 def create():
     form = BlogCreateForm()
+    if form.categories.data == "居家美學":
+        caturl = "home_aesthetics"
+    elif form.categories.data == "規劃師QA":
+        caturl = "room_planner_expertise"
+    elif form.categories.data == "驗屋知識":
+        caturl = "home_inspection_knowledge"
+    else:
+        caturl = get_trans_title_url_name(form.categories.data)
+
     front_matter = '''---
 title: "{}"\n\
 date: {}\n\
@@ -35,18 +45,22 @@ type: "{}"\n\
 url: "{}"\n\
 image: "/img/blog/{}"\n\
 categories: ["{}"]\n\
+caturl: "{}"\n\
 ---'''.format(form.title.data,
               get_now_time(),
               'false',
               'blog',
               '/blog/{}'.format(get_trans_title_url_name(form.title.data)),
               form.image.data.filename,
-              form.categories.data)
+              form.categories.data,
+              caturl)
     data = {'frontMatter': front_matter,
             'name': get_trans_title_url_name(request.form['title']),
             'type': 'blog',
-            'categories': form.categories.data}
-    print(form.title.data)
+            'categories': form.categories.data,
+            'caturl': caturl
+            }
+
     return create_content(data, form.image.data)
 
 
@@ -83,6 +97,12 @@ def remove():
     return redirect(url_for('blogs.blog_list'))
 
 
+@blogs_app.route('/backstage/utils', methods=['GET'])
+def transService():
+    #print(request.args["trantext"])
+    return get_trans_title_url_name(request.args["trantext"])
+
+
 """ def GetCategories():
     GetCategories
     configfiles = [os.path.join(dirpath, f)

+ 0 - 1
backstage/static/js/editor.js

@@ -164,7 +164,6 @@ submitButton.onclick = function () {
 
   editor.save().then((outputData) => {
     //console.log('Article data: ', outputData);
-
     var mdContent = GetMdHeader();
     mdContent = mdContent.replace('draft: ' + (!$('#cdraft').is(':checked')), 'draft: ' + $('#cdraft').val())
     //alert(mdContent);

+ 5 - 0
backstage/static/js/yo.js

@@ -69,6 +69,10 @@ function parseMd(content) {
                 //alert(line);
                 $('#ccategories').val(line.replace('categories: ', '').replace('categories: ', '').replace(/\[|]/g, '').replace(/\"/g, ''));
             }
+            if (line.includes('caturl: ')) {
+                //alert(line);
+                $('#ccaturl').val(line.replace('caturl: ', '').replaceAll('\"', ''));
+            }
             if (line.includes('col1: ')) {
                 //alert(line);
                 $('#ccol1').val(line.replace('col1: ', '').replaceAll('\"', ''));
@@ -159,6 +163,7 @@ function GetMdHeader() {
     }
     else if ($('#ctype').val() == "blog") {
         rContent += 'categories: ["' + $('#ccategories').val() + '"]\n';
+        rContent += 'caturl: "' + $('#ccaturl').val() + '"\n';
         rContent += 'col1: "' + ($('#ccol1').val() == 'undefined' ? "" : $('#ccol1').val()) + '"\n';
         rContent += 'col2: "' + ($('#ccol2').val() == 'undefined' ? "" : $('#ccol2').val()) + '"\n';
     }

+ 1 - 0
backstage/templates/blogs.html

@@ -104,6 +104,7 @@
           <input id="cweight" type="text" /><br />
           <input id="ctag" type="text" /><br />
           <input id="ccategories" type="text" /><br />
+          <input id="ccaturl" type="text" /><br />
           <input id="ccol1" type="text" /><br />
           <input id="ccol2" type="text" /><br />
         </div>

+ 21 - 12
backstage/templates/editor.html

@@ -5,23 +5,31 @@
     <!-- <div class="column column__edit"> -->
     <div class="container col-sm-12 col-md-8">
       <button id='submit_button' class="btn__submit"><b>編輯完成</b></button>
-     
-      <h3 class="h3 column__h3"><i class="fas fa-user-edit mr-1 mb-2"></i> 作品編輯</h3>
+
+      <h3 style='display: none;' class="h3 column__h3"><i class="fas fa-user-edit mr-1 mb-2"></i> 作品編輯</h3>
       <table class="table table-bordered">
         <tbody>
           <tr style='display: none;'>
-            <td><h4>標題</h4></td>
-            <td><input class="form-control" id="ctitle" type="text" /> <div class="mt-1 text-danger">(建議字數: 15字內)</div></td>
+            <td>
+              <h4>標題</h4>
+            </td>
+            <td><input class="form-control" id="ctitle" type="text" />
+              <div class="mt-1 text-danger">(建議字數: 15字內)</div>
+            </td>
           </tr>
           {% if type == 'collection' %}
           <tr>
-            <td><h4>描述</h4></td>
-            <td><textarea class="form-control" rows="3" id="cdescription"></textarea> <div class="mt-1 text-danger">(建議字數: 50字內)</div></td>
+            <td>
+              <h4>描述</h4>
+            </td>
+            <td><textarea class="form-control" rows="3" id="cdescription"></textarea>
+              <div class="mt-1 text-danger">(建議字數: 50字內)</div>
+            </td>
           </tr>
           {% endif %}
         </tbody>
       </table>
-      
+
 
       <div class="d-none">
         <input id="cdate" type="text" /><br />
@@ -32,12 +40,13 @@
         <input id="cweight" type="text" /><br />
         <input id="ctag" type="text" /><br />
         <input id="ccategories" type="text" /><br />
+        <input id="ccaturl" type="text" /><br />
         <input id="ccol1" type="text" /><br />
         <input id="ccol2" type="text" /><br />
       </div>
-      
+
       <!-- <div class="mb-2"> -->
-        <!-- title:<input id="ctitle" type="text" /><br />
+      <!-- title:<input id="ctitle" type="text" /><br />
         date:<input id="cdate" type="text" /><br />
         draft:<input id="cdraft" type="text" /><br />
         type:<input id="ctype" type="text" /><br />
@@ -46,16 +55,16 @@
         description:<input id="cdescription" type="text" /><br />
         weight:<input id="cweight" type="text" /><br />
         tag:<input id="ctag" type="text" /><br /> -->
-        <!-- <button class="btn__arTitle mb-2" id="title_button">標題修改</button> -->
+      <!-- <button class="btn__arTitle mb-2" id="title_button">標題修改</button> -->
       <!-- </div> -->
-        
+
       <h3 class="h3 column__h3"><i class="fa fa-file-text mr-2 mb-2"></i> 內容編輯</h3>
       <div class="card">
         <div class="card-body">
           <div id="editorjs"></div>
         </div>
       </div>
-      
+
       <div id="editor_block"><span><button class="btn__arTitle mb-2"
             id="add_title_button">+標題</button></span><span><button class="btn__arTitle mb-2"
             id="add_img_button">+圖</button></span><span><button class="btn__arTitle mb-2"

+ 38 - 10
backstage/templates/tables/editor_table.html

@@ -132,13 +132,11 @@
       blocks = parseMd(aa);
       oTitle = $("#ctitle").val();
       // $("#dialog-form").dialog();
-      if($("#ctype").val() == "collection")
-      {
+      if ($("#ctype").val() == "collection") {
         $("#scat").hide();
-        
+
       }
-      else
-      {
+      else {
         $("#sdesc").hide();
       }
       $("#myModal").modal();
@@ -181,7 +179,37 @@
     // location.reload();
   }
   function writeMd() {
-    mdContent = GetMdHeader();
+
+    axios.get('/backstage/utils?trantext=' + $('#ccategories').val()).then(({ data }) => { $('#ccaturl').val(data); })
+      .finally(() => {
+        mdContent = GetMdHeader();
+        mdContent += contentMatters.join("\n");
+        var formData = new FormData();
+        var imagefile = document.querySelector('#cfile');
+        formData.append("image", imagefile.files[0]);
+        axios.post('/backstage/upload/title', formData, {
+          headers: {
+            'Content-Type': 'multipart/form-data'
+          }
+        }).then(({ data }) => {
+          $('#cfile').val("");
+          if (data.success == "1") {
+            mdContent = mdContent.replace($('#cimage').val(), '/img/collection/' + data.file.url.substring(data.file.url.lastIndexOf('/') + 1));
+            //alert(data.file.url.substring(data.file.url.lastIndexOf('/')+1));
+          }
+
+          postData = {
+            content: mdContent,
+            url: editTarget
+          };
+          //console.log(mdContent);
+          axios.post(contentApiUrl + editTarget, json = postData).then(({ data }) => {
+          });
+        }).finally(() => {
+          alert('資料已更新'); // test
+          location.reload();
+        });
+      });
     /*
     mdContent += '---\n';
     mdContent += 'title: "' + $('#ctitle').val() + '"\n';
@@ -203,10 +231,10 @@
     }
     mdContent += '---\n';
     */
-    mdContent += contentMatters.join("\n");
+
     //console.log(contentMatters);
 
-    var formData = new FormData();
+    /* var formData = new FormData();
     var imagefile = document.querySelector('#cfile');
     formData.append("image", imagefile.files[0]);
     axios.post('/backstage/upload/title', formData, {
@@ -230,13 +258,13 @@
     }).finally(() => {
       alert('資料已更新'); // test
       location.reload();
-    });
+    }); */
   }
 
   $(function () {
 
     $("#dialog-form").hide();
-    
+
 
   });
   //allObjs = JSON.parse(htmlDecode('{{ collections }}'));

+ 4 - 2
backstage/utils/routes.py

@@ -1,4 +1,5 @@
-from flask import flash, request, redirect, url_for, Response
+from flask import flash, request, redirect, url_for, Response,Blueprint
+from flask_restful import Resource, Api
 import requests
 from time import sleep
 from backstage.utils import translate
@@ -6,6 +7,8 @@ from collections import defaultdict
 from os import path
 from backstage.config import PORTAL_SERVER
 
+contents_app = Blueprint('contents', __name__)
+api = Api(contents_app)
 TYPE_URL_FOR = {'collection': 'collections.collection_list',
                 'blog': 'blogs.blog_list'}
 
@@ -43,7 +46,6 @@ def get_trans_title_url_name(title):
     return ('_'.join(tary))
     #return translate(title).replace(' ', '_')
 
-
 def update_manage_table(data, elements, req_args, files, img_type):
     def allow_file(filename):
         return '.' in filename and \