瀏覽代碼

修改搜索工具的下载逻辑1

tanghongbin 4 年之前
父節點
當前提交
7f0e7a454a
共有 1 個文件被更改,包括 72 次插入40 次删除
  1. 72 40
      controller/LangArea.py

+ 72 - 40
controller/LangArea.py

@@ -125,14 +125,15 @@ class LangAreaView(TemplateView):
         elif type == 'link_ios' or type == 'link_android':
             langType = (4, 5, 6, 7)
 
-        en_qs = LangKeyModel.objects.filter(langvalmodel__la__id=20, type__in=langType).values('word_key', 'langvalmodel__word_val')
+        en_qs = LangKeyModel.objects.filter(langvalmodel__la__id=20, type__in=langType).values('word_key',
+                                                                                               'langvalmodel__word_val')
         en_kv = {}
         for e in en_qs:
             en_kv[e['word_key']] = e['langvalmodel__word_val']
 
         content = ''
         if type == 'ios' or type == 'link_ios':
-            res_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, type__in=langType).\
+            res_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, type__in=langType). \
                 values('word_key', 'langvalmodel__word_val')
             res = {}
             for r in res_qs:
@@ -145,7 +146,7 @@ class LangAreaView(TemplateView):
                 content_val = res[l].replace('"', '\'')
                 content = content + '"' + l + '"="' + content_val + '";\n'
         elif type == 'android' or type == 'link_android':
-            res_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, type__in=langType).\
+            res_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, type__in=langType). \
                 values('word_key', 'langvalmodel__word_val').order_by('addTime')
             res = {}
             for r in res_qs:
@@ -186,16 +187,15 @@ class LangAreaView(TemplateView):
         elif type == 'avss':
             content = self.do_download_avss(id)
         elif type == 'new_searchTool':
-            content = self.do_download_SearchTool(id,0)
+            content = self.do_download_SearchTool(id, 0)
         elif type == 'old_searchTool':
-            content = self.do_download_SearchTool(id,1)
+            content = self.do_download_SearchTool(id, 1)
         response = StreamingHttpResponse(content)
         response['Content-Type'] = 'application/octet-stream'
         response['Content-Disposition'] = 'attachment;filename="lang.txt"'
         return response
         return response.json(0, res)
 
-
     def do_download_avss(self, id):
         content = ''
         en_result = self.get_area_language(20)
@@ -238,7 +238,8 @@ class LangAreaView(TemplateView):
                     print(message)
                     for location in locations:
                         if location['filename'] != '':
-                            content += '<location filename=\"' + location['filename'] + '\" line=\"' + str(location['line']) + '\"/>\n'
+                            content += '<location filename=\"' + location['filename'] + '\" line=\"' + str(
+                                location['line']) + '\"/>\n'
                     content += '</message>\n'
                 content += '</context>\n'
             content += '</TS>'
@@ -248,15 +249,13 @@ class LangAreaView(TemplateView):
 
         return content
 
-
-
     def get_area_language(self, id):
         result = {}
         lkc_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, type__in=(2, 3, 6, 7)).values('word_key',
-                                                                                                 'langkeyclassmodel__clazz__name',
-                                                                                                 'langvalmodel__word_val',
-                                                                                                 'langlocationmodel__filename',
-                                                                                                 'langlocationmodel__line')
+                                                                                                    'langkeyclassmodel__clazz__name',
+                                                                                                    'langvalmodel__word_val',
+                                                                                                    'langlocationmodel__filename',
+                                                                                                    'langlocationmodel__line')
         for lkc in lkc_qs:
             # print(lkc)
             name = lkc['langkeyclassmodel__clazz__name']
@@ -279,6 +278,35 @@ class LangAreaView(TemplateView):
         print(result)
         return result
 
