Пример #1
0
 def __init__(self,
              name,
              website=None,
              events_url=None,
              members_count=None,
              rss=None,
              projects_list_url=None,
              type=None,
              city=None,
              latitude=None,
              longitude=None,
              last_updated=time.time()):
     self.name = name
     self.website = website
     self.events_url = events_url
     self.rss = rss
     self.projects_list_url = projects_list_url
     self.type = type
     self.city = city
     self.latitude = latitude
     self.longitude = longitude
     self.keep = True
     self.last_updated = last_updated
     self.started_on = unicode(date.today())
     self.id = safe_name(raw_name(name))
     self.members_count = members_count
Пример #2
0
def get_orgs_attendance(organization_name):
    ''' A clean url to get an organizations attendance '''

    # Get one named organization.
    organization = Organization.query.filter_by(
        name=raw_name(organization_name)).first()
    if not organization:
        return "Organization not found", 404

    attendance_response = {
        "organization_name": organization.name,
        "cfapi_url": organization.api_url(),
        "total": 0,
        "weekly": {}
    }

    # Get that organization's attendance
    attendance = Attendance.query.filter_by(
        organization_name=organization.name).first()

    if attendance:
        weekly = {}
        for week in attendance.weekly.keys():
            if week in weekly.keys():
                weekly[week] += attendance.weekly[week]
            else:
                weekly[week] = attendance.weekly[week]
        attendance.weekly = weekly

        attendance_response['total'] = attendance.total
        attendance_response['weekly'] = attendance.weekly

    return jsonify(attendance_response)
Пример #3
0
def get_orgs_attendance(organization_name):
    ''' A clean url to get an organizations attendance '''

    # Get one named organization.
    organization = Organization.query.filter_by(name=raw_name(organization_name)).first()
    if not organization:
        return "Organization not found", 404

    attendance_response = {
        "organization_name": organization.name,
        "cfapi_url": organization.api_url(),
        "total": 0,
        "weekly": {}
    }

    # Get that organization's attendance
    attendance = Attendance.query.filter_by(organization_name=organization.name).first()

    if attendance:
        weekly = {}
        for week in attendance.weekly.keys():
            if week in weekly.keys():
                weekly[week] += attendance.weekly[week]
            else:
                weekly[week] = attendance.weekly[week]
        attendance.weekly = weekly

        attendance_response['total'] = attendance.total
        attendance_response['weekly'] = attendance.weekly

    return jsonify(attendance_response)
Пример #4
0
def get_organizations(name=None):
    ''' Regular response option for organizations.
    '''

    filters, querystring = get_query_params(request.args)

    if name:
        # Get one named organization.
        org_filter = Organization.name == raw_name(name)
        org = db.session.query(Organization).filter(org_filter).first()
        if org:
            return jsonify(org.asdict(True))
        else:
            # If no org found
            return jsonify({"status": "Resource Not Found"}), 404

    # Get a bunch of organizations.
    query = db.session.query(Organization)
    # Default ordering of results
    ordering = desc(Organization.last_updated)

    for attr, value in filters.iteritems():
        if 'q' in attr:
            query = query.filter('organization.tsv_body @@ plainto_tsquery(:search_query)').params(search_query=value)
            ordering = desc(func.ts_rank(Organization.tsv_body, func.plainto_tsquery(value)))
        else:
            query = query.filter(getattr(Organization, attr).ilike(format_ilike_term(value)))

    query = query.order_by(ordering)
    response = paged_results(query=query, include_args=dict(include_extras=True), page=int(request.args.get('page', 1)), per_page=int(request.args.get('per_page', 10)), querystring=querystring)

    return jsonify(response)
Пример #5
0
def save_organization_info(session, org_dict):
    ''' Save a dictionary of organization info to the datastore session.

        Return an app.Organization instance.
    '''
    # Select an existing organization by name.
    filter = Organization.name == org_dict['name']
    existing_org = session.query(Organization).filter(filter).first()

    # :::here (organization/true)
    # If this is a new organization, save and return it. The keep parameter is True by default.
    if not existing_org:
        new_organization = Organization(**org_dict)
        session.add(new_organization)
        return new_organization

    # Check that the id exists
    if not existing_org.id:
        existing_org.id = safe_name(raw_name(existing_org.name))

    # Timestamp the existing organization
    existing_org.last_updated = time()
    # :::here (organization/true)
    existing_org.keep = True

    # Update existing organization details.
    for (field, value) in org_dict.items():
        setattr(existing_org, field, value)

    return existing_org
