Ejemplo n.º 1
0
def accept_question(sql_session, participation, timestamp, subject, text):
    """Add a contestant-submitted question to the database.

    Validate and add a question received from a contestant (usually
    through CWS) to the database.

    sql_session (Session): the SQLAlchemy database session to use.
    participation (Participation): the participation of the user who
        posed the question.
    timestamp (datetime): when the question was asked.
    subject (str): the subject of the question.
    text (str): the body of the question.

    return (Question): the question that was added to the database.

    raise (QuestionsNotAllowed): if the contest doesn't allow
        contestants to ask questions.
    raise (UnacceptableQuestion): if some conditions necessary to
        accept the questions were not met.

    """
    if not participation.contest.allow_questions:
        raise QuestionsNotAllowed()

    subject_length = len(subject)
    text_length = len(text.strip())
    if subject_length > Question.MAX_SUBJECT_LENGTH \
            or text_length > Question.MAX_TEXT_LENGTH:
        logger.warning("Long question (%d, %d) dropped for user %s.",
                       subject_length, text_length,
                       participation.user.username)
        raise UnacceptableQuestion(
            N_("Question too long!"),
            N_("Subject must be at most %(max_subject_length)d characters, "
               "content at most %(max_text_length)d."),
            {"max_subject_length": Question.MAX_SUBJECT_LENGTH,
             "max_text_length": Question.MAX_TEXT_LENGTH})

    if text_length == 0:
        logger.warning("Empty question (%d, %d) dropped for user %s.",
                       subject_length, text_length,
                       participation.user.username)
        raise UnacceptableQuestion(
            N_("Empty question!"),
            N_("Question subject can be empty, but question text cannot be empty."))

    question = Question(timestamp, subject, text, participation=participation)
    sql_session.add(question)

    logger.info("Question submitted by user %s.", participation.user.username)

    try:
        send_telegram_question_notification(question)
    except Exception as e:
        logger.exception("Error occurred while sending Telegram notification")

    return question
Ejemplo n.º 2
0
def accept_question(sql_session, participation, timestamp, subject, text):
    """Add a contestant-submitted question to the database.

    Validate and add a question received from a contestant (usually
    through CWS) to the database.

    sql_session (Session): the SQLAlchemy database session to use.
    participation (Participation): the participation of the user who
        posed the question.
    timestamp (datetime): when the question was asked.
    subject (str): the subject of the question.
    text (str): the body of the question.

    return (Question): the question that was added to the database.

    raise (QuestionsNotAllowed): if the contest doesn't allow
        contestants to ask questions.
    raise (UnacceptableQuestion): if some conditions necessary to
        accept the questions were not met.

    """
    if not participation.contest.allow_questions:
        raise QuestionsNotAllowed()

    subject_length = len(subject)
    text_length = len(text)
    if subject_length > Question.MAX_SUBJECT_LENGTH \
            or text_length > Question.MAX_TEXT_LENGTH:
        logger.warning("Long question (%d, %d) dropped for user %s.",
                       subject_length, text_length,
                       participation.user.username)
        raise UnacceptableQuestion(
            N_("Question too long!"),
            N_("Subject must be at most %(max_subject_length)d characters, "
               "content at most %(max_text_length)d."), {
                   "max_subject_length": Question.MAX_SUBJECT_LENGTH,
                   "max_text_length": Question.MAX_TEXT_LENGTH
               })

    question = Question(timestamp, subject, text, participation=participation)
    sql_session.add(question)

    logger.info("Question submitted by user %s.", participation.user.username)

    # Make manual log of question
    f = open('/home/ubuntu/logs/questions', 'a')
    l = str(datetime.datetime.now())
    l += " NEW QUESTION "
    l += " Contest: " + participation.contest.name
    l += " Username: "******" Subject: " + subject
    f.write(l + '\n')
    f.close()

    return question
Ejemplo n.º 3
0
 def get_question(cls, participation=None, **kwargs):
     """Create a question."""
     participation = participation if participation is not None \
         else cls.get_participation()
     args = {
         "participation": participation,
         "subject": unique_unicode_id(),
         "text": unique_unicode_id(),
         "question_timestamp": (participation.contest.start
                                + timedelta(0, unique_long_id())),
     }
     args.update(kwargs)
     question = Question(**args)
     return question
Ejemplo n.º 4
0
    def post(self):
        participation = self.current_user

        # User can post only if we want.
        if not self.contest.allow_questions:
            raise tornado.web.HTTPError(404)

        fallback_page = self.contest_url("communication")

        subject_length = len(self.get_argument("question_subject", ""))
        text_length = len(self.get_argument("question_text", ""))
        if subject_length > 50 or text_length > 2000:
            logger.warning("Long question (%d, %d) dropped for user %s.",
                           subject_length, text_length,
                           self.current_user.user.username)
            self.application.service.add_notification(
                self.current_user.user.username,
                self.timestamp,
                self._("Question too big!"),
                self._("You have reached the question length limit."),
                NOTIFICATION_ERROR)
            self.redirect(fallback_page)
            return

        question = Question(self.timestamp,
                            self.get_argument("question_subject", ""),
                            self.get_argument("question_text", ""),
                            participation=participation)
        self.sql_session.add(question)
        self.sql_session.commit()

        logger.info(
            "Question submitted by user %s.", participation.user.username)

        if config.email_notification:
            send_mail('New Question Received', 'Please Check CMS.',
                      config.email_notification)

        # Add "All ok" notification.
        self.application.service.add_notification(
            participation.user.username,
            self.timestamp,
            self._("Question received"),
            self._("Your question has been received, you will be "
                   "notified when it is answered."),
            NOTIFICATION_SUCCESS)

        self.redirect(fallback_page)
Ejemplo n.º 5
0
 def update(self):
     self.question = Question.get_from_id(self.question.id, self.sql_session)