husong %!s(int64=7) %!d(string=hai) anos
pai
achega
b4ffc9ac02

BIN=BIN
common/.DS_Store


A diferenza do arquivo foi suprimida porque é demasiado grande
+ 50 - 1
common/common.py


BIN=BIN
common/common.pyc


BIN=BIN
common/img/1511160453.png


BIN=BIN
common/img/1511160491.png


BIN=BIN
common/img/1511160653.png


BIN=BIN
common/img/1511160685.png


BIN=BIN
common/img/1511160691.png


BIN=BIN
common/img/1test.png


+ 170 - 0
common/upload_to_oss.py

@@ -0,0 +1,170 @@
+# -*- coding: utf-8 -*-
+import os
+import oss2
+import requests
+import uuid
+
+# from common import convert_to_pdf
+
+
+class MyOSS:
+    def __init__(self):
+        self.AccessKeyID = 'LTAIPp6YI1JUzpz2'
+        self.AccessKeySecret = 'UWc04c4TpUeVot9cYqbkRpU4YhE25S'
+        self.bucket_name = 'tederen'
+        self.root_name = 'xdcjing'
+        self.auth = oss2.Auth(self.AccessKeyID, self.AccessKeySecret)
+        self.endpoint = 'http://oss-cn-shanghai.aliyuncs.com'
+        self.bucket = oss2.Bucket(self.auth, self.endpoint, self.bucket_name)
+        self.domain = 'http://tederen.oss-cn-shanghai.aliyuncs.com'
+
+    def upload_from_str(self,content_str=None,filename=None):
+        '''
+        通过字符串上传
+        byte,unicode,str
+        '''
+        filename = os.path.join(self.root_name,filename)
+
+        result = self.bucket.put_object(filename,content_str)
+
+        if result.status == 200:
+            url = os.path.join(self.domain,filename)
+        else:
+            url = ''
+
+        return url
+
+    def upload_from_local(self, localfile=None, ossfile=None):
+        '''
+        上传本地文件到oss
+        '''
+        ossfile = os.path.join(self.root_name, ossfile)
+
+        with open(localfile, 'rb') as fileobj:
+            result = self.bucket.put_object(ossfile, fileobj)
+
+        if result.status == 200:
+            url = os.path.join(self.domain, ossfile)
+        else:
+            url = ''
+
+        return url
+
+    def resumable_upload_from_local(self, localfile=None, ossfile=None):
+        ossfile = os.path.join(self.root_name, ossfile)
+
+        result = oss2.resumable_upload(self.bucket,ossfile,localfile,
+            store=oss2.ResumableStore(root='/tmp'), 
+            multipart_threshold=100*1024,
+            part_size=100*1024,
+            num_threads=4,
+            progress_callback=self.progress_callback
+        )
+
+        if result.status == 200:
+            url = os.path.join(self.domain,ossfile)
+        else:
+            url = ''
+
+        return url
+
+    def upload_from_url(self, url=None, ossfile=None):
+        '''
+        '''
+        ossfile = os.path.join(self.root_name,ossfile)
+        
+        resp = requests.get(url)
+
+        result = self.bucket.put_object(ossfile,resp)
+
+        if result.status == 200:
+            url = os.path.join(self.domain,ossfile)
+        else:
+            url = ''
+
+        return url
+
+    def upload_from_ajax(self, request, path_name, key):
+        """
+        上传文件
+        :param request: 请求
+        :param path_name: 要存放到服务器上的路径
+        :param key: 从request取文件的form的name
+        :return: dict
+        """
+        file_obj = request.FILES.get(key, None)
+        if file_obj:
+            file_name = file_obj.name.split(".")[0]
+            file_type = file_obj.name.split(".")[-1]
+            file_size = file_obj.size
+            origin_file_name = path_name + str(uuid.uuid1()) + '.' + file_type
+            pdf_file_name = path_name + str(uuid.uuid1()) + '.pdf'
+
+            # 把文件写入tmp,上传到server后,删除本地
+            tmp_file_name = os.path.join("/tmp", file_obj.name)
+            print "tmp_file_name --> ", tmp_file_name
+
+            with open(tmp_file_name, "wb+") as fobj:
+                for chunk in file_obj.chunks():
+                    fobj.write(chunk)
+            file_url = MyOSS().resumable_upload_from_local(tmp_file_name,
+                                                           origin_file_name)
+
+            result = {"name": file_name, "type": file_type,
+                      "size": file_size, "url": file_url}
+
+            # 只有资源、任务才进行转换
+            if key in ["resource", "task", "url"]:
+                res = convert_to_pdf(tmp_file_name)
+                print "convert result: -->", res
+
+                # 转换为pdf,再次上传到server
+                tmp_pdf_name = os.path.join("/tmp", file_name + ".pdf")
+                print "tmp_pdf_name --> ", tmp_pdf_name
+
+                if res:
+                    pdf_file_url = MyOSS().resumable_upload_from_local(tmp_pdf_name,
+                                                                       pdf_file_name)
+                    if pdf_file_url:
+                        result["pdf_url"] = pdf_file_url
+                        os.remove(tmp_pdf_name)
+
+            # 删除源文件
+            if file_url:
+                os.remove(tmp_file_name)
+
+            print "upload_file_info: --> ", result
+        else:
+            result = {}
+
+        return result
+
+    def progress_callback(self, consumed_bytes, total_bytes):
+        if total_bytes:
+            rate = int(100*float(consumed_bytes)/float(total_bytes))
+            if rate % 50 == 0:
+                print '\r{0}%'.format(rate)
+
+
+        
+
+
+
+if __name__ == '__main__':
+    myoss = MyOSS()
+    #url = myoss.upload_from_str('test','test/test.txt')
+    #print url
+
+    #localfile = 'localtest.txt'
+    #ossfile = 'localtooss/text.txt'
+    #url = myoss.upload_from_local(localfile,ossfile)
+    #print url
+
+    #ossfile = 'test/test.jpg'
+    #url = myoss.upload_from_url('http://img.ph.126.net/ocT0cPlMSiTs2BgbZ8bHFw==/631348372762626203.jpg',ossfile)
+    #print url
+
+    localfile = 'test.mp4'
+    ossfile = 'localtooss/test.mp4'
+    url = myoss.resumable_upload_from_local(localfile,ossfile)
+    print url

