예제 #1
0
class QuestionSchema(ContentSchema):
    title = colander.SchemaNode(
        colander.String(),
        title=_(u'Question'),
    )
    question_type = colander.SchemaNode(
        colander.String(),
        title=_(u'Question Type'),
        validator=colander.OneOf(["radio", "checkbox", "text"]),
        widget=RadioChoiceWidget(values=[["radio", _(
            "Single Choice")], ["checkbox", _("Multiple Choice")],
                                         ["text", _("Text")]]))
예제 #2
0
class SurveySchema(ContentSchema):
    title = colander.SchemaNode(
        colander.String(),
        title=_(u'Survey Title'),
    )
    collect_user_info = colander.SchemaNode(
        colander.Boolean(), title=_(u'Restrict to login users'))
    redirect_url = colander.SchemaNode(
        colander.String(),
        title=_(u'Redirect URL:'),
        description=("If no redirect url is given. Users will be redirect to "
                     "the default result page."),
        missing=None)
예제 #3
0
class AnswerAddForm(AddFormView):
    schema_factory = AnswerSchema
    add = AnswerField
    item_type = _(u"Answer")

    @property
    def success_url(self):  # pragma: no cover
        return self.request.resource_url(self.context)

    def save_success(self, appstruct):
        prevanswers = self.context.children
        question_type = self.context.question_type

        if question_type == "text":
            self.request.session.flash(
                u'Cannot add answer to freetext question'.format(
                    self.context.title), 'error')
            raise HTTPFound(location=self.request.resource_url(self.context))
        elif question_type == "radio" or question_type == "checkbox":
            for prevanswer in prevanswers:
                if appstruct.get("title", "") == prevanswer.title:
                    self.request.session.flash(
                        u'Question already has this option'.format(
                            self.context.title), 'error')
                    raise HTTPFound(
                        location=self.request.resource_url(self.context))
        super(AnswerAddForm, self).save_success(appstruct)
예제 #4
0
    def save_answers(self, request, questions, answers, username=None):
        browser_data = {
            "host": request.host,
            "client_ip": request.client_addr,
            "user_agent": request.user_agent,
        }

        if self.collect_user_info:
            if not username and not request.user:
                request.session.flash(
                    _("Please login or provide your username to continue."),
                    "warning"
                )
                return False
            survey = UserSurvey.query.filter(
                UserSurvey.survey_id == self.id,
                UserSurvey.username == (username or request.user.name)
            ).first()
            if survey:
                request.session.flash(
                    _("You have already done this survey."),
                    "warning"
                )
                return False
            survey = UserSurvey(
                browser_data=browser_data,
                survey_id=self.id,
                id=uuid_factory(),
                username=(username or request.user.name),
            )
        else:
            survey = UserSurvey(
                browser_data=browser_data,
                survey_id=self.id,
                id=uuid_factory()
            )
        if survey.save_answers(questions, answers):
            DBSession.add(survey)
            request.session.flash(
                _(u'Thank you for completing ${title}.',
                  mapping=dict(title=self.title)), 'success')
            return survey
        else:
            request.session.flash(
                _((u'Something went wrong, pleace check'
                   ' if all required fields are field out.')), 'danger')
            return False
예제 #5
0
class Survey(Content):
    """Survey Content type."""
    implements(IDefaultWorkflow)

    id = Column(Integer, ForeignKey('contents.id'), primary_key=True)
    collect_user_info = Column(Boolean, default=False)
    redirect_url = Column(String)
    date_added = Column(DateTime, default=datetime.datetime.utcnow)

    type_info = Content.type_info.copy(
        name=u'Survey',
        title=_(u'Survey'),
        add_view=u'add_survey',
        addable_to=[u'Document'],
    )

    def save_answers(self, request, questions, answers, username=None):
        browser_data = {
            "host": request.host,
            "client_ip": request.client_addr,
            "user_agent": request.user_agent,
        }

        if self.collect_user_info:
            if not username and not request.user:
                request.session.flash(
                    _("Please login or provide your username to continue."),
                    "warning")
                return False
            survey = UserSurvey.query.filter(
                UserSurvey.survey_id == self.id,
                UserSurvey.username == (username
                                        or request.user.name)).first()
            if survey:
                request.session.flash(_("You have already done this survey."),
                                      "warning")
                return False
            survey = UserSurvey(
                browser_data=browser_data,
                survey_id=self.id,
                id=uuid_factory(),
                username=(username or request.user.name),
            )
        else:
            survey = UserSurvey(browser_data=browser_data,
                                survey_id=self.id,
                                id=uuid_factory())
        if survey.save_answers(questions, answers):
            DBSession.add(survey)
            request.session.flash(
                _(u'Thank you for completing ${title}.',
                  mapping=dict(title=self.title)), 'success')
            return survey
        else:
            request.session.flash(
                _((u'Something went wrong, pleace check'
                   ' if all required fields are field out.')), 'danger')
            return False