Пример #6
0
def get_orgs_projects(organization_name):
    '''
        A cleaner url for getting an organizations projects
    '''
    # Check org name
    organization = Organization.query.filter_by(name=raw_name(organization_name)).first()
    if not organization:
        return "Organization not found", 404

    filters, querystring = get_query_params(request.args)

    # Get project objects
    query = db.session.query(Project).filter_by(organization_name=organization.name).options(defer('tsv_body'))

    # Default ordering of results
    last_updated_ordering_filter = Project.last_updated
    relevance_ordering_filter = None
    ordering_filter_name = 'last_updated'
    ordering_filter = last_updated_ordering_filter
    ordering_dir = 'desc'
    ordering = None

    for attr, value in filters.iteritems():
        if 'q' in attr:
            # Returns all results if the value is empty
            if value:
                query = query.filter('project.tsv_body @@ plainto_tsquery(:search_query)').params(search_query=value)
                relevance_ordering_filter = func.ts_rank(Project.tsv_body, func.plainto_tsquery(value))
                ordering_filter_name = 'relevance'
        elif 'only_ids' in attr:
            query = query.with_entities(Project.id)
        elif 'sort_by' in attr:
            if value == 'relevance':
                ordering_filter_name = 'relevance'
            else:
                ordering_filter_name = 'last_updated'
        elif 'sort_dir' in attr:
            if value == 'asc':
                ordering_dir = 'asc'
            else:
                ordering_dir = 'desc'
        else:
            query = query.filter(getattr(Project, attr).ilike(format_ilike_term(value)))

    if ordering_filter_name == 'last_updated':
        ordering_filter = last_updated_ordering_filter
    elif ordering_filter_name == 'relevance' and dir(relevance_ordering_filter) != dir(None):
        ordering_filter = relevance_ordering_filter

    if ordering_dir == 'desc':
        ordering = ordering_filter.desc()
    else:
        ordering = ordering_filter.asc()
    query = query.order_by(ordering)

    response = paged_results(query=query, include_args=dict(include_organization=True, include_issues=True), page=int(request.args.get('page', 1)), per_page=int(request.args.get('per_page', 10)), querystring=querystring)
    return jsonify(response)
Пример #7
0
def gather_orgs_rsvps(organization_name=None):
    ''' Orgs rsvps summarized '''
    # Check org name
    organization = Organization.query.filter_by(name=raw_name(organization_name)).first()
    if not organization:
        return "Organization not found", 404
    orgs_events = Event.query.filter(Event.organization_name == organization.name).all()
    rsvps = build_rsvps_response(orgs_events)

    return jsonify(rsvps)
Пример #8
0
def get_upcoming_events(organization_name):
    '''
        Get events that occur in the future. Order asc.
    '''
    # Check org name
    organization = Organization.query.filter_by(name=raw_name(organization_name)).first()
    if not organization:
        return "Organization not found", 404
    # Get upcoming event objects
    query = Event.query.filter(Event.organization_name == organization.name, Event.start_time_notz >= datetime.utcnow())
    response = paged_results(query=query, include_args=dict(include_organization=True), page=int(request.args.get('page', 1)), per_page=int(request.args.get('per_page', 25)))
    return jsonify(response)
Пример #9
0
def get_orgs_stories(organization_name):
    '''
        A cleaner url for getting an organizations stories
    '''
    # Check org name
    organization = Organization.query.filter_by(name=raw_name(organization_name)).first()
    if not organization:
        return "Organization not found", 404

    # Get story objects
    query = Story.query.filter_by(organization_name=organization.name).order_by(desc(Story.id))
    response = paged_results(query=query, include_args=dict(include_organization=True), page=int(request.args.get('page', 1)), per_page=int(request.args.get('per_page', 25)))
    return jsonify(response)
Пример #10
0
def get_past_events(organization_name):
    '''
        Get events that occur in the past. Order desc.
    '''
    # Check org name
    organization = Organization.query.filter_by(name=raw_name(organization_name)).first()
    if not organization:
        return "Organization not found", 404
    # Get past event objects
    query = Event.query.filter(Event.organization_name == organization.name, Event.start_time_notz < datetime.utcnow()).\
        order_by(desc(Event.start_time_notz))
    response = paged_results(query, int(request.args.get('page', 1)), int(request.args.get('per_page', 25)))
    return jsonify(response)
Пример #11
0
def get_orgs_events(organization_name):
    '''
        A cleaner url for getting an organizations events
        Better than /api/events?q={"filters":[{"name":"organization_name","op":"eq","val":"Code for San Francisco"}]}
    '''
    # Check org name
    organization = Organization.query.filter_by(name=raw_name(organization_name)).first()
    if not organization:
        return "Organization not found", 404

    # Get event objects
    query = Event.query.filter_by(organization_name=organization.name)
    response = paged_results(query=query, include_args=dict(include_organization=True), page=int(request.args.get('page', 1)), per_page=int(request.args.get('per_page', 25)))
    return jsonify(response)
