Example #1
0
def load_orgs():
    """ Load wildlife orgs from user.csv into database. """

    print "Orgs"
    Org.query.delete()

    # Read u.user file and insert data
    data = csv_to_tuples(user_csv_path)

    for row in data:

        user_id, name, show_address, address1, address2, city, state, zipcode, phone, desc, accept_volunteers = row[:
                                                                                                                    11]

        org = Org(user_id=user_id,
                  name=name,
                  ein=None,
                  show_address=show_address,
                  address1=address1,
                  address2=address2,
                  city=city,
                  state=state,
                  zipcode=zipcode,
                  desc=desc,
                  phone=phone,
                  email=None,
                  website=None,
                  accept_animals=1,
                  accept_volunteers=accept_volunteers)

        db.session.add(org)
    db.session.commit()
Example #2
0
    def random_orgs():
        org_name = forgery_py.forgery.basic.text(length=None,
                                                 at_least=3,
                                                 at_most=15,
                                                 lowercase=True,
                                                 uppercase=True,
                                                 digits=False,
                                                 spaces=True,
                                                 punctuation=False)
        password = forgery_py.forgery.basic.text(length=None,
                                                 at_least=3,
                                                 at_most=15,
                                                 lowercase=True,
                                                 uppercase=True,
                                                 digits=False,
                                                 spaces=False,
                                                 punctuation=False)
        org_url = forgery_py.forgery.internet.top_level_domain()

        an_org = Org(org_name=org_name, password=password, org_url=org_url)

        db.session.add(an_org)

        for i in range(4):
            random_orgs()
Example #3
0
def load_click_info_from_db():
    """Loads all the information needed to generate analytics JSON files."""

    orgs = Org.list_of_org_objects()
    animals = Animal.list_of_animal_objects()
    clicks = Click.list_of_click_objects()
    click_filters = ClickFilter.list_of_click_filter_objects()

    return (orgs, animals, clicks, click_filters)
Example #4
0
def load_click_info_from_db():
    """Loads all the information needed to generate analytics JSON files."""

    orgs = Org.list_of_org_objects()
    animals = Animal.list_of_animal_objects()
    clicks = Click.list_of_click_objects()
    click_filters = ClickFilter.list_of_click_filter_objects()

    return (orgs, animals, clicks, click_filters)
Example #5
0
def create_org_with_cause_id(org_name, cause_id, mission, web_url, tagline):
    """Create and return a new organization."""

    org = Org(org_name=org_name,
              cause_id=cause_id,
              mission=mission,
              web_url=web_url,
              tagline=tagline)

    db.session.add(org)
    db.session.commit()

    return org
Example #6
0
def sample_data():
    """Create some sample data."""

    # In case this is run more than once, empty out existing data
    Cause.query.delete()
    Org.query.delete()

    # Add sample Organizations
    womens_services = Cause(cause_name='Womens Services')
    swap = Org(
        org_name='DIVAS',
        cause=womens_services,
        mission=
        'To support women escaping domestic abuse and homelessness and provide them with marketable skills'
    )

    db.session.add_all([womens_services, swap])
    db.session.commit()
Example #7
0
def post_org(**kwargs):
    return Org.create(user.domain)
Example #8
0
def user_added():
    """New account form submission."""

    print request.form.get

    # Basic user fields:
    username = request.form.get("username")
    email = request.form.get("email")
    password = request.form.get("password")
    account_made = datetime.now()

    user = User(email=email,
                username=username,
                password=password,
                account_made=account_made)

    db.session.add(user)
    db.session.flush()
    db.session.commit()

    # Automatically log the new user in
    session['user_id'] = user.id
    flash("Welcome to WildAlly, {}!".format(username))

    # Check if the user is affiliated with an org:
    is_org = request.form.get("is_org")

    # If so, insert data into orgs and pickups:
    if is_org == 'yes':

        # Wildlife rehabilitator org fields:
        name = request.form.get("org_name")
        ein = request.form.get("ein")
        show_address = request.form.get("show_address")
        address1 = request.form.get("address1")
        address2 = request.form.get("address2")
        city = request.form.get("city")
        state = request.form.get("state")
        zipcode = request.form.get("zipcode")
        desc = request.form.get("desc")
        phone = request.form.get("phone")
        org_email = request.form.get("org_email")
        website = request.form.get("website")

        org = Org(user_id=user.id,
                  name=name,
                  ein=ein,
                  show_address=show_address,
                  address1=address1,
                  address2=address2,
                  city=city,
                  state=state,
                  zipcode=zipcode,
                  desc=desc,
                  phone=phone,
                  email=org_email,
                  website=website)

        db.session.add(org)
        db.session.flush()

        # Create geocode and insert coordinates into pickups:
        org = Org.query.get(org.id)

        if show_address == '1':
            address = [address1, address2, city, state, zipcode]
        else:
            address = [city, state, zipcode]

        coords = org.make_geocode()

        pickup = Pickup(org_id=org.id, latitude=coords[0], longitude=coords[1])

        db.session.add(pickup)
        db.session.commit()

    return redirect('/settings')