BIN=BIN
common/upload_to_oss.pyc


+ 22 - 1
common/views.py

@@ -1,3 +1,24 @@
-from django.shortcuts import render
+# -*-coding:utf-8 -*-
+import traceback
+from django.views.decorators.csrf import csrf_exempt
 
+from common import JsonResponse,params,save_bs64img_local
 # Create your views here.
+
+@csrf_exempt
+@params
+def upload_img(request):
+    req_params = request.PARAMS
+    if req_params:
+        try:
+            img_data = req_params.get("img_data")
+            img_url = save_bs64img_local(img_data)
+
+            if img_url:
+                return JsonResponse({"code": 0, "img_url":img_url})
+            else:
+                return JsonResponse({"code": -1,"msg":u"图片上传失败!"})
+        except:
+            traceback.print_exc()
+    else:
+        return JsonResponse({"code": -1, "msg": u"缺少参数"})

BIN=BIN
common/views.pyc


+ 24 - 0
content_manage/migrations/0007_auto_20171117_1117.py

@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('content_manage', '0006_news'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='banner',
+            name='img_url',
+            field=models.URLField(default=b'', max_length=500, null=True, verbose_name='\u56fe\u7247url'),
+        ),
+        migrations.AlterField(
+            model_name='news',
+            name='img_url',
+            field=models.URLField(default=b'', max_length=500, null=True, verbose_name='\u56fe\u7247url'),
+        ),
+    ]

BIN=BIN
content_manage/migrations/0007_auto_20171117_1117.pyc


+ 19 - 0
content_manage/migrations/0008_auto_20171120_0146.py

@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('content_manage', '0007_auto_20171117_1117'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='article',
+            name='img_url',
+            field=models.URLField(default=b'', max_length=500, null=True, verbose_name='\u56fe\u7247url'),
+        ),
+    ]

