def AuthenticatedPost(self, authenticated_org, event):
        form = primary_contact_db.ContactFormFull(self.request.POST)
        org_id = self.request.get("choose_organization")  # TODO: is this intended?
        try:
            id = int(org_id)
            org = organization.Organization.get_by_id(id)
        except:
            return

        # validate form and save if successful
        if form.validate():
            contact = primary_contact_db.Contact(
                first_name=form.first_name.data,
                last_name=form.last_name.data,
                title=form.title.data,
                phone=form.phone.data,
                email=form.email.data,
                is_primary = bool(form.is_primary.data),
                organization = org.key(),
            )
            primary_contact_db.PutAndCache(contact, ten_minutes)
            self.redirect("/organization-settings?message=Contact created. It may take a few moments for the contact to show up on your list.")
            return
        else:
            self.response.out.write(template.render({
                "form": form,
                "organization_list": self._get_org_list(org),
            }))
            return
コード例 #2
0
def GetOrganizationForm(post_data):
  e = event_db.Event(name = "Test Incident",
  case_label = "B",
  counties = ["Kings"])
  query_string = "SELECT * FROM Organization WHERE is_active = True ORDER BY name"
  organizations = db.GqlQuery(query_string)
  events = event_db.GetAllCached()
  events = db.GqlQuery("SELECT * From Event ORDER BY created_date DESC")
  event_key = None
  if events.count() == 0:
    logging.warning("Initialize called")
    e = event_db.Event(name = "North Central Victorian Floods",
                       case_label = "A",
                       short_name = "ncv_floods")
    e.put()
    event_key = e.key()
    # TODO(Jeremy): This could be dangerous if we reset events.
    for s in site_db.Site.all().run(batch_size = 1000):
      event_db.AddSiteToEvent(s, e.key().id(), force = True)
    events = [e]

  if organizations.count() == 0:
    # init: populate the database with Admin user
    admin_org = organization.Organization(
        name="Admin",
        password="******",
        org_verified=True,
        is_active=True,
        is_admin=True,
        incidents=[event_key]
    )
    admin_org.put()
    admin_contact = primary_contact_db.Contact(
        first_name="Admin",
        last_name="Admin",
        title="Admin",
        phone="1234",
        email="*****@*****.**",
        organization=admin_org,
        is_primary=True
    )
    admin_contact.put()
    organizations = db.GqlQuery("SELECT * FROM Organization WHERE is_active = True ORDER BY name")

  class OrganizationForm(wtforms.form.Form):
    event = wtforms.fields.SelectField(
        'Work Event',
        choices = [(e.name, e.name) for e in events],
        validators = [wtforms.validators.required()])
    password = wtforms.fields.PasswordField(
        'Password',
        validators = [ wtforms.validators.required() ])
  form = OrganizationForm(post_data)
  return form
コード例 #3
0
    def AuthenticatedPost(self, org, event):
        global_admin = False
        local_admin = False
        if org.name == GLOBAL_ADMIN_NAME:
            global_admin = True
        if org.is_admin == True and global_admin == False:
            local_admin = True

        if global_admin == False and local_admin == False:
            self.redirect("/")
            return

        if self.request.get("create_contact"):
            data = primary_contact_db.ContactFormFull(self.request.POST)
            if data.validate():
                organization_id = self.request.get("choose_organization")
                try:
                    id = int(organization_id)
                except:
                    return
                this_organization = organization.Organization.get_by_id(id)
                if not org.may_administer(this_organization):
                    self.abort(403)
                contact = primary_contact_db.Contact(
                    first_name=data.first_name.data,
                    last_name=data.last_name.data,
                    title=data.title.data,
                    phone=data.phone.data,
                    email=data.email.data,
                    is_primary=bool(data.is_primary.data),
                    organization=this_organization.key(),
                )
                primary_contact_db.PutAndCache(contact, ten_minutes)
                self.redirect(
                    "/admin-create-contact?selected_org=%s&message=Contact Created"
                    % this_organization.key().id())
                return
            else:
                #query_string = "SELECT * FROM Event"
                #events_list = db.GqlQuery(query_string)
                suggested_password = random_password.generate_password()
                query_string = "SELECT * FROM Organization"
                organization_list = db.GqlQuery(query_string)

                self.response.out.write(
                    template.render({
                        "form": data,
                        "errors": data.errors,
                        "create_contact": True,
                        "organization_list": organization_list,
                    }))
                return

        if self.request.get("create_admin"):
            data = organization.OrganizationAdminForm(self.request.POST)
            event_id = self.request.get("choose_event")
            try:
                id = int(event_id)
            except:
                return
            this_event = event_db.Event.get_by_id(id)
            if local_admin:
                if not this_event.key() == event.key():
                    self.redirect("/")
                    return

            if data.validate():
                new_org = organization.Organization(
                    name=data.name.data,
                    email=data.email.data,
                    phone=data.phone.data,
                    address=data.address.data,
                    city=data.city.data,
                    state=data.state.data,
                    zip_code=data.zip_code.data,
                    physical_presence=True,
                    number_volunteers="0",
                    voad_member=False,
                    org_verified=True,
                    twitter=data.twitter.data,
                    url=data.url.data,
                    facebook=data.facebook.data,
                    incidents=[this_event.key()],
                    password=self.request.get("password"),
                    is_active=True,
                    is_admin=True,
                )

                # set all phase fields true for admin
                for phase_name in new_org.get_phase_boolean_names():
                    setattr(new_org, phase_name, True)

                new_contact = primary_contact_db.Contact(
                    first_name=data.contact_first_name.data,
                    last_name=data.contact_last_name.data,
                    title=data.contact_title.data,
                    email=data.contact_email.data,
                    phone=data.contact_phone.data,
                    is_primary=True)

                organization.PutAndCacheOrganizationAndContact(
                    organization=new_org,
                    contact=new_contact,
                )
                self.redirect("/admin?message=Admin Created")
                return
            else:
                # needs events lists, password, errors
                query_string = "SELECT * FROM Event"
                suggested_password = random_password.generate_password()
                self.response.out.write(
                    template.render({
                        "form": data,
                        "errors": data.errors,
                        "create_admin": True,
                        #"events_list": events_list,
                        "auto_password": suggested_password,
                    }))
                return

        if self.request.get("delete_org_id"):
            # delete organization
            try:
                id = int(self.request.get("delete_org_id"))
                org_by_id = organization.Organization.get_by_id(id)
            except:
                self.abort(400)

            if not org.may_administer(org_by_id):
                self.abort(403)

            primary_contact_db.RemoveOrgFromContacts(org_by_id)
            db.delete(org_by_id)
            self.redirect("/admin")
            return

        if self.request.get("delete_contact_id"):
            # delete contact
            try:
                id = int(self.request.get("delete_contact_id"))
                contact_by_id = primary_contact_db.Contact.get_by_id(id)
            except:
                self.abort(400)

            if not org.may_administer(org_by_id):
                self.abort(403)

            db.delete(contact_by_id)
            self.redirect("/admin")
            return

        if self.request.get("verify_organization"):
            # verify organization
            try:
                id = int(self.request.get("verify_organization"))
                org_by_id = organization.Organization.get_by_id(id)
            except:
                self.abort(400)

            # check we are allowed
            if not org.may_administer(org_by_id):
                self.abort(403)

            # perform verification
            org_by_id.verify()

            # cache
            organization.PutAndCache(org_by_id, 600)
            self.redirect("/admin")
            return

        if self.request.get("save_org_id"):
            # save org (?)
            try:
                id = int(self.request.get("save_org_id"))
                org_by_id = organization.Organization.get_by_id(id)
            except:
                self.abort(400)

            if not org.may_administer(org_by_id):
                self.abort(403)

            org_by_id.org_verified = True
            organization.PutAndCache(org_by_id, 600)
            self.redirect("/admin")
            return