Example #9
0
def restore_org(orm, user, org_id, historic, d_time=None):
    LOG.info("Restoring org %d", org_id)

    d_time = historic and d_time or None
    org, org_v, d_time = get_deleted_entity(
        orm, Org, Org_v, "org_id", org_id, "org_v_id", d_time)
    d_time = historic and d_time or None

    if org:
        LOG.warning("Org %d already exists.", org_id)
        return org

    if not org_v:
        LOG.error(
            "Cannot restore org %d because no recently "
            "deleted versions exist.",
            org_id)
        return None

    LOG.info("deleted time: %s", d_time)

    org = Org(org_v.name,
              description=org_v.description,
              moderation_user=user,
              public=org_v.public)
    org.org_id = org_v.org_id
    del org_v
    orm.add(org)


    _orgalias_list, orgalias_v_list = get_deleted_entity_list(
        orm, Orgalias, Orgalias_v,
        "org_id", org_id, "orgalias_id", "orgalias_v_id", d_time)

    for orgalias_v in orgalias_v_list:
        orgalias = Orgalias(orgalias_v.name, org, user, orgalias_v.public)
        orgalias.orgalias_id = orgalias_v.orgalias_id


    orgtag_id_list = get_deleted_child_id_list(
        orm, "org_orgtag", "org_orgtag_v",
        "org_id", org_id, "orgtag_id", d_time)

    for orgtag_id in orgtag_id_list:
        orgtag = restore_orgtag(orm, user, orgtag_id)
        if orgtag in org.orgtag_list:
            if not orgtag:
                continue
            LOG.warning(
                "Not linking orgtag %s, already linked to this org.",
                orgtag_id)
            continue
        org.orgtag_list.append(orgtag)


    address_id_list = get_deleted_child_id_list(
        orm, "org_address", "org_address_v",
        "org_id", org_id, "address_id", d_time)

    for address_id in address_id_list:
        address = restore_address(orm, user, address_id, historic, d_time)
        if not address:
            continue
        if address.org_list or address.event_list:
            LOG.warning(
                "Not linking address %s, already linked to another entity.",
                address_id)
            continue
        if address in org.address_list:
            LOG.warning(
                "Not linking address %s, already linked to this org.",
                address_id)
            continue
        org.address_list.append(address)


    note_id_list = get_deleted_child_id_list(
        orm, "org_note", "org_note_v", "org_id", org_id, "note_id", d_time)

    for note_id in note_id_list:
        note = restore_note(orm, user, note_id, historic, d_time)
        if not note:
            continue
        if note in org.note_list:
            LOG.warning(
                "Not linking note %s, already linked to this org.", note_id)
            continue
        org.note_list.append(note)


    event_id_list = get_deleted_child_id_list(
        orm, "org_event", "org_event_v", "org_id", org_id, "event_id", d_time)

    for event_id in event_id_list:
        event = get_existing_entity(orm, Event, "event_id", event_id)
        if event:
            if event in org.event_list:
                LOG.warning(
                    "Not linking event %s, already linked to this org.",
                    event_id)
                continue
            org.event_list.append(event)
        else:
            LOG.warning("Not restoring link to deleted event %d.", event_id)


    return org
