Prechádzať zdrojové kódy

OTA功能添加限制条件

tanghongbin 4 rokov pred
rodič
commit
e1d3980683

+ 18 - 18
Ansjer/local_settings.py

@@ -73,15 +73,15 @@ TEMPLATES = [
 WSGI_APPLICATION = 'Ansjer.local_wsgi.application'
 
 # 服务器类型
-DATABASE_DATA = 'ansjerlocal'
-SERVER_HOST = '127.0.0.1'
-DATABASES_USER = 'root'
-DATABASES_PASS = '123456'
+DATABASE_DATA = 'AnsjerLocal'
+SERVER_HOST = '192.168.136.99'
+DATABASES_USER = 'ansjer'
+DATABASES_PASS = 'ansjer.x.x'
 
-DATABASE_DATA2 = 'asjl'
-SERVER_HOST2 = '127.0.0.1'
-DATABASES_USER2 = 'root'
-DATABASES_PASS2 = '123456'
+# DATABASE_DATA2 = 'asjl'
+# SERVER_HOST2 = '127.0.0.1'
+# DATABASES_USER2 = 'root'
+# DATABASES_PASS2 = '123456'
 
 DATABASES = {
     'default': {
@@ -94,16 +94,16 @@ DATABASES = {
         'OPTIONS': {'charset': 'utf8mb4', 'use_unicode': True, 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"},
         'AUTOCOMMIT': True
     },
-    'mysql02': {
-        'ENGINE': 'django.db.backends.mysql',
-        'NAME': DATABASE_DATA2,
-        'USER': DATABASES_USER2,
-        'PASSWORD': DATABASES_PASS2,
-        'HOST': SERVER_HOST2,
-        'PORT': '3306',
-        'OPTIONS': {'charset': 'utf8mb4', 'use_unicode': True, 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"},
-        'AUTOCOMMIT': True
-    }
+    # 'mysql02': {
+    #     'ENGINE': 'django.db.backends.mysql',
+    #     'NAME': DATABASE_DATA2,
+    #     'USER': DATABASES_USER2,
+    #     'PASSWORD': DATABASES_PASS2,
+    #     'HOST': SERVER_HOST2,
+    #     'PORT': '3306',
+    #     'OPTIONS': {'charset': 'utf8mb4', 'use_unicode': True, 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"},
+    #     'AUTOCOMMIT': True
+    # }
 }
 DATABASE_ROUTERS = ['Ansjer.database_router.DatabaseAppsRouter']
 DATABASE_APPS_MAPPING = {

+ 2 - 1
Ansjer/urls.py

@@ -10,7 +10,7 @@ from Controller import FeedBack, EquipmentOTA, EquipmentInfo, AdminManage, AppIn
     StsOssController, UIDPreview, OssCrd, SysMsg, UidUser, EquipmentManagerV2, EquipmentManagerV3, PushDeploy, \
     AppSetController, \
     ApplicationController, UserExController, CloudStorage, TestApi, UserBrandControllerV2, \
-    StatisticsController, Alexa, FAQController, AppLogController
+    StatisticsController, Alexa, FAQController, AppLogController, EquipmentVersionLimit
 
 urlpatterns = [
     url(r'^testApi/(?P<operation>.*)$', TestApi.testView.as_view()),
@@ -236,6 +236,7 @@ urlpatterns = [
     # 本地登录接口
     url(r'^local/(?P<operation>.*)$', UserController.LocalUserView.as_view()),
     url(r'^account/updateUserCountry', UserController.updateUserCountry),
+    url(r'^equipmentVersionLimit/(?P<operation>.*)$', EquipmentVersionLimit.EquipmentVersionLimitView.as_view()),
 
 
     # app 设备消息模板

+ 110 - 0
Controller/EquipmentVersionLimit.py

@@ -0,0 +1,110 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+import time
+
+from django.views import View
+
+from Model.models import EquipmentVersionLimitModel, UidSetModel
+from Object.RedisObject import RedisObject
+from Object.ResponseObject import ResponseObject
+from Object.TokenObject import TokenObject
+
+
+class EquipmentVersionLimitView(View):
+
+    def get(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation', None)
+        request_dict = request.GET
+        return self.validate(request_dict, operation)
+
+    def post(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation', None)
+        request_dict = request.POST
+        return self.validate(request_dict, operation)
+
+    def validate(self, request_dict, operation):
+        token = request_dict.get('token', None)
+        token = TokenObject(token)
+        print('ind')
+        response = ResponseObject()
+        if token.code != 0:
+            return response.json(token.code)
+
+        if operation == 'add':
+            return self.do_add(request_dict, response)
+        elif operation == 'update':
+            return self.do_update(request_dict, response)
+        elif operation == 'delete':
+            return self.do_delete(request_dict, response)
+        elif operation == 'uid':
+            return self.do_query(request_dict, response)
+        else:
+            return response.json(404)
+
+    def do_add(self, request_dict, response):
+        type = request_dict.get('type', None)
+        content = request_dict.get('content', None)
+        eid = request_dict.get('eid', None)
+
+        if type and content and eid:
+            now_time = int(time.time())
+            data = EquipmentVersionLimitModel()
+            data.type = type
+            data.content = content
+            data.equipment_version_id = eid
+            data.status = 1
+            data.add_time = now_time
+            data.update_time = now_time
+            data.save()
+            return response.json(0)
+        else:
+            return response.json(444)
+
+    def do_update(self, request_dict, response):
+        print('in ')
+        id = request_dict.get('id', None)
+        content = request_dict.get('content', None)
+        status = request_dict.get('status', None)
+
+        if id is None:
+            return response.json(444)
+
+        update = {}
+        if content:
+            update['content'] = content
+
+        if status:
+            update['status'] = status
+
+        evl_qs = EquipmentVersionLimitModel.objects.filter(id=id)
+        if evl_qs.exists():
+            eid = evl_qs[0].equipment_version_id
+            evl_qs.update(**update)
+            redisObject = RedisObject()
+            redisObject.del_data(key='limit_{eid}'.format(eid=eid))
+        return response.json(0)
+
+    def do_delete(self, request_dict, response):
+        id = request_dict.get('id', None)
+
+        if id is None:
+            return response.json(0)
+
+        evl_qs = EquipmentVersionLimitModel.objects.filter(id=id)
+        if evl_qs.exists():
+            eid = evl_qs[0].equipment_version_id
+            redisObject = RedisObject()
+            redisObject.del_data(key='limit_{eid}'.format(eid=eid))
+            evl_qs.delete()
+        return response.json(0)
+
+    def do_query(self, request_dict, response):
+        uid_qs = UidSetModel.objects.filter()[0:5000].values('uid')
+        data = []
+        for uid in uid_qs:
+            data.append(uid['uid'])
+            data.append(uid['uid'])
+            data.append(uid['uid'])
+        return response.json(0, data)

+ 33 - 2
Controller/OTAEquipment.py

@@ -14,8 +14,9 @@ from django.views.generic import TemplateView
 from Ansjer.config import BASE_DIR
 from Ansjer.config import SERVER_DOMAIN
 from Ansjer.config import SERVER_TYPE
-from Model.models import Device_User
+from Model.models import Device_User, EquipmentVersionLimitModel, CountryIPModel
 from Model.models import Equipment_Version
+from Object.RedisObject import RedisObject
 from Object.ResponseObject import ResponseObject
 from Object.TokenObject import TokenObject
 from Object.UrlTokenObject import UrlTokenObject
@@ -476,6 +477,7 @@ def getNewVerInterface(request):
     token = request_dict.get('token', None)
     lang = request_dict.get('lang', None)
     now_ver = request_dict.get('ver', None)
+    uid = request_dict.get('uid', None)
     if not code or not now_ver:
         return response.json(902, {'param':'code,ver'})
         # return response.json(444, 'code,ver')
@@ -490,9 +492,38 @@ def getNewVerInterface(request):
         equipmentValid = Equipment_Version.objects.filter(code=code, status=1, lang='en').order_by(
             '-data_joined')
 
-    print(equipmentValid)
     if equipmentValid.exists():
         equipment = equipmentValid[0]
+        redisObject = RedisObject()
+        key = 'limit_{eid}'.format(eid=equipment.eid)
+
+        evl_qs = redisObject.get_data(key=key)
+        if evl_qs:
+            evl_qs = json.loads(evl_qs)
+        else:
+            evl_qs = EquipmentVersionLimitModel.objects.filter(equipment_version_id=equipment.eid, status=1).values()
+            if evl_qs.exists():
+                redisObject.set_data(key=key, val=json.dumps(list(evl_qs.values())), expire=600)
+        if evl_qs and len(evl_qs) > 0:
+            evl = evl_qs[0]
+            if evl['type'] == 1: # uid限制
+                uids = json.loads(evl['content'])
+                if not uids.__contains__(uid):
+                    return response.json(902)
+            elif evl['type'] == 2: # user限制
+                users = json.loads(evl['content'])
+                if not users.__contains__(tko.userID):
+                    return response.json(902)
+            elif evl['type'] == 3: # 国家地区限制
+                countries = json.loads(evl['content'])
+                country_ip_qs = CountryIPModel.objects.filter(user_ex__userID=tko.userID)
+                if country_ip_qs.exists():
+                    country = country_ip_qs[0].country
+                else:
+                    country = CommonService.getAddr(CommonService.get_ip_address(request))
+                if not countries.__contains__(country):
+                    return response.json(902)
+
         file_path = equipment.filePath
         ver = equipment.softwareVersion
         max_ver = equipment.max_ver

+ 6 - 8
Controller/UserController.py

@@ -3630,22 +3630,20 @@ def delete_local_account(username):
 
 
 def updateUserCountry(request):
-    country_ip_qs = CountryIPModel.objects.filter()
+    country_ip_qs = CountryIPModel.objects.filter(status=0)[0: 100]
     if country_ip_qs.exists():
-        country_ip_qs = country_ip_qs[0: 1000]
-        ids = []
+        country_ip_qs = country_ip_qs.values()
         redisObject = RedisObject(db=6)
         for country_ip in country_ip_qs:
-            key = 'ip_country_{ip}'.format(ip=country_ip.ip)
+            key = 'ip_country_{ip}'.format(ip=country_ip['ip'])
             data = redisObject.get_data(key=key)
             if data:
                 country = data
             else:
-                country = CommonService.getAddr(country_ip.ip)
+                country = CommonService.getAddr(country_ip['ip'])
                 redisObject.set_data(key=key, val=country, expire=3600)
 
-            UserExModel.objects.filter(id=country_ip.user_ex_id).update(country=country)
-            ids.append(country_ip.id)
-        CountryIPModel.objects.filter(id__in=tuple(ids)).delete()
+            country_ip['country'] = country
+            CountryIPModel.objects.filter(id=country_ip['id']).update(country=country, status=1)
     response = ResponseObject()
     return response.json(0)

+ 14 - 1
Model/models.py

@@ -751,7 +751,6 @@ class UserExModel(models.Model):
     userID = models.ForeignKey(Device_User, to_field='userID', on_delete=models.CASCADE)
     appBundleId = models.CharField(default='', max_length=32, verbose_name=u'appID')
     region = models.CharField(default='', max_length=16, verbose_name='区域语言')
-    country = models.CharField(default='', max_length=100, verbose_name='国家名称')
     addTime = models.IntegerField(verbose_name='添加时间', default=0)
     updTime = models.IntegerField(verbose_name='更新时间', default=0)
 
@@ -1017,6 +1016,8 @@ class CountryIPModel(models.Model):
     id = models.AutoField(primary_key=True)
     ip = models.CharField(default='', max_length=32, verbose_name='ip')
     user_ex = models.ForeignKey(UserExModel, to_field='id', on_delete=models.CASCADE, verbose_name='关联用户扩展信息表id')
+    country = models.CharField(default='', max_length=100, verbose_name='国家')
+    status = models.SmallIntegerField(default=0, verbose_name='是否已经查找,0:否,1:是')
     add_time = models.IntegerField(default=0, verbose_name='添加时间')
 
     class Meta:
@@ -1025,4 +1026,16 @@ class CountryIPModel(models.Model):
         verbose_name_plural = verbose_name
 
 
+class EquipmentVersionLimitModel(models.Model):
+    id = models.AutoField(primary_key=True)
+    type = models.SmallIntegerField(default=0, verbose_name='限制类型')
+    content = models.TextField(default='', verbose_name='限制内容')
+    equipment_version = models.ForeignKey(Equipment_Version, to_field='eid', on_delete=models.CASCADE, verbose_name='关联设备版本信息id')
+    status = models.SmallIntegerField(default=0, verbose_name='是否启用。0:不启用,1:启用')
+    add_time = models.IntegerField(default=0, verbose_name='添加时间')
+    update_time = models.IntegerField(default=0, verbose_name='更新时间')
 
+    class Meta:
+        db_table = 'equipment_version_limit'
+        verbose_name = '设备版本信息限制表'
+        verbose_name_plural = verbose_name