예제 #6
0
    def save_answers(self, request, questions, answers, username=None):
        browser_data = {
            "host": request.host,
            "client_ip": request.client_addr,
            "user_agent": request.user_agent,
        }

        if self.collect_user_info:
            if not username and not request.user:
                request.session.flash(
                    _("Please login or provide your username to continue."),
                    "warning")
                return False
            survey = UserSurvey.query.filter(
                UserSurvey.survey_id == self.id,
                UserSurvey.username == (username
                                        or request.user.name)).first()
            if survey:
                request.session.flash(_("You have already done this survey."),
                                      "warning")
                return False
            survey = UserSurvey(
                browser_data=browser_data,
                survey_id=self.id,
                id=uuid_factory(),
                username=(username or request.user.name),
            )
        else:
            survey = UserSurvey(browser_data=browser_data,
                                survey_id=self.id,
                                id=uuid_factory())
        if survey.save_answers(questions, answers):
            DBSession.add(survey)
            request.session.flash(
                _(u'Thank you for completing ${title}.',
                  mapping=dict(title=self.title)), 'success')
            return survey
        else:
            request.session.flash(
                _((u'Something went wrong, pleace check'
                   ' if all required fields are field out.')), 'danger')
            return False
예제 #7
0
class AnswerField(Content):
    """Answer Content type."""
    tablename = 'survey_fields'

    @declared_attr
    def __tablename__(cls):
        return cls.tablename

    id = Column(Integer, ForeignKey('contents.id'), primary_key=True)

    type_info = Content.type_info.copy(
        name=u'Answer',
        title=_(u'Answer'),
        add_view=u'add_answer',
        addable_to=[u'Question'],
    )
예제 #8
0
class Question(Content):
    """Question Content type."""
    tablename = 'survey_questions'

    @declared_attr
    def __tablename__(cls):
        return cls.tablename

    id = Column(Integer, ForeignKey('contents.id'), primary_key=True)
    question_type = Column(String())

    # change the type info to your needs
    type_info = Content.type_info.copy(
        name=u'Question',
        title=_(u'Question'),
        add_view=u'add_question',
        addable_to=[u'Survey'],
    )
예제 #9
0
    def show_answers(self):
        questions = self.context.children
        if self.request.has_permission("add", self.context):
            username = self.request.params.get(
                "username",
                self.request.user.name if self.request.user else ""
            )
        else:
            username = self.request.user.name if self.request.user else ""

        user_survey = UserSurvey.query.filter(
            UserSurvey.username == username,
            UserSurvey.survey_id == self.context.id
        ).order_by(UserSurvey.date_completed.desc()).first()

        answers = {}
        answer_dbs = []
        if user_survey:
            answer_dbs = UserAnswer.query.filter(
                UserAnswer.survey_id == self.context.id,
                UserAnswer.user_survey_id == user_survey.id
            ).all()
        else:
            self.request.session.flash(
                _(u'Please complete the survey before continuing'),
                "warnings"
            )
            return httpexc.HTTPFound(location=self.context.path)
        for answer_db in answer_dbs:
            if answer_db.question_id in answers:
                answers[answer_db.question_id].append(answer_db.answer)
            else:
                answers[answer_db.question_id] = [answer_db.answer]
        return {
            'questions': questions,
            'answers': answers,
            'user_survey': user_survey
        }
예제 #10
0
class QuestionAddForm(AddFormView):
    schema_factory = QuestionSchema
    add = Question
    item_type = _(u"Question")
예제 #11
0
class SurveyAddForm(AddFormView):
    schema_factory = SurveySchema
    add = Survey
    item_type = _(u"Survey")
예제 #12
0
class AnswerSchema(ContentSchema):
    title = colander.SchemaNode(
        colander.String(),
        title=_(u'Answering Choice:'),
    )