Пример #1
0
  def create(self, commit=True, key_name=None, parent=None):
    """Save this form's cleaned data into a conversation model instance.

    See soc.views.forms.ModelForm.create for original specification.

    Args:
      commit: Optional bool, default True; If True, the conversation model
              is also saved to the datastore. A message model will only be
              created if the conversation is comitted.
      key_name: The key name of the new model instance; None by default.
      parent: Key (ndb) for the entity that is the new entity's parent; None by
              default.

    Returns:
      The GCIConversation model instance created by this call.
    """

    cleaned_data = self.cleaned_data
    creator = cleaned_data['creator']
    organization = cleaned_data['organization']
    user_keys = set()

    if creator is not None:
      user_keys.add(creator)

    recipients_type = cleaned_data['recipients_type']

    if recipients_type != conversation_model.ORGANIZATION:
      organization = None

    conversation = gciconversation_model.GCIConversation(
        program=cleaned_data['program'], id=key_name, parent=parent,
        subject=cleaned_data['subject'], creator=creator,
        recipients_type=recipients_type, organization=organization,
        include_admins=cleaned_data.get('include_admins', False),
        include_mentors=cleaned_data.get('include_mentors', False),
        include_students=cleaned_data.get('include_students', False),
        include_winners=cleaned_data.get('include_winners', False),
        auto_update_users=cleaned_data['auto_update_users'])

    if not commit:
      return conversation

    conversation.put()

    if recipients_type == conversation_model.USER:
      user_keys.update(cleaned_data['users'])
      for user_key in user_keys:
        gciconversation_logic.addUserToConversation(conversation.key, user_key)
    else:
      gciconversation_logic.refreshConversationParticipants(conversation.key)

    message = gciconversation_logic.createMessage(
        conversation=conversation.key, user=creator,
        content=cleaned_data['message_content'])

    gciconversation_logic.notifyParticipantsOfMessage(
        message.key, False)

    return conversation
Пример #2
0
    def createReplyFromForm(self, data):
        """Creates a new message based on the data inserted into the form.

    Args:
      data: A RequestData object for the current request.

    Returns:
      A newly created message entity.
    """
        assert access_checker.isSet(data.conversation)
        assert access_checker.isSet(data.user)

        content = cleaning.sanitize_html_string(data.request.POST["content"].strip())

        if len(html.strip_tags(content).strip()) == 0:
            raise exception.Forbidden(message=DEF_BLANK_MESSAGE)

        author = ndb.Key.from_old_key(data.user.key())

        return gciconversation_logic.createMessage(data.conversation.key, author, content)
Пример #3
0
    def createReplyFromForm(self, data):
        """Creates a new message based on the data inserted into the form.

    Args:
      data: A RequestData object for the current request.

    Returns:
      A newly created message entity.
    """
        assert access_checker.isSet(data.conversation)
        assert access_checker.isSet(data.user)

        content = cleaning.sanitize_html_string(
            data.request.POST['content'].strip())

        if len(html.strip_tags(content).strip()) == 0:
            raise exception.Forbidden(message=DEF_BLANK_MESSAGE)

        author = ndb.Key.from_old_key(data.user.key())

        return gciconversation_logic.createMessage(data.conversation.key,
                                                   author, content)
Пример #4
0
    def create(self, commit=True, key_name=None, parent=None):
        """Save this form's cleaned data into a conversation model instance.

    See soc.views.forms.ModelForm.create for original specification.

    Args:
      commit: Optional bool, default True; If True, the conversation model
              is also saved to the datastore. A message model will only be
              created if the conversation is comitted.
      key_name: The key name of the new model instance; None by default.
      parent: Key (ndb) for the entity that is the new entity's parent; None by
              default.

    Returns:
      The GCIConversation model instance created by this call.
    """

        cleaned_data = self.cleaned_data
        creator = cleaned_data['creator']
        organization = cleaned_data['organization']
        user_keys = set()

        if creator is not None:
            user_keys.add(creator)

        recipients_type = cleaned_data['recipients_type']

        if recipients_type != conversation_model.ORGANIZATION:
            organization = None

        conversation = gciconversation_model.GCIConversation(
            program=cleaned_data['program'],
            id=key_name,
            parent=parent,
            subject=cleaned_data['subject'],
            creator=creator,
            recipients_type=recipients_type,
            organization=organization,
            include_admins=cleaned_data.get('include_admins', False),
            include_mentors=cleaned_data.get('include_mentors', False),
            include_students=cleaned_data.get('include_students', False),
            include_winners=cleaned_data.get('include_winners', False),
            auto_update_users=cleaned_data['auto_update_users'])

        if not commit:
            return conversation

        conversation.put()

        if recipients_type == conversation_model.USER:
            user_keys.update(cleaned_data['users'])
            for user_key in user_keys:
                gciconversation_logic.addUserToConversation(
                    conversation.key, user_key)
        else:
            gciconversation_logic.refreshConversationParticipants(
                conversation.key)

        message = gciconversation_logic.createMessage(
            conversation=conversation.key,
            user=creator,
            content=cleaned_data['message_content'])

        gciconversation_logic.notifyParticipantsOfMessage(message.key, False)

        return conversation