Example #1
0
File: quiz.py Project: bxshi/PTL
def QuizGet(request, elementlimit=-1, qid=None, type="-1"):
    """get quiz

        Args:
            request:    Django default parameter

            elementlimit:   -1 for unlimited, other positive number is the max element client wants to retrive

        Returns:
            XML file, the example could be seen in ../samples/quiz.xml

    """

    if request.method == 'POST':
        elementlimit = int(request.POST.get('elementlimit', ''))

    username = Login.objects(session=request.session.session_key).first()

    xml = Element("root")
    i = 0
    if qid is not None:
        quizset = Quiz.objects(id=qid, creator=username.username)
    elif type == "-1":
        quizset = Quiz.objects(creator=username.username)
    else:
        quizset = Quiz.objects(creator=username.username, type=int(type))
    for quiz in quizset:
        xmlquiz = Element('quiz')
        SubElement(xmlquiz, 'creator').text = smart_unicode(quiz.creator)
        SubElement(xmlquiz, 'id').text = smart_unicode(str(quiz.id))

        quiz_info = Element('quiz_info')
        while len(quiz.info) > 0:
            quizinfo = quiz.info.pop(0)
            try:
                SubElement(quiz_info, smart_str(quizinfo.type)).text = smart_unicode(quizinfo.description)
            except KeyError:
                pass

        xmlquiz.append(quiz_info)

        SubElement(xmlquiz, 'description').text = smart_unicode(quiz.description)

        attachments = Element('attachments')
        SubElement(xmlquiz,"attnum").text = str(len(quiz.attachment))
        for attach in quiz.attachment:
            xmlatt = Element("attachment")
            SubElement(xmlatt, "description").text = smart_unicode(attach.description)
            SubElement(xmlatt, "file").text = smart_unicode(attach.file)
            attachments.append(xmlatt)
        xmlquiz.append(attachments)

        correct_answer = Element('correct_answer')
        for answer in quiz.correctanswer:
            xmlans = Element('answer')
            SubElement(xmlans, 'string').text = smart_unicode(answer.answer)
            SubElement(xmlans, 'attach').text = smart_unicode(answer.attach)
            correct_answer.append(xmlans)
        xmlquiz.append(correct_answer)

        wrong_answer = Element('wrong_answer')
        for answer in quiz.wronganswer:
            xmlans = Element('answer')
            SubElement(xmlans, 'string').text = smart_unicode(answer.answer)
            SubElement(xmlans, 'attach').text = smart_unicode(answer.attach)
            wrong_answer.append(xmlans)
        xmlquiz.append(wrong_answer)

        SubElement(xmlquiz, 'manual_difficulty').text = smart_unicode(quiz.manualdifficulty)
        SubElement(xmlquiz, 'auto_difficulty').text = smart_unicode(quiz.autodifficulty)

        tags = Element('tags')
        while len(quiz.tag) > 0:
            tag = quiz.tag.pop(0)
            SubElement(tags, 'tag').text = smart_unicode(tag)
        xmlquiz.append(tags)

        xml.append(xmlquiz)

        i += 1
        if elementlimit != -1 and i >= elementlimit:
            break

    return HttpResponse(smart_unicode(tostring(xml,encoding='UTF-8')),content_type='text/xml')
Example #2
0
File: quiz.py Project: bxshi/PTL
def QuizInsert(request, xml=''):
    """insert quiz into database

        Args:
            xmlstr: A xml string, the root label is root, and each quiz is enclosed by <quiz></quiz> label

        Returns:
            ERR UNKNOWN:    unknown error
            XML EPT:        xml is empty
            QUIZ ADD OK, id=%d  %d represent for id in database

    """
    returnmsg = 'ERR UNKNOWN'

    if request.method == 'GET':
        return HttpResponse('NO GET METHOD')
    if request.method == 'POST':
        xml=smart_str(request.POST.get('xml',''))

    if xml == '':
        returnmsg = "XML EPT"
    else:
        #try:
        #xml = str(xml).decode()
        #xml = str(xml).encode('UTF-8')
        quizxml = fromstring(xml)
        #except:
        #    return HttpResponse("XML ERR")
        quizall = quizxml.findall('quiz')
        while len(quizall) > 0:
            qp = QuizParser(quizall.pop(0))
            username = Login.objects(session=request.session.session_key).first()
            quiz = Quiz(creator=username.username, description=qp.GetDescription(), manualdifficulty=float(qp.GetManualDifficulty()))
            info = qp.GetInfoDict()

            #fill quiz info
            if info is not None:
                try:
                    if info['type'] is not None:
                        quiz.info.append(QuizInfo(type='type', description=info['type']))
                        quiz.type = int(info['type'])
                except KeyError:
                    pass
                try:
                    if info['choose'] is not None:
                        quiz.info.append(QuizInfo(type='choose', description=info['choose']))
                except KeyError:
                    pass
                try:
                    if info['order'] is not None:
                        quiz.info.append(QuizInfo(type='order', description=info['order']))
                except KeyError:
                    pass
                try:
                    if info['selection number'] is not None:
                        quiz.info.append(QuizInfo(type='selection_number', description=info['selection number']))
                except KeyError:
                    pass

            #fill quiz attachments

            attch = qp.GetAttachmentList()
            if attch is not None:
                for tmpattch in attch:
                    try:
                        quiz.attachment.append(QuizAttach(description=tmpattch['description'], file=tmpattch['file']))
                    except KeyError:
                        pass

            #fill answers

            answer = qp.GetCorrectAnswerList()
            if answer is not None:
                while len(answer) > 0:
                    try:
                        tmpanswer = answer.pop(0)
                        quiz.correctanswer.append(QuizAnswer(answer=tmpanswer['string'],attach=tmpanswer['attach']))
                    except KeyError:
                        pass

            answer = qp.GetWrongAnswerList()
            if answer is not None:
                 while len(answer) > 0:
                    try:
                        tmpanswer = answer.pop(0)
                        quiz.wronganswer.append(QuizAnswer(answer=tmpanswer['string'],attach=tmpanswer['attach']))
                    except KeyError:
                        pass

            #fill tags
            tags = qp.GetTagList()
            if tags is not None:
                while len(tags) > 0:
                    try:
                        tmptags = tags.pop(0)
                        quiz.tag.append(tmptags)
                    except KeyError:
                        pass
            quiz.save()
            returnmsg = 'QUIZ ADD OK id='+ str(quiz.id)
    return HttpResponse(returnmsg)