コード例 #4
0
    def post(self):
        # create inactive, unverified org with a random password
        org = organization.Organization(
            name=self.request.get("name"),
            is_active=False,
            org_verified=False,
            voad_referral=self.request.get("voad_referral"),
            password=random_password.generate_password())

        # set non-contact org attributes by type
        for k, v in self.request.POST.iteritems():
            if not any(
                    k.startswith(prop)
                    for prop in self.CONTACT_PROPERTIES_LIST):
                if k == "choose_event":
                    chosen_event = event_db.Event.get_by_id(int(v))
                    setattr(org, "incidents", [chosen_event.key()])
                elif k in self.BOOLEAN_PROPERTIES_LIST:
                    setattr(org, k, bool(int(v)))
                else:
                    # clean away known pre-supplied strings
                    if v.strip() in ('http://', '@'):
                        v = None
                    setattr(org, k, v)

        # create contacts
        def get_contact_field(field_name, contact_num):
            field_value = self.request.get(field_name + '_%d' % i)
            return field_value.strip() if field_value else None

        new_contacts = []

        for i in range(10):
            first_name = get_contact_field('first_name', i)
            last_name = get_contact_field('last_name', i)
            title = get_contact_field('title', i)
            email = get_contact_field('personal_email', i)
            phone = get_contact_field('personal_phone', i)

            if first_name and last_name and email and phone:  # required fields
                new_contacts.append(
                    primary_contact_db.Contact(
                        first_name=first_name,
                        last_name=last_name,
                        title=title,
                        email=email,
                        phone=phone,
                        is_primary=(i == 0
                                    )  # the first contact is the primary one
                    ))

        # save
        organization.PutAndCacheOrganizationAndContact(org, new_contacts)

        # email primary contacts & administrators
        organization_form = organization.OrganizationForm(None, org)
        contact_forms = [
            primary_contact_db.ContactFormFull(None, contact)
            for contact in new_contacts
        ]
        messaging.email_contacts_using_templates(
            event=chosen_event,
            contacts=[c for c in new_contacts if c.is_primary],
            subject_template_name='new_organization.to_contacts.subject.txt',
            body_template_name='new_organization.to_contacts.body.txt',
            new_organization=org,
            primary_contact=new_contacts[0],
            application_id=messaging.get_application_id(),
            organization_form=organization_form,
            contact_forms=contact_forms,
        )
        approval_url = "%s://%s/admin-new-organization?new_organization=%d" % (
            urlparse(self.request.url).scheme, urlparse(
                self.request.url).netloc, org.key().id())
        messaging.email_administrators_using_templates(
            event=chosen_event,
            subject_template_name='new_organization.to_admins.subject.txt',
            body_template_name='new_organization.to_admins.body.txt',
            new_organization=org,
            primary_contact=new_contacts[0],
            application_id=messaging.get_application_id(),
            approval_url=approval_url,
            organization_form=organization_form,
            contact_forms=contact_forms,
        )

        self.redirect("/welcome")