+    def get_area_language_search(self, id):
+        result = {}
+        lkc_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, project__id=4).values('word_key',
+                                                                             'searchtoolkeymodel__bk__name',
+                                                                             'langvalmodel__word_val',
+                                                                             'langlocationmodel__filename',
+                                                                             'langlocationmodel__line')
+        for lkc in lkc_qs:
+            # print(lkc)
+            name = lkc['searchtoolkeymodel__bk__name']
+            hasClass = result.__contains__(name)
+            if hasClass is False:
+                result[name] = {}
+
+            source = lkc['word_key']
+            if not result[name].__contains__(source):
+                result[name][source] = {}
+            message = result[name][source]
+
+            message['source'] = source[(len(name) + 1):]
+            message['translation'] = lkc['langvalmodel__word_val']
+
+            # location
+            locations = []
+            locations.append({'filename': lkc['langlocationmodel__filename'], 'line': lkc['langlocationmodel__line']})
+            message['locations'] = locations
+        print(result)
+        return result
+
     def get_avss_language(self, id):
         if id == 18:  # 简体中文
             return 'zh_CN'
@@ -313,46 +341,49 @@ class LangAreaView(TemplateView):
 
     def do_download_SearchTool(self, id, searchtype):
         content = ''
-        en_result = self.get_area_language(20)
-        block_qs = SearchToolBlock.objects.filter(type__contains=searchtype).values('name', 'id')
-
-        skm_qs = SearchToolBlock.objects.filter(type__contains=searchtype).values('name', 'id',
-                                                                                    "searchtoolkeymodel__lk__id")
-        if block_qs:
+        en_result = self.get_area_language_search(20)
+        print(en_result)
+        target_result = self.get_area_language_search(id)
+        if target_result:
             content += '<TS version=\"2.1\" language=\"' + self.get_avss_language(int(id)) + '\">\n'
-
-            k_list = []
-            for k in skm_qs:
-                k_list.append(k['searchtoolkeymodel__lk__id'])
-
-            key_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, id__in=k_list).values('id', 'word_key', 'langvalmodel__word_val')
-
-            loc_qs = LangLocationModel.objects.filter(lk__id__in=k_list).values('id', 'filename', 'line', 'lk__id',
-                                                                                   'lk__word_key', 'lk__updTime')
-
-            for bk in block_qs:
+            # keys = result.keys()
+            keys = en_result.keys()
+            for key in keys:
                 content += '<context>\n<name>'
-                content += bk['name']
+                content += key
                 content += '</name>\n'
 
-                for lk in key_qs:
+                messages = None
+                if target_result.__contains__(key):
+                    messages = target_result[key]
+                else:
+                    messages = en_result[key]
+                messageKeys = en_result[key].keys()
+                for messageKey in messageKeys:
+                    message = None
+                    if messages.__contains__(messageKey):
+                        message = messages[messageKey]
+                    else:
+                        message = en_result[key][messageKey]
+
+                    # message = messages[messageKey]
                     content += '<message>\n'
-                    for loc in loc_qs:
-                        if loc['lk__id'] == lk['id'] :
-                            if loc['filename'] != '':
-                                content += '<location filename=\"' + loc['filename'] + '\" line=\"' + str(
-                                    loc['line']) + '\"/>\n'
-                    word_key = lk['word_key']
                     content += '<source>'
-                    content += word_key[(len(bk['name']) + 1):]
+                    content += message['source']
                     content += '</source>\n'
                     content += '<translation>'
-                    translation = lk['langvalmodel__word_val']
+                    translation = message['translation']
                     # if translation is None or translation == '':
                     #     content += '23333'
                     # else:
                     content += translation
                     content += '</translation>\n'
+                    locations = message['locations']
+                    print(message)
+                    for location in locations:
+                        if location['filename'] != '':
+                            content += '<location filename=\"' + location['filename'] + '\" line=\"' + str(
+                                location['line']) + '\"/>\n'
                     content += '</message>\n'
                 content += '</context>\n'
             content += '</TS>'
@@ -362,6 +393,7 @@ class LangAreaView(TemplateView):
 
         return content
 
+
 class exportLangView(TemplateView):
     def post(self, request, *args, **kwargs):
         request.encoding = 'utf-8'