Beispiel #1
0
    def testGetHostsForProgram(self):
        """Tests if a host entity for a program is returned.
    """
        properties = {
            'scope': self.sponsor,
        }
        program = seeder_logic.seed(Program, properties)

        #hosts of the program
        user_entities = []
        for i in range(5):
            properties = {'host_for': [self.sponsor.key()]}
            entity = seeder_logic.seed(User, properties)
            user_entities.append(entity)

        host_entities = []
        for i in range(5):
            properties = {'parent': user_entities[i]}
            entity = seeder_logic.seed(Host, properties)
            host_entities.append(entity)

        expected = [entity.key() for entity in host_entities]
        hosts_list = host_logic.getHostsForProgram(program)
        actual = [host.key() for host in hosts_list]
        self.assertEqual(actual, expected)

        #program with a different scope
        program = seeder_logic.seed(Program)
        expected = []
        hosts_list = host_logic.getHostsForProgram(program)
        actual = [host.key() for host in hosts_list]
        self.assertEqual(actual, expected)
Beispiel #2
0
  def createOrUpdateFromForm(self):
    """Creates a new proposal based on the data inserted in the form.

    Returns:
      a newly created proposal entity or None
    """
    slot_transfer_entity = None

    slot_transfer_form = SlotTransferForm(self.data.organization.slots,
                                          self.data.POST)

    if not slot_transfer_form.is_valid():
      return None

    slot_transfer_form.cleaned_data['program'] = self.data.program

    for ent in self.data.slot_transfer_entities:
      if ent.status == 'pending':
        slot_transfer_entity = ent
        break

    host_entities = host_logic.getHostsForProgram(self.data.program)
    to_emails = [i.parent().account.email() for i in host_entities
                 if i.notify_slot_transfer]

    def create_or_update_slot_transfer_trx():
      update = False
      if slot_transfer_entity:
        slot_transfer = db.get(slot_transfer_entity.key())
        slot_transfer_form.instance = slot_transfer
        slot_transfer = slot_transfer_form.save(commit=True)

        update = True
      else:
        slot_transfer = slot_transfer_form.create(
            commit=True, parent=self.data.organization)

      context = notifications.createOrUpdateSlotTransferContext(
          self.data, slot_transfer,
          to_emails, update)
      sub_txn = mailer.getSpawnMailTaskTxn(
          context, parent=slot_transfer.parent())
      sub_txn()

      return slot_transfer

    return db.run_in_transaction(create_or_update_slot_transfer_trx)