BIN=BIN
content_manage/migrations/0008_auto_20171120_0146.pyc


+ 3 - 3
content_manage/models.py

@@ -25,7 +25,7 @@ class Article(models.Model):
     )
 
     title = models.CharField(u"文章标题", null= True,max_length=128, help_text=u"不超过64个字")
-    img_url = models.URLField(u"图片url", null=True, default="")
+    img_url = models.URLField(u"图片url", null=True, default="",max_length=500)
     type = models.SmallIntegerField(u"文章分类", default=0, choices=COMMENT_TYPE)
     content = models.TextField(u"文章内容",default="", blank=True,null=True)
 
@@ -36,7 +36,7 @@ class Banner(models.Model):
     横幅内容表
     """
     position = models.IntegerField(u"横幅位置",default=1)
-    img_url = models.URLField(u"图片url", null=True, default="")
+    img_url = models.URLField(u"图片url", null=True, default="",max_length=500)
     display_title = models.CharField(u"展示标题",max_length=128, help_text=u"不超过64个字")
     article_url = models.URLField(u"文章url", null=True, default="")
     article_title = models.CharField(u"文章标题",max_length=128, help_text=u"不超过64个字")
@@ -50,7 +50,7 @@ class News(models.Model):
     横幅内容表
     """
     title = models.CharField(u"文章标题",max_length=128, help_text=u"不超过64个字")
-    img_url = models.URLField(u"图片url", null=True, default="")
+    img_url = models.URLField(u"图片url", null=True, default="",max_length=500)
     author = models.CharField(u"文章作者", max_length=32, blank=True,null=True,default="")
     content = models.TextField(u"文章内容",default="", blank=True,null=True)
     create_time = models.DateField(auto_now_add=True)

BIN=BIN
content_manage/models.pyc


+ 55 - 17
content_manage/views.py

@@ -1,7 +1,7 @@
 # -*-coding:utf-8 -*-
 import traceback
 from common.common import (get_user_from_request, login_required,
-                           params,JsonResponse)
+                           params,JsonResponse,save_bs64img_local)
 from django.views.decorators.csrf import csrf_exempt
 from models import *
 # Create your views here.
@@ -27,9 +27,9 @@ def content_show(request,offset):
         dict_data["img_url"]=content_data.img_url
         dict_data["type"]=content_data.type
         dict_data["content"]=content_data.content
-        res = {"code":0,"page_type":offset,"msg":"show sucess!","content_data":dict_data}
+        res = {"code":0,"msg":"show sucess!","content_data":dict_data}
     else:
-        res = {"code":-1,"page_type":offset,"msg":"show false!",}
+        res = {"code":-1,"msg":"get empty data!",}
 
     return JsonResponse(res)
 
@@ -76,9 +76,17 @@ def banner_show(request):
         for obj_banner in objs:
             dict_data={}
             dict_data["bid"] = obj_banner.id
+            dict_data["position"] = obj_banner.position
+            dict_data["img_url"] = obj_banner.img_url
             dict_data["display_title"] = obj_banner.display_title
+            dict_data["article_title"] = obj_banner.article_title
+            dict_data["article_url"] = obj_banner.article_url
+            
+            dict_data["author"] = obj_banner.author
+            dict_data["content"] = obj_banner.content
             dict_data["create_time"] = obj_banner.create_time
-            dict_data["position"] = obj_banner.position
+
+            # print(obj_banner.img_url)
             data_list.append(dict_data)
         res = {"code":0,"msg":"get data sucess!","content_data":data_list}
     else:
@@ -95,7 +103,7 @@ def banner_add(request):
     req_params = request.PARAMS
     if req_params:
         try:
-            position = req_params.get("position",None)
+            position = req_params.get("position")
             img_url = req_params.get("img_url",None)
             display_title = req_params.get("display_title",None)
             article_url = req_params.get("article_url",None)
@@ -103,9 +111,18 @@ def banner_add(request):
             author = req_params.get("author",None)
             content = req_params.get("content",None)
 
