예제 #1
0
def reject(request):
    res = {'code': 0, 'msg': 'success', 'data': {}}
    params = request.POST.dict()
    required = {
        'id': {'requried': True},
        'user_id': {'requried': True}
    }
    check_res = check(required, params)
    if check_res is None or check_res['code'] != 0:
        return JsonResponse(check_res)

    try:
        dynamic = Dynamic.objects.get(id=params['id'])
        if int(params['user_id'])!=dynamic.user_id:
            res = {'code': -3, 'msg': '当前用户无权限', 'data': dynamic.format()}
            return JsonResponse(res)
        if dynamic.state == 2:  #
            dynamic.state = 1
            dynamic.save()
        else:
            res = {'code': -3, 'msg': '状态不可拒绝', 'data': dynamic.format()}
            return JsonResponse(res)
    except Exception as e:
        res = {'code': -2, 'msg': e.__str__(), 'data': []}
        utils.log('ERROR', 'dynamic reject', res['msg'], data=params)
    return JsonResponse(res)
예제 #2
0
def apply(request):
    res = {'code': 0, 'msg': 'success', 'data': {}}
    params = request.POST.dict()
    required = {
        'id': {'requried': True},
        'user_id': {'requried': True}
    }
    check_res = check(required, params)
    if check_res is None or check_res['code'] != 0:
        return JsonResponse(check_res)

    try:
        dynamic=Dynamic.objects.get(id=params['id'])
        if dynamic.state==1:#
            dynamic.state=2
            dynamic.belongsTo=params['user_id']
            res['data']['user_info']=dynamic.format()['user_info']
            dynamic.save()
        else:
            res = {'code': -3, 'msg': '他人正在申领', 'data': dynamic.format()}
            return JsonResponse(res)
        #发消息确认

    except Exception as e:
        res = {'code': -2, 'msg': e.__str__(), 'data': []}
        utils.log('ERROR', 'dynamic apply', res['msg'], data=params)
    return JsonResponse(res)
예제 #3
0
def update(request):
    res = {'code': 0, 'msg': 'success', 'data': {}}
    params = request.POST.dict()
    required = {
        'id':{'requried':True},
        'user_id': {'required': False},
        'type': {'required': False},
        'category': {'required': False},
        'title': {'required': False},
        'desc': {'required': False},
        'images': {'required': False},
        'location': {'required': False},
        'meta': {'required': False},
    }
    check_res = check(required, params)
    if check_res is None or check_res['code'] != 0:
        return JsonResponse(check_res)

    id=params['id']
    params.pop('id')

    try:
        Dynamic.objects.filter(id=id).update(**params)

    except Exception as e:
        res = {'code': -2, 'msg': e.__str__(), 'data': []}
        utils.log('ERROR', 'dynamic update', res['msg'], data=params)
    return JsonResponse(res)
예제 #4
0
def dynamicImgByUrl(request):
    res = {'code': 0, 'msg': 'success', 'data': []}
    params = request.POST.dict()
    required = {'img_url': {'requried': True}}
    check_res = check(required, params)
    if check_res is None or check_res['code'] != 0:
        return JsonResponse(check_res)

    dt = datetime.now()
    url_mid = 'dynamic/{0}/{1}/{2}'.format(dt.year, dt.month, dt.day)
    dir = '{0}/{1}'.format(settings.MEDIA_ROOT, url_mid)
    if not os.path.exists(dir):
        os.makedirs(dir)

    fname = '{0}_{1}.jpg'.format(
        time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())),
        random.randint(1, 1000000))
    path = '{0}/{1}'.format(dir, fname)
    try:
        r = requests.get(params['img_url'])
        with open(path, "wb") as code:
            code.write(r.content)

        res['data'] = '{0}/media/{1}/{2}'.format(URL_PREFIX, url_mid, fname)

    except Exception as e:
        res = {'code': -2, 'msg': e.__str__(), 'data': []}
    return JsonResponse(res)
