コード例 #1
0
def index(request):
    if 'Email' in request.COOKIES:
        # if user has logged in, it has Email in cookies
        email = request.COOKIES.get('Email', '')
        if email:
            # initialize some result
            status = ''
            imagePath = ''
            message = ''
            completeData = []
            ret = {}
            completeCount = 0
            rectCount = 0
            bookName = ''
            volume = ''
            page = ''
            # get the imagePath and complete situation
            # imagePath = getNextPhoto(email)
            completeData = getComplete(email)
            completeCount = completeData[0]
            rectCount = completeData[1]
            imagePath = completeData[2]
            if imagePath == BUSY_FLAG:
                # no effective imagePath
                # distribute again and update
                imagePath = distributePhoto(email)
            if imagePath[0] == '-':
                # there is no new image need to be labeled
                status = imagePath
            else:
                message = getMessage(imagePath)
                bookName, volume, page = getBookInfo(imagePath)

            # set the return result
            imagePath = datapath + imagePath
            ret = {
                'completeCount': completeCount,
                'rectCount': rectCount,
                'status': status,
                'imagePath': imagePath,
                'message': message,
                'bookName': bookName,
                'volume': volume,
                'page': page,
                'Email': email
            }
            # set cookies which will expires 3 days later and return
            # response
            response = render(request, 'index.html', {'ret': ret})
            response.set_cookie('Email',
                                email,
                                expires=datetime.datetime.now() +
                                datetime.timedelta(days=3))
            return response
            # return render(request, 'index.html', {'ret': ret})
        else:
            return redirect('error')
    else:
        # if user has not logged in, redirect to the login page
        return redirect('login_with_args', state='0')
コード例 #2
0
def setMessage(request):
    # check the email first
    if 'Email' in request.COOKIES:
        # get the data and update the database
        email = request.COOKIES.get('Email', '')
        if email:
            # extract data
            message = request.POST.get("Message", '')
            photoPath = request.POST.get('PhotoPath', '')
            reSubmitFlag = request.POST.get('reSubmitFlag', '')
            # update database
            if MessageDatas.objects.filter(PhotoPath=photoPath).exists():
                rec = MessageDatas.objects.get(PhotoPath=photoPath)
                rec.Email = email
                rec.Message = message.encode('utf8')
                rec.RectCount = getRectNumFromMessage(message.encode('utf8'))
                rec.Status = '11'
                rec.save()
                # get complete situation
                usrComplt = CompleteDatas.objects.get(Email=email)
                if reSubmitFlag == "false":
                    usrComplt.Count += 1
                    usrComplt.RectCount += rec.RectCount
                # get next imagePath
                try:
                    imagePath = getNextPhoto(email)
                except Exception as e:
                    print e
                    imagePath = BUSY_FLAG
                usrComplt.CurrentImg = imagePath
                usrComplt.save()
                # return result
                ret = {}
                status = 0
                bookName = ''
                volume = ''
                page = ''
                if imagePath[0] == '-':
                    # there is no new image need to be labeled
                    status = imagePath
                else:
                    message = getMessage(imagePath)
                    bookName, volume, page = getBookInfo(imagePath)
                ret = {"status": status,
                       "nextImage": datapath + imagePath,
                       "completeCount": usrComplt.Count,
                       "rectTotalRect": usrComplt.RectCount,
                       "message": message,
                       "bookName": bookName,
                       "volume": volume,
                       "page": page,
                       }
                return HttpResponse(json.dumps(ret), content_type='text/json')
            else:
                return redirect('error')
        else:
            return redirect('error')
    else:
        return redirect('login_with_args', state='0')