+            # img_path = save_bs64img_local(img_url)
+            # print("banner_edit*****_img_",img_path)
+
+            if position!=0 and Banner.objects.filter(position=position):
+                return JsonResponse({"code": -1, "msg": "横幅位置重复!"})
+
             obj= Banner.objects.create(position=position,img_url=img_url,display_title=display_title,
                 article_url=article_url,article_title=article_title,author=author,content=content)
-            res = res={"code": 0, "msg": "banner add sucess"}
+            if obj:
+                res = {"code": 0, "msg": "banner add sucess"}
+            else:
+                res = {"code": -1, "msg": "添加失败"}
         except:
             traceback.print_exc()
     else:
@@ -123,7 +140,7 @@ def banner_edit(request):
     if req_params:
         try:
             bid = req_params.get("bid")
-            position = req_params.get("position",None)
+            position = int(req_params.get("position",None))
             img_url = req_params.get("img_url",None)
             display_title = req_params.get("display_title",None)
             article_url = req_params.get("article_url",None)
@@ -131,9 +148,27 @@ def banner_edit(request):
             author = req_params.get("author",None)
             content = req_params.get("content",None)
 
-            obj= Banner.objects.filter(id=bid)
+            print("banner_edit*****_img_url",img_url)
+            # img_path = save_bs64img_local(img_url)
+            # print("banner_edit*****_img_",img_path)
+
+            # if position!=0 and Banner.objects.filter(position=position):
+                # return JsonResponse({"code": -1, "msg": "横幅位置重复!"})
+
+            obj= Banner.objects.filter(id=bid).first()
             if obj:
-                obj.id = bid
+                # 处理横幅位置重复,0 表示未展示
+                if position!=0 and obj.position!=position:
+                    obj_postions=Banner.objects.all()
+                    list_postion =[]
+                    for ob in obj_postions:
+                        list_postion.append(int(ob.position))
+                    print("00000000",position)
+                    print("11111111",list_postion)
+                    if position in list_postion:
+                        print("333333")
+                        return JsonResponse({"code": -1, "msg": "横幅位置重复!"})
+
                 obj.position = position
                 obj.img_url = img_url
                 obj.display_title = display_title
@@ -141,9 +176,9 @@ def banner_edit(request):
                 obj.author = author
                 obj.content = content
                 obj.save()
-                res = {"code": 0, "msg": "banner add sucess"}
+                res = {"code": 0, "msg": "banner edit sucess"}
             else:
-                res = {"code": -1, "msg": "banner add fail"}
+                res = {"code": -1, "msg": "banner edit fail"}
         except:
             traceback.print_exc()
     else:
@@ -194,7 +229,7 @@ def news_show(request):
             data_list.append(dict_data)
         res = {"code":0,"msg":"show sucess!","content_data":data_list}
     else:
-        res = {"code":-1,"msg":"no data",}
+        res = {"code":-1,"msg":"no data"}
 
     return JsonResponse(res)
 
@@ -209,12 +244,14 @@ def news_add(request):
     if req_params:
         try:
             img_url = req_params.get("img_url",None)
-            title = req_params.get("title",None)
-            author = req_params.get("author",None)
-            content = req_params.get("content",None)
+            title = req_params.get("title")
+            author = req_params.get("author")
+            content = req_params.get("content")
             obj= News.objects.create(title=title,img_url=img_url,author=author,content=content)
-
-            res = res={"code": 0, "msg": "banner add sucess"}
+            if obj:
+                res = {"code": 0, "msg": "news add sucess"}
+            else:
+                res = {"code": -1, "msg": "news add fail"}
         except:
             traceback.print_exc()
     else:
@@ -241,6 +278,7 @@ def news_look(request):
                 dict_data["create_time"] = obj.create_time
                 dict_data["author"] = obj.author
                 dict_data["content"] = obj.content
+                dict_data["img_url"] = obj.img_url
                 print("news_look-----",dict_data)
                 res={"code": 0, "msg": "sucess","look_data":dict_data}
             else:

BIN=BIN
content_manage/views.pyc