예제 #5
0
def delete(request):
    res = {'code': 0, 'msg': 'success', 'data': {}}
    params = request.POST.dict()
    required = {
        'id': {'requried': True},
        'user_id': {'requried': True}
    }
    check_res = check(required, params)
    if check_res is None or check_res['code'] != 0:
        return JsonResponse(check_res)
    try:
        Dynamic.objects.filter(id=params['id'],user_id=params['user_id']).update(status=0)
    except Exception as e:
        res = {'code': -2, 'msg': e.__str__(), 'data': []}
        utils.log('ERROR', 'dynamic delete', res['msg'], data=params)
    return JsonResponse(res)
예제 #6
0
def get(request):
    res = {'code': 0, 'msg': 'success', 'data': []}
    params = request.POST.dict()
    required = {'id': {}, 'stu_id': {}}
    check_res = check(required, params)
    if check_res is None or check_res['code'] != 0:
        return JsonResponse(check_res)
    try:
        user = User.objects.get(**params)
        res['data'] = user.format()
    except User.DoesNotExist:
        res = {'code': -2, 'msg': 'DoesNotExist', 'data': []}
    except User.MultipleObjectsReturned:
        res = {'code': -2, 'msg': 'MultipleObjectsReturned', 'data': []}
    except Exception as e:
        res = {'code': -2, 'msg': e.__str__(), 'data': []}
    return JsonResponse(res)
예제 #7
0
def list(request):
    res = {'code': 0, 'msg': 'success', 'data': {}}
    params = request.POST.dict()
    required = {
        'id': {'required': False},
        'user_id': {'required': False},
        'type': {'required': False},
        'category': {'required': False},
        'title': {'required': False},
        'desc': {'required': False},
        'state': {'required': False},
        'belongsTo': {'required': False},
        'page': {'required': False},
        'size': {'required': False},
    }
    check_res = check(required, params)
    page=int(params.get('page',0))
    size=int(params.get('size',10))
    if 'size' in params:
        params.pop('size')
    if 'page' in params:
        params.pop('page')
    params['status']=1

    if check_res is None or check_res['code'] != 0:
        return JsonResponse(check_res)

    try:
        cnt=Dynamic.objects.filter(**params).count()
        data=Dynamic.objects.filter(**params)[page*size:(page+1)*size]
        res['data']['cnt'] = cnt
        res['data']['dynamics']=[]
        for obj in data:
            obj=obj.format()
            res['data']['dynamics'].append(obj)
    except Exception as e:
        res = {'code': -2, 'msg': e.__str__(), 'data': []}
        utils.log('ERROR', 'dynamic list', res['msg'], data=params)
    return JsonResponse(res)
예제 #8
0
def create(request):
    res = {'code': 0, 'msg': 'success', 'data': []}
    params=request.POST.dict()
    required={
        'user_id':{'required':True},
        'type' : {'required':True},
        'category' : {'required':True},
        'title': {'required': True},
        'desc' : {'required':True},
        'images' : {'required':False},
        'location' : {'required':False},
        'meta' : {'required':False},
    }
    check_res=check(required,params)
    if check_res is None or check_res['code']!=0:
        return JsonResponse(check_res)

    try:
        Dynamic.objects.create(**params)
        #校园卡专区
        if int(params.get('type',0))==2 and int(params.get('category',0))==1: #
            if params.get('meta',False) and len(params['meta'])>2: #提取学号
                #提取手机号
                rpc_res=client.rpc('user/get',{'stu_id':params['meta']})
                if rpc_res is None or rpc_res['code']!=0:
                    utils.log('ERROR','dynamic create', 'get use phone NO failed.', data=rpc_res)
                elif len(rpc_res['data']['phone'])>2:
                    sms.send_for_loser(
                        phoneNO=rpc_res['data']['phone'],
                        name=rpc_res['data']['name'],
                        good_name=params['title']
                    )

    except Exception as e:
        res={'code':-2, 'msg':e.__str__(), 'data':[]}
        utils.log('ERROR', 'dynamic create', res['msg'], data=params)
    return JsonResponse(res)