Example #10
0
def user_added():
    """New account form submission."""

    print request.form.get

    # Basic user fields:
    username = request.form.get("username")
    email = request.form.get("email")
    password = request.form.get("password")
    account_made = datetime.now()

    user = User(email=email,
                username=username,
                password=password,
                account_made=account_made)

    db.session.add(user)
    db.session.flush()
    db.session.commit()

    # Automatically log the new user in
    session['user_id'] = user.id
    flash("Welcome to WildAlly, {}!".format(username))

    # Check if the user is affiliated with an org:
    is_org = request.form.get("is_org")

    # If so, insert data into orgs and pickups:
    if is_org == 'yes':

        # Wildlife rehabilitator org fields:
        name = request.form.get("org_name")
        ein = request.form.get("ein")
        show_address = request.form.get("show_address")
        address1 = request.form.get("address1")
        address2 = request.form.get("address2")
        city = request.form.get("city")
        state = request.form.get("state")
        zipcode = request.form.get("zipcode")
        desc = request.form.get("desc")
        phone = request.form.get("phone")
        org_email = request.form.get("org_email")
        website = request.form.get("website")

        org = Org(user_id=user.id,
                    name=name,
                    ein=ein,
                    show_address=show_address,
                    address1=address1,
                    address2=address2,
                    city=city,
                    state=state,
                    zipcode=zipcode,
                    desc=desc,
                    phone=phone,
                    email=org_email,
                    website=website)

        db.session.add(org)
        db.session.flush()

        # Create geocode and insert coordinates into pickups:
        org = Org.query.get(org.id)

        if show_address == '1':
            address = [address1, address2, city, state, zipcode]
        else:
            address = [city, state, zipcode]

        coords = org.make_geocode()

        pickup = Pickup(org_id=org.id,
                        latitude=coords[0],
                        longitude=coords[1])

        db.session.add(pickup)
        db.session.commit()

    return redirect('/settings')
def insert_fast(
        data, orm,
        public=None, tag_names=None, dry_run=None, address_exclusive=None,
        search=True, org_id_whitelist=None
):
    user = orm.query(User).filter_by(user_id=-1).one()
    tag_names = tag_names or []

    tags = []
    for tag_name in tag_names:
        tag = Orgtag.get(
            orm,
            tag_name,
            moderation_user=user,
            public=public,
        )
        tags.append(tag)

    context = {
        "refresh": False,
        "user": user
    }
    for chunk in data:
        # pylint: disable=maybe-no-member
        has_address = None
        LOG.info("\n%s\n", chunk["name"])
        org = select_org(orm, chunk["name"], context, search)

        if (
                org is False or
                (org_id_whitelist and
                 ((not org) or (org.org_id not in org_id_whitelist)))
        ):
            LOG.info("Skipping org: %s", org and org.org_id)
            orm.rollback()
            continue

        if not org:
            LOG.warning("\nCreating org %s\n", chunk["name"])
            org = Org(chunk["name"], moderation_user=user, public=public,)
            orm.add(org)
            # Querying org address list on a new org would trigger a commit
            has_address = False
        else:
            has_address = bool(org.address_list)

        if tags:
            org.orgtag_list = list(set(tags + org.orgtag_list))

        if "tag" in chunk:
            for tag_name in chunk["tag"]:
                tag = Orgtag.get(
                    orm, tag_name,
                    moderation_user=user, public=public,
                )
                if tag not in org.orgtag_list:
                    org.orgtag_list.append(tag)

        if "address" in chunk and not (address_exclusive and has_address):
            for address_data in chunk["address"]:
                if address_data["postal"] in \
                        [address.postal for address in org.address_list]:
                    continue
                address = Address(
                    address_data["postal"], address_data["source"],
                    moderation_user=user, public=None,
                    )
                address.geocode()
                LOG.debug(address)
                orm.add(address)
                org.address_list.append(address)

        if "contact" in chunk:
            for contact_data in chunk["contact"]:
                text = sanitise_name(contact_data["text"])
                match = False
                for contact in org.contact_list:
                    if (
                            contact.text == text and
                            contact.medium.name == contact_data["medium"]
                    ):
                        match = True
                        break
                if match:
                    continue

                try:
                    medium = orm.query(Medium) \
                        .filter_by(name=contact_data["medium"]) \
                        .one()
                except NoResultFound:
                    LOG.warning("%s: No such medium", contact_data["medium"])
                    continue

                contact = Contact(
                    medium, text,
                    source=contact_data["source"],
                    moderation_user=user, public=None,
                )
                LOG.debug(contact)
                orm.add(contact)
                org.contact_list.append(contact)

        if "note" in chunk:
            for note_data in chunk["note"]:
                if note_data["text"] in [note.text for note in org.note_list]:
                    continue
                note = Note(
                    note_data["text"], note_data["source"],
                    moderation_user=user, public=None,
                    )
                LOG.debug(note)
                orm.add(note)
                org.note_list.append(note)

        if not (orm.new or orm.dirty or orm.deleted):
            LOG.info("Nothing to commit.")
            continue

        if dry_run is True:
            LOG.warning("rolling back")
            orm.rollback()
            continue

        LOG.info("Committing.")
        orm.commit()