+ 35 - 0
users/migrations/0011_auto_20171120_0814.py

@@ -0,0 +1,35 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('users', '0010_auto_20171117_0831'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='Reply',
+            fields=[
+                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+                ('fb_content', models.TextField(default=b'', max_length=500, blank=True, help_text='\u4e0d\u8d85\u8fc7250\u5b57', null=True, verbose_name='\u95ee\u9898\u56de\u590d')),
+            ],
+        ),
+        migrations.RemoveField(
+            model_name='reader',
+            name='fb_content',
+        ),
+        migrations.AddField(
+            model_name='reply',
+            name='fb_id',
+            field=models.ForeignKey(verbose_name='\u5173\u8054\u8bfb\u8005', to='users.Reader', null=True),
+        ),
+        migrations.AddField(
+            model_name='reply',
+            name='u_id',
+            field=models.ForeignKey(verbose_name='\u5173\u8054\u7528\u6237', to='users.Users', null=True),
+        ),
+    ]

BIN=BIN
users/migrations/0011_auto_20171120_0814.pyc


+ 18 - 0
users/migrations/0012_remove_reader_fb_id.py

@@ -0,0 +1,18 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('users', '0011_auto_20171120_0814'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='reader',
+            name='fb_id',
+        ),
+    ]

BIN=BIN
users/migrations/0012_remove_reader_fb_id.pyc


+ 13 - 3
users/models.py

@@ -27,6 +27,16 @@ class Reader(models.Model):
     create_time = models.DateField(auto_now_add=True)
     question = models.TextField(u"问题内容",max_length=500, default="", blank=True,
                              help_text=u"不超过250字")
-    fb_id = models.IntegerField(u"管理者id",blank=True,null=True)
-    fb_content = models.TextField(u"问题回复",max_length=500, null=True,default="", blank=True,
-                             help_text=u"不超过250字")
+    # fb_id = models.IntegerField(u"管理者id",blank=True,null=True)
+    # fb_content = models.TextField(u"问题回复",max_length=500, null=True,default="", blank=True,
+    #                          help_text=u"不超过250字")
+
+
+class Reply(models.Model):
+    """
+    回复内容表
+    """
+    fb_id = models.ForeignKey(Reader, verbose_name=u"关联读者",null = True)
+    u_id = models.ForeignKey(Users, verbose_name=u"关联用户",null = True)
+    fb_content = models.TextField(u"问题回复",max_length=500,default="", blank=True,
+                             help_text=u"不超过250字")

BIN=BIN
users/models.pyc


+ 38 - 25
users/views.py

@@ -266,17 +266,28 @@ def reader_fb(request):
 
     print("00000",search_question)
     if search_question:
-        user_data = Reader.objects.filter(question__icontains = search_question)
+        reader_data = Reader.objects.filter(question__icontains = search_question)
     else:
-        user_data = Reader.objects.all()
+        reader_data = Reader.objects.all()
     data =[]
-    if user_data:
-        for list_data in user_data:
+    if reader_data:
+        for list_data in reader_data:
             dict_data = {}
             dict_data["rid"] = list_data.id
             dict_data["readername"] = list_data.readername
             dict_data["create_time"] = list_data.create_time
             dict_data["question"] = list_data.question
+
+
+            # list_reply = Reply.objects.filter(fb_id = list_data.id).values('u_id','fb_content')
+
+            # for dict_v in list_reply:
+            #     name = Users.objects.filter(id=dict_v['u_id']).first().username
+            #     dict_v['u_id'] = name
+
+            # dict_data["fb_content"] = list_reply
+            # print list_reply
+
             data.append(dict_data)
         res = {"code": 0, "msg": "get data success", "reader_data": data}
 
@@ -289,7 +300,7 @@ def reader_fb(request):
 
 
 @csrf_exempt
-@login_required
+# @login_required
 @params
 def fb_question(request):
     """
@@ -298,26 +309,28 @@ def fb_question(request):
     :return: True / False
     """
     