Пример #12
0
def get_orgs_issues(organization_name, labels=None):
    ''' A clean url to get an organizations issues
    '''
    # Get one named organization.
    organization = Organization.query.filter_by(
        name=raw_name(organization_name)).first()
    if not organization:
        return "Organization not found", 404

    # Get that organization's projects
    projects = Project.query.filter_by(
        organization_name=organization.name).all()
    project_ids = [project.id for project in projects]

    if labels:
        # Get all issues belonging to these projects
        query = Issue.query.filter(Issue.project_id.in_(project_ids))

        # Create a labels list by comma separating the argument
        labels = [label.strip() for label in labels.split(',')]

        # Create the filter for each label
        labels = [
            Label.name.ilike(format_ilike_term(label)) for label in labels
        ]

        # Create the base query object by joining on Issue.labels
        query = query.join(Issue.labels)

        # Filter for issues with each individual label
        label_queries = [query.filter(L) for L in labels]

        # Intersect filters to find issues with all labels
        query = query.intersect(*label_queries).order_by(func.random())

    else:
        # Get all issues belonging to these projects
        query = Issue.query.filter(Issue.project_id.in_(project_ids)).order_by(
            func.random())

    filters, querystring = get_query_params(request.args)
    response = paged_results(query=query,
                             include_args=dict(include_project=True,
                                               include_labels=True),
                             page=int(request.args.get('page', 1)),
                             per_page=int(request.args.get('per_page', 10)),
                             querystring=querystring)
    return jsonify(response)
Пример #13
0
 def __init__(self, name, website=None, events_url=None, members_count=None,
              rss=None, projects_list_url=None, type=None, city=None, latitude=None, longitude=None, last_updated=time.time()):
     self.name = name
     self.website = website
     self.events_url = events_url
     self.rss = rss
     self.projects_list_url = projects_list_url
     self.type = type
     self.city = city
     self.latitude = latitude
     self.longitude = longitude
     self.keep = True
     self.last_updated = last_updated
     self.started_on = unicode(date.today())
     self.id = safe_name(raw_name(name))
     self.members_count = members_count
Пример #14
0
 def __init__(self, name, **kwargs):
     self.name = name
     self.website = kwargs.get('website')
     self.events_url = kwargs.get('events_url')
     self.rss = kwargs.get('rss')
     self.projects_list_url = kwargs.get('projects_list_url')
     self.tags = kwargs.get('tags', [])
     self.type = kwargs.get('type')
     self.city = kwargs.get('city')
     self.latitude = kwargs.get('latitude')
     self.longitude = kwargs.get('longitude')
     self.keep = True
     self.last_updated = kwargs.get('last_updated', time.time())
     self.social_profiles = kwargs.get('social_profiles', {})
     self.started_on = unicode(date.today())
     self.id = safe_name(raw_name(name))
     self.members_count = kwargs.get('members_count')
     self.logo_url = kwargs.get('logo_url')
Пример #15
0
 def __init__(self, name, **kwargs):
     self.name = name
     self.website = kwargs.get('website')
     self.events_url = kwargs.get('events_url')
     self.rss = kwargs.get('rss')
     self.projects_list_url = kwargs.get('projects_list_url')
     self.tags = kwargs.get('tags', [])
     self.type = kwargs.get('type')
     self.city = kwargs.get('city')
     self.latitude = kwargs.get('latitude')
     self.longitude = kwargs.get('longitude')
     self.keep = True
     self.last_updated = kwargs.get('last_updated', time.time())
     self.social_profiles = kwargs.get('social_profiles', {})
     self.started_on = unicode(date.today())
     self.id = safe_name(raw_name(name))
     self.members_count = kwargs.get('members_count')
     self.logo_url = kwargs.get('logo_url')
Пример #16
0
def get_orgs_issues(organization_name, labels=None):
    ''' A clean url to get an organizations issues
    '''
    # Get one named organization.
    organization = Organization.query.filter_by(name=raw_name(organization_name)).first()
    if not organization:
        return "Organization not found", 404

    # Get that organization's projects
    projects = Project.query.filter_by(organization_name=organization.name).all()
    project_ids = [project.id for project in projects]

    if labels:
        # Get all issues belonging to these projects
        query = Issue.query.filter(Issue.project_id.in_(project_ids))

        # Create a labels list by comma separating the argument
        labels = [label.strip() for label in labels.split(',')]

        # Create the filter for each label
        labels = [Label.name.ilike(format_ilike_term(label)) for label in labels]

        # Create the base query object by joining on Issue.labels
        query = query.join(Issue.labels)

        # Filter for issues with each individual label
        label_queries = [query.filter(L) for L in labels]

        # Intersect filters to find issues with all labels
        query = query.intersect(*label_queries).order_by(func.random())

    else:
        # Get all issues belonging to these projects
        query = Issue.query.filter(Issue.project_id.in_(project_ids)).order_by(func.random())

    filters, querystring = get_query_params(request.args)
    response = paged_results(query=query, include_args=dict(include_project=True, include_labels=True), page=int(request.args.get('page', 1)), per_page=int(request.args.get('per_page', 10)), querystring=querystring)
    return jsonify(response)