|
@@ -11,7 +11,8 @@ from utils.exceptions import CustomError
|
|
|
from apps.foundation.models import BizLog, Config
|
|
|
from apps.account import tenant_log
|
|
|
from apps.images.models import Images
|
|
|
-from apps.store.models import Shop
|
|
|
+from apps.store.models import Shop, StoreBrand
|
|
|
+from apps.vehicle.models import Brand
|
|
|
from apps.store.filters import ShopFilter
|
|
|
from .serializers import ShopSerializer
|
|
|
from hashlib import md5
|
|
@@ -27,14 +28,12 @@ class ShopViewSet(CustomModelViewSet):
|
|
|
f = ShopFilter(self.request.GET, queryset=queryset)
|
|
|
return f.qs
|
|
|
|
|
|
- @permission_required('shop.add_shop')
|
|
|
def perform_create(self, serializer):
|
|
|
super(ShopViewSet, self).perform_create(serializer)
|
|
|
instance = serializer.instance
|
|
|
validated_data = serializer.validated_data
|
|
|
tenant_log(self.request.user, BizLog.INSERT, u'添加门店[%s],id=%d' % (instance.name, instance.id),validated_data)
|
|
|
|
|
|
- @permission_required('shop.delete_shop')
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
with transaction.atomic():
|
|
|
instance = self.get_object()
|
|
@@ -46,6 +45,7 @@ class ShopViewSet(CustomModelViewSet):
|
|
|
@action(methods=['post'], detail=True)
|
|
|
def update_shop(self, request, pk):
|
|
|
file = request.FILES.get('image', None)
|
|
|
+ brands = request.POST.get('brands')
|
|
|
user = request.user
|
|
|
instance = self.get_object()
|
|
|
|
|
@@ -59,6 +59,17 @@ class ShopViewSet(CustomModelViewSet):
|
|
|
|
|
|
tenant_log(user, BizLog.UPDATE, u'修改门店[%s],id=%d' % (instance.name, instance.id), request.data)
|
|
|
|
|
|
+ StoreBrand.objects.filter(store=instance).delete()
|
|
|
+
|
|
|
+ if brands:
|
|
|
+ brands = brands.split(',')
|
|
|
+ for item in brands:
|
|
|
+ brand = Brand.objects.filter(id=int(item), enabled=True, delete=False).first()
|
|
|
+
|
|
|
+ if not brand:
|
|
|
+ raise CustomError(u'所选择的品牌不存在或不可用或已被删除')
|
|
|
+ StoreBrand.objects.create(store=instance, brand=brand)
|
|
|
+
|
|
|
if file:
|
|
|
old_img = instance.img
|
|
|
img = Images.objects.employee_addnew(user.employee, Images.SHOP_FILE, file)
|
|
@@ -71,21 +82,13 @@ class ShopViewSet(CustomModelViewSet):
|
|
|
|
|
|
@action(methods=['get'], detail=False)
|
|
|
def update_company(self, request):
|
|
|
- print('00000000000000000000000000000000')
|
|
|
-
|
|
|
with transaction.atomic():
|
|
|
xgj_ip = Config.getConfigValue(Config.KEY_XGJ_IP)
|
|
|
xgj_session_key = Config.getConfigValue(Config.KEY_XGJ_SESSION_KEY)
|
|
|
- print('3333333333333')
|
|
|
- print(xgj_ip)
|
|
|
- print(xgj_session_key)
|
|
|
if not xgj_ip:
|
|
|
- print('111111111111')
|
|
|
raise CustomError(u'没有配置销管佳地址,请前往基础设置中配置!')
|
|
|
|
|
|
- print('444444444444444444444')
|
|
|
if not xgj_session_key:
|
|
|
- print('22222222222')
|
|
|
raise CustomError(u'没有配置销管佳密钥,请前往基础设置中配置!')
|
|
|
ts = timezone.now().strftime('%Y%m%d%H%M%S')
|
|
|
token = xgj_session_key + ts
|
|
@@ -94,14 +97,30 @@ class ShopViewSet(CustomModelViewSet):
|
|
|
m.update(token.encode("utf8"))
|
|
|
sign = m.hexdigest()
|
|
|
|
|
|
- # url = xgj_ip + 'wechat/applet/member/company/?ts=' + ts + '&sign=' + sign
|
|
|
- # param = {}
|
|
|
- # result = requests.post(url=url, data=json.dumps(param))
|
|
|
- # result = result.json()
|
|
|
- #
|
|
|
- # if not result['success']:
|
|
|
- # if 'errors' in result:
|
|
|
- # raise CustomError(u'请求失败--' + str(result['errors']))
|
|
|
- # else:
|
|
|
- # raise CustomError(u'请求失败')
|
|
|
+ url = xgj_ip + 'wechat/applet/member/company/?ts=' + ts + '&sign=' + sign
|
|
|
+ param = {}
|
|
|
+ result = requests.post(url=url, data=json.dumps(param))
|
|
|
+ result = result.json()
|
|
|
+ if result['success']:
|
|
|
+ data = result['data']
|
|
|
+ for item in data:
|
|
|
+ store = Shop.objects.filter(xgj_id=item['id']).first()
|
|
|
+ if store:
|
|
|
+ store.delete = False
|
|
|
+ store.save()
|
|
|
+ else:
|
|
|
+ Shop.objects.create(
|
|
|
+ name=item['name'],
|
|
|
+ addr=item['company_address'],
|
|
|
+ tel=item['company_tel'],
|
|
|
+ is_xgj=False,
|
|
|
+ xgj_id=item['id'],
|
|
|
+ )
|
|
|
+
|
|
|
+ tenant_log(request.user, BizLog.UPDATE, u'更新销管佳门店')
|
|
|
+ else:
|
|
|
+ if 'errors' in result:
|
|
|
+ raise CustomError(u'请求失败--' + str(result['errors']))
|
|
|
+ else:
|
|
|
+ raise CustomError(u'请求失败')
|
|
|
return response_ok()
|