-    user_id = -1
-    user = get_user_from_request(request)
-    if user:
-        user_id = user["uid"]
-    else:
-        return JsonResponse({"code":-1, "msg":"没登陆"})
+    user_id = 3
+    # user = get_user_from_request(request)
+    # if user:
+    #     user_id = user["uid"]
+    # else:
+    #     return JsonResponse({"code":-1, "msg":"没登陆"})
     req_params = request.PARAMS
-    rid = req_params.get("rid")
-    fb_content = req_params.get("fb_content", None)
-
-    reader_obj = Reader.objects.filter(id=rid).first()
-    print("update-----",reader_obj)
+    if req_params:
+        try:
+            rid = req_params.get("rid")
+            fb_content = req_params.get("fb_content")
+
+            # reader_obj = Reader.objects.filter(id=rid).first()
+            obj= Reply.objects.create(fb_id_id= rid,u_id_id = user_id,fb_content = fb_content)
+            # print("update-----",reader_obj)
+            if obj:
+                print(obj)
+                return JsonResponse({"code":0, "msg":"success", "fb_content":fb_content})
+            else:
+                return JsonResponse({"code":-1, "msg":"回复失败"})
+        except:
+            traceback.print_exc()
 
-    if reader_obj:
-        reader_obj.fb_content = fb_content
-        reader_obj.fb_id = user_id
-        reader_obj.save()
-        res = {"code":0, "msg":"success", "fb_content":fb_content}
     else:
-        res = {"code":-1, "msg":"回复失败"}
-
-    print(res)
-    return JsonResponse(res)
+        return JsonResponse({"code":-1, "msg":"参数不完整"})

BIN=BIN
users/views.pyc


+ 4 - 0
xdcjing/settings.py

@@ -61,6 +61,9 @@ MIDDLEWARE_CLASSES = (
 )
 
 
+'''
+配置允许跨域请求
+'''
 CORS_ALLOW_CREDENTIALS = True
 CORS_ORIGIN_ALLOW_ALL = True
 CORS_ORIGIN_WHITELIST = (
@@ -93,6 +96,7 @@ CORS_ALLOW_HEADERS = (
 
 
 
+
 ROOT_URLCONF = 'xdcjing.urls'
 
 TEMPLATES = [

BIN=BIN
xdcjing/settings.pyc


+ 10 - 1
xdcjing/urls.py

@@ -2,13 +2,14 @@ from django.conf.urls import include, url
 from django.contrib import admin
 import users.views
 import content_manage.views
+import common.views
 
 urlpatterns = [
     # Examples:
     # url(r'^$', 'xdcjing.views.home', name='home'),
     # url(r'^blog/', include('blog.urls')),
 
-    url(r'^admin/$', include(admin.site.urls)),
+    # url(r'^admin/$', include(admin.site.urls)),
     url(r'^login/$', users.views.login),
     url(r'^logout/$', users.views.logout),
     url(r'^adduser/$', users.views.adduser),
@@ -16,16 +17,24 @@ urlpatterns = [
     url(r'^deleteuser/$', users.views.delete_user),
     url(r'^modifiedpassword/$', users.views.modified_password),
     url(r'^usermanage/$', users.views.user_manage),
+
     url(r'^reader_fb/$', users.views.reader_fb),
     url(r'^fb_question/$', users.views.fb_question),
+
     url(r'^content_show-([0,1,2,3,4,5])/$', content_manage.views.content_show),
     url(r'^content_edit-([0,1,2,3,4,5])/$', content_manage.views.content_edit),
+
     url(r'^banner_show/$', content_manage.views.banner_show),
     url(r'^banner_add/$', content_manage.views.banner_add),
     url(r'^banner_edit/$', content_manage.views.banner_edit),
+    url(r'^banner_delete/$', content_manage.views.banner_delete),
+
     url(r'^news_show/$', content_manage.views.news_show),
     url(r'^news_add/$', content_manage.views.news_add),
     url(r'^news_look/$', content_manage.views.news_look),
     url(r'^news_delete/$', content_manage.views.news_delete),
 
+    url(r'^upload_img/$', common.views.upload_img),
+
+
 ]

BIN=BIN
xdcjing/urls.pyc