예제 #1
0
def get_client_users_out_stack(request):

    logger.debug('get_client_users_out_stack is running')

    client_id = request.matchdict.get('id', -1)
    client = Client.query.filter_by(id=client_id).first()
    if not client:
        transaction.abort()
        return Response('Can not find a client with id: %s' % client_id, 500)

    sql_query = """
            select
                "User_SimpleEntities".name,
                "User_SimpleEntities".id
            from "Users"
            left outer join "Client_Users" on "Client_Users".uid = "Users".id
            join "SimpleEntities" as "User_SimpleEntities" on "User_SimpleEntities".id = "Users".id

            where "Client_Users".cid != %(client_id)s or "Client_Users".cid is Null
    """

    sql_query = sql_query % {'client_id': client_id}
    result = DBSession.connection().execute(sql_query)

    users = []
    for r in result.fetchall():
        user = {'name': r[0], 'id': r[1]}
        users.append(user)

    resp = Response(json_body=users)

    return resp
예제 #2
0
def get_dailies_count(request):
    """missing docstring
    """
    project_id = request.matchdict.get('id')
    logger.debug(
        'get_dailies_count is working for the project which id is %s' %
        project_id)

    sql_query = """
select count(1) from (
select "Dailies".id

from "Projects"
join "Dailies" on "Dailies".project_id = "Projects".id
join "Statuses" on "Dailies".status_id = "Statuses".id

where "Statuses".code = 'OPEN' and "Projects".id = %(project_id)s
) as data
    """
    sql_query = sql_query % {'project_id': project_id}

    from sqlalchemy import text  # to be able to use "%" sign use this function
    result = DBSession.connection().execute(text(sql_query))

    return result.fetchone()[0]
예제 #3
0
def delete_budgetentry_action(budgetentry):

    logger.debug('delete_budgetentry_action %s' % budgetentry.name)
    budgetentry_name = budgetentry.name
    try:
        DBSession.delete(budgetentry)
        transaction.commit()
    except Exception as e:
        transaction.abort()
        c = StdErrToHTMLConverter(e)
        transaction.abort()
예제 #4
0
def get_studio_clients(request):
    """returns client with the given id
    """

    logger.debug('get_studio_clients is working for the studio')

    sql_query = """
         select
            "Clients".id,
            "Client_SimpleEntities".name,
            "Client_SimpleEntities".description,
            "Thumbnail_Links".full_path,
            projects.project_count
        from "Clients"
        join "SimpleEntities" as "Client_SimpleEntities" on "Client_SimpleEntities".id = "Clients".id
        left outer join "Links" as "Thumbnail_Links" on "Client_SimpleEntities".thumbnail_id = "Thumbnail_Links".id
        left outer join  (
            select "Projects".client_id as client_id,
                    count("Projects".id) as project_count
                from "Projects"
                group by "Projects".client_id)as projects on projects.client_id = "Clients".id
    """

    clients = []

    result = DBSession.connection().execute(sql_query)
    update_client_permission = \
        PermissionChecker(request)('Update_Client')

    for r in result.fetchall():
        client = {
            'id': r[0],
            'name': r[1],
            'description': r[2],
            'thumbnail_full_path': r[3],
            'projectsCount': r[4] if r[4] else 0
        }
        if update_client_permission:
            client['item_update_link'] = \
                '/clients/%s/update/dialog' % client['id']
            client['item_remove_link'] =\
                '/clients/%s/delete/dialog?came_from=%s' % (
                    client['id'],
                    request.current_route_path()
                )

        clients.append(client)

    resp = Response(json_body=clients)

    return resp
예제 #5
0
def inline_update_daily(request):
    """Inline updates the given daily with the data coming from the request
    """

    logger.debug('inline_update_daily is working')

    logged_in_user = get_logged_in_user(request)

    # *************************************************************************
    # collect data
    attr_name = request.params.get('attr_name', None)
    attr_value = request.params.get('attr_value', None)

    logger.debug('attr_name %s', attr_name)
    logger.debug('attr_value %s', attr_value)

    # get daily
    daily_id = request.matchdict.get('id', -1)
    daily = Daily.query.filter(Daily.id == daily_id).first()

    # update the daily
    if not daily:
        transaction.abort()
        return Response("No daily found with id : %s" % daily_id, 500)

    if attr_name and attr_value:

        logger.debug('attr_name %s', attr_name)

        if attr_name == 'status':

            status = Status.query.filter_by(code=attr_value).first()

            if not status:
                transaction.abort()
                return Response("No type found with id : %s" % attr_value, 500)

            daily.status = status
        else:
            setattr(daily, attr_name, attr_value)

        daily.updated_by = logged_in_user
        utc_now = local_to_utc(datetime.datetime.now())
        daily.date_updated = utc_now

    else:
        logger.debug('not updating')
        return Response("MISSING PARAMETERS", 500)

    return Response('Daily updated successfully %s %s' %
                    (attr_name, attr_value))
예제 #6
0
def get_budget_entries(request):
    """returns budgets with the given id
    """

    budget_id = request.matchdict.get('id')
    logger.debug(
        'get_budget_entries is working for the project which id is: %s' %
        budget_id)

    sql_query = """
        select
           "BudgetEntries_SimpleEntities".id,
           "BudgetEntries_SimpleEntities".name,
           "Types_SimpleEntities".name as type_name,
           "BudgetEntries".amount,
           "BudgetEntries".cost,
           "BudgetEntries".msrp,
           "BudgetEntries".price,
           "BudgetEntries".realized_total,
           "BudgetEntries".unit,
           "BudgetEntries_SimpleEntities".description,
           "BudgetEntries_SimpleEntities".generic_text
        from "BudgetEntries"
        join "SimpleEntities" as "BudgetEntries_SimpleEntities" on "BudgetEntries_SimpleEntities".id = "BudgetEntries".id
        join "SimpleEntities" as "Types_SimpleEntities" on "Types_SimpleEntities".id = "BudgetEntries_SimpleEntities".type_id
        join "Budgets" on "Budgets".id = "BudgetEntries".budget_id
        where "Budgets".id = %(budget_id)s
    """

    sql_query = sql_query % {'budget_id': budget_id}

    result = DBSession.connection().execute(sql_query)
    entries = [{
        'id': r[0],
        'name': r[1],
        'type': r[2],
        'amount': r[3],
        'cost': r[4],
        'msrp': r[5],
        'price': r[6],
        'realized_total': r[7],
        'unit': r[8],
        'note': r[9],
        'addition_type': r[10]
    } for r in result.fetchall()]

    resp = Response(json_body=entries)

    return resp
예제 #7
0
def update_budgetentry(request):
    """updates the budgetentry with data from request
    """

    logger.debug('***update_budgetentry method starts ***')
    logged_in_user = get_logged_in_user(request)
    utc_now = local_to_utc(datetime.datetime.now())

    budgetentry_id = request.params.get('id')
    budgetentry = BudgetEntry.query.filter_by(id=budgetentry_id).first()

    good = Good.query.filter(Good.name == budgetentry.name).first()
    # user supply this data
    amount = request.params.get('amount', None)
    price = request.params.get('price', None)
    if not price:
        transaction.abort()
        return Response('Please supply price', 500)
    price = int(price)
    description = request.params.get('note', '')

    if budgetentry.generic_text == 'Calendar':
        budgetentry.price = price
        budgetentry.description = description
        budgetentry.date_updated = utc_now
        budgetentry.updated_by = logged_in_user
    else:
        if not amount or amount == '0':
            transaction.abort()
            return Response('Please supply the amount', 500)

        amount = int(amount)
        budgetentry.amount = amount
        budgetentry.cost = good.cost
        budgetentry.msrp = good.msrp
        budgetentry.good = good
        # budgetentry.realized_total = good.msrp
        budgetentry.price = price if price != '0' else good.cost * amount
        budgetentry.description = description
        budgetentry.date_updated = utc_now
        budgetentry.updated_by = logged_in_user
        budgetentry.generic_text = 'Producer'

    request.session.flash('success:updated %s budgetentry!' % budgetentry.name)
    return Response('successfully updated %s budgetentry!' % budgetentry.name)
예제 #8
0
def append_user_to_client(request):

    logged_in_user = get_logged_in_user(request)
    utc_now = local_to_utc(datetime.datetime.now())

    came_from = request.params.get('came_from', '/')

    client_id = request.matchdict.get('id', -1)
    client = Client.query.filter(Client.id == client_id).first()
    if not client:
        transaction.abort()
        return Response('Can not find a client with id: %s' % client_id, 500)

    user_id = request.params.get('entity_id', -1)
    user = User.query.filter(User.id == user_id).first()
    if not user:
        transaction.abort()
        return Response('Can not find a user with id: %s' % user_id, 500)

    role_name = request.params.get('role_name', None)
    role = query_role(role_name)
    role.updated_by = logged_in_user
    role.date_created = utc_now

    logger.debug("%s role is created" % role.name)
    logger.debug(client.users)

    client_user = ClientUser()
    client_user.client = client
    client_user.role = role
    client_user.user = user
    client_user.date_created = utc_now
    client_user.created_by = logged_in_user

    DBSession.add(client_user)

    if user not in client.users:
        client.users.append(user)
        request.session.flash('success:%s is added to %s user list' %
                              (user.name, client.name))

    logger.debug(client.users)

    return Response('success:%s is added to %s.' % (user.name, client.name))
예제 #9
0
def create_client(request):
    """called when adding a new client
    """
    logged_in_user = get_logged_in_user(request)
    utc_now = local_to_utc(datetime.datetime.now())

    came_from = request.params.get('came_from', '/')

    # parameters
    name = request.params.get('name')
    description = request.params.get('description')

    logger.debug('create_client          :')

    logger.debug('name          : %s' % name)
    logger.debug('description   : %s' % description)

    if name and description:

        try:
            new_client = Client(name=name,
                                description=description,
                                created_by=logged_in_user,
                                date_created=utc_now,
                                date_updated=utc_now)

            DBSession.add(new_client)
            # flash success message
            request.session.flash(
                'success:Client <strong>%s</strong> is created '
                'successfully' % name)
        except BaseException as e:
            request.session.flash('error: %s' % e)
            HTTPFound(location=came_from)

    else:
        transaction.abort()
        return Response('There are missing parameters', 500)

    return Response(
        'success:Client with name <strong>%s</strong> is created.' % name)
예제 #10
0
def update_client(request):
    """called when updating a client
    """
    logged_in_user = get_logged_in_user(request)
    utc_now = local_to_utc(datetime.datetime.now())

    client_id = request.matchdict.get('id', -1)
    client = Client.query.filter_by(id=client_id).first()
    if not client:
        transaction.abort()
        return Response('Can not find a client with id: %s' % client_id, 500)

    # parameters
    name = request.params.get('name')
    description = request.params.get('description')

    logger.debug('create_client          :')

    logger.debug('name          : %s' % name)
    logger.debug('description   : %s' % description)

    if name and description:
        client.name = name
        client.description = description
        client.updated_by = logged_in_user
        client.date_updated = utc_now

        DBSession.add(client)

    else:
        transaction.abort()
        return Response('There are missing parameters', 500)

    request.session.flash('success:Client <strong>%s</strong> is updated '
                          'successfully' % name)

    return Response(
        'success:Client with name <strong>%s</strong> is updated.' % name)
예제 #11
0
def get_budgets(request):
    """returns budgets with the given id
    """

    project_id = request.matchdict.get('id')
    logger.debug('get_budgets is working for the project which id is: %s' %
                 project_id)

    status_code = request.params.get('status_code', None)
    status = Status.query.filter(Status.code == status_code).first()

    sql_query = """
        select
            "Budgets".id,
            "Budget_SimpleEntities".name,
            "Created_By_SimpleEntities".created_by_id,
            "Created_By_SimpleEntities".name,
            "Type_SimpleEntities".name,
            (extract(epoch from "Budget_SimpleEntities".date_created::timestamp at time zone 'UTC') * 1000)::bigint as date_created

        from "Budgets"
        join "SimpleEntities" as "Budget_SimpleEntities" on "Budget_SimpleEntities".id = "Budgets".id
        join "SimpleEntities" as "Created_By_SimpleEntities" on "Created_By_SimpleEntities".id = "Budget_SimpleEntities".created_by_id
        left outer join "SimpleEntities" as "Type_SimpleEntities" on "Type_SimpleEntities".id = "Budget_SimpleEntities".type_id
        join "Projects" on "Projects".id = "Budgets".project_id

        where "Projects".id = %(project_id)s %(additional_condition)s
    """

    additional_condition = ''
    if status:
        additional_condition = 'and "Budgets_Statuses".id=%s' % status.id

    budgets = []

    sql_query = sql_query % {
        'project_id': project_id,
        'additional_condition': additional_condition
    }

    result = DBSession.connection().execute(sql_query)
    update_budget_permission = \
        PermissionChecker(request)('Update_Budget')

    for r in result.fetchall():
        budget = {
            'id': r[0],
            'name': r[1],
            'created_by_id': r[2],
            'created_by_name': r[3],
            'item_view_link': '/budgets/%s/view' % r[0],
            'type_name': r[4],
            'date_created': r[5]
        }
        if update_budget_permission:
            budget['item_update_link'] = \
                '/budgets/%s/update/dialog' % budget['id']
            budget['item_remove_link'] =\
                '/budgets/%s/delete/dialog?came_from=%s' % (
                    budget['id'],
                    request.current_route_path()
                )

        budgets.append(budget)

    resp = Response(json_body=budgets)

    return resp
예제 #12
0
def get_daily_outputs(request):
    """missing docstring
    """

    daily_id = request.matchdict.get('id')
    logger.debug(
        'get_daily_outputs is working for the daily which id is : %s' %
        daily_id)

    sql_query = """
        select
            "Link_Tasks".id as task_id,
            "ParentTasks".full_path as task_name,
            "Task_Statuses".code as task_status_code,
            "Task_Status_SimpleEntities".name as task_status_name,
            array_agg(link_data.link_id) as link_id,

            array_agg(link_data.original_filename) as link_original_filename,
            array_agg(link_data.hires_full_path) as link_hires_full_path,
            array_agg(link_data.webres_full_path) as link_webres_full_path,
            array_agg(link_data.thumbnail_full_path) as link_thumbnail_full_path,

            array_agg(link_data.version_id) as version_id,
            array_agg(link_data.version_take_name) as version_take_name,
            array_agg(link_data.version_number) as version_number,
            array_agg(link_data.version_is_published) as version_is_published,

            daily_note.note_id as note_id,
            daily_note.entity_id as entity_id,
            daily_note.user_name as user_name,
            daily_note.user_thumbnail as user_thumbnail,
            daily_note.note_content as note_content,
            daily_note.note_date_created as note_date_created,
            daily_note.note_type_name as note_type_name,

            "Resource_SimpleEntities".name as resource_name,
            "Resource_SimpleEntities".id as resource_id

        from "Dailies"
        join "Daily_Links" on "Daily_Links".daily_id = "Dailies".id
        join (
            select
                "Tasks".id as task_id,
                "Versions".id as version_id,
                "Versions".take_name as version_take_name,
                "Versions".version_number as version_number,
                "Versions".is_published as version_is_published,
                "Links".id as link_id,
                "Links".original_filename as original_filename,

                "Links".full_path as hires_full_path,
                "Links_ForWeb".full_path as webres_full_path,
                "Thumbnails".full_path as thumbnail_full_path

            from "Links"
            join "SimpleEntities" as "Link_SimpleEntities" on "Link_SimpleEntities".id = "Links".id
            join "Links" as "Links_ForWeb" on "Link_SimpleEntities".thumbnail_id = "Links_ForWeb".id
            join "SimpleEntities" as "Links_ForWeb_SimpleEntities" on "Links_ForWeb".id = "Links_ForWeb_SimpleEntities".id
            join "Links" as "Thumbnails" on "Links_ForWeb_SimpleEntities".thumbnail_id = "Thumbnails".id
            join "Version_Outputs" on "Version_Outputs".link_id = "Links".id
            join "Versions" on "Versions".id = "Version_Outputs".version_id
            join "Tasks" on "Tasks".id = "Versions".task_id
        ) as link_data on link_data.link_id = "Daily_Links".link_id
        join "Tasks" as "Link_Tasks" on "Link_Tasks".id = link_data.task_id
        join "Statuses" as "Task_Statuses" on "Task_Statuses".id = "Link_Tasks".status_id
        join "SimpleEntities" as "Task_Status_SimpleEntities" on "Task_Status_SimpleEntities".id = "Link_Tasks".status_id
        join "Task_Resources" on "Task_Resources".task_id = "Link_Tasks".id
        join "SimpleEntities" as "Resource_SimpleEntities" on "Resource_SimpleEntities".id = "Task_Resources".resource_id
        --find the task daily notes
        left outer join (
            select
                "Task_Notes".entity_id as task_id,
                array_agg( "User_SimpleEntities".id) as entity_id,
                array_agg( "User_SimpleEntities".name) as user_name,
                array_agg( "Users_Thumbnail_Links".full_path) as user_thumbnail,
                array_agg( "Notes_SimpleEntities".id) as note_id,
                array_agg( "Notes_SimpleEntities".description) as note_content,
                array_agg( "Notes_SimpleEntities".date_created) as note_date_created,
                array_agg( "Notes_Types_SimpleEntities".id) as note_type_id,
                array_agg( "Notes_Types_SimpleEntities".name) as note_type_name

            from "Notes"
            join "SimpleEntities" as "Notes_SimpleEntities" on "Notes_SimpleEntities".id = "Notes".id
            left outer join "SimpleEntities" as "Notes_Types_SimpleEntities" on "Notes_Types_SimpleEntities".id = "Notes_SimpleEntities".type_id
            join "SimpleEntities" as "User_SimpleEntities" on "Notes_SimpleEntities".created_by_id = "User_SimpleEntities".id
            left outer join "Links" as "Users_Thumbnail_Links" on "Users_Thumbnail_Links".id = "User_SimpleEntities".thumbnail_id
            join "Entity_Notes" as "Daily_Notes" on "Daily_Notes".note_id = "Notes".id
            join "Entity_Notes" as "Task_Notes" on "Task_Notes".note_id = "Daily_Notes".note_id

            where "Daily_Notes".entity_id =  %(daily_id)s and "Notes_Types_SimpleEntities".name = 'Daily_Note'

            group by "Task_Notes".entity_id
        ) as daily_note on daily_note.task_id = "Link_Tasks".id

        left join (
            %(generate_recursive_task_query)s
        ) as "ParentTasks" on "Link_Tasks".id = "ParentTasks".id

        where "Dailies".id = %(daily_id)s

        group by
            "Link_Tasks".id,
            "ParentTasks".full_path,
            "Task_Statuses".code,
            "Task_Status_SimpleEntities".name,
            "Resource_SimpleEntities".name,
            "Resource_SimpleEntities".id,
            daily_note.note_id,
            daily_note.entity_id,
            daily_note.user_name,
            daily_note.user_thumbnail,
            daily_note.note_content,
            daily_note.note_date_created,
            daily_note.note_type_name
        order by
            "ParentTasks".full_path


    """
    sql_query = sql_query % {
        'daily_id': daily_id,
        'generate_recursive_task_query': generate_recursive_task_query()
    }

    result = DBSession.connection().execute(sql_query)

    tasks = []

    for r in result.fetchall():
        links = []

        link_ids = r[4]
        original_filename = r[5]
        hires_full_path = r[6]
        full_path = r[7]
        thumbnail_full_path = r[8]

        version_ids = r[9]
        version_take_names = r[10]
        version_numbers = r[11]
        versions_is_published = r[12]

        for i in range(len(link_ids)):
            link = {
                'id': link_ids[i],
                'original_filename': original_filename[i],
                'hires_full_path': hires_full_path[i],
                'webres_full_path': full_path[i],
                'thumbnail_full_path': thumbnail_full_path[i],
                'version_id': version_ids[i],
                'version_take_name': version_take_names[i],
                'version_number': version_numbers[i],
                'version_is_published': versions_is_published[i]
            }
            if link not in links:
                links.append(link)

        notes = []

        note_ids = r[13]
        note_created_by_ids = r[14]
        note_created_by_names = r[15]
        note_created_by_thumbnails = r[16]
        note_contents = r[17]
        note_created_dates = r[18]
        note_type_names = r[19]

        if note_ids:
            for j in range(len(note_ids)):
                if note_ids[j]:
                    note = {
                        'id':
                        note_ids[j],
                        'created_by_id':
                        note_created_by_ids[j],
                        'created_by_name':
                        note_created_by_names[j],
                        'created_by_thumbnail':
                        note_created_by_thumbnails[j],
                        'content':
                        note_contents[j],
                        'created_date':
                        milliseconds_since_epoch(note_created_dates[j]),
                        'note_type_name':
                        note_type_names[j]
                    }
                    if note not in notes:
                        notes.append(note)

        task = {
            'task_id': r[0],
            'task_name': r[1],
            'task_status_code': r[2].lower(),
            'task_status_name': r[3],
            'task_resource_name': r[20],
            'task_resource_id': r[21],
            'links': links,
            'notes': notes
        }
        tasks.append(task)

    resp = Response(json_body=tasks)

    return resp
예제 #13
0
def get_dailies(request):
    """returns dailies with the given id
    """

    project_id = request.matchdict.get('id')
    logger.debug('get_dailies is working for the project which id is: %s' %
                 project_id)

    status_code = request.params.get('status_code', None)
    status = Status.query.filter(Status.code == status_code).first()

    sql_query = """
        select
            "Dailies_SimpleEntities".id,
            "Dailies_SimpleEntities".name,
            "Dailies_Statuses".code,
            "Dailies_Statuses".id,
            "Dailies_Statuses_SimpleEntities".name,
            "Dailies_SimpleEntities".created_by_id,
            "Dailies_Creator_SimpleEntities".name,
            daily_count.link_count,
            (extract(epoch from "Dailies_SimpleEntities".date_created::timestamp at time zone 'UTC') * 1000)::bigint as date_created

        from "Projects"
        join "Dailies" on "Dailies".project_id = "Projects".id
        join "SimpleEntities" as "Dailies_SimpleEntities" on "Dailies_SimpleEntities".id = "Dailies".id
        join "SimpleEntities" as "Dailies_Creator_SimpleEntities" on "Dailies_Creator_SimpleEntities".id = "Dailies_SimpleEntities".created_by_id
        join "Statuses" as "Dailies_Statuses" on "Dailies_Statuses".id = "Dailies".status_id
        join "SimpleEntities" as "Dailies_Statuses_SimpleEntities" on "Dailies_Statuses_SimpleEntities".id = "Dailies".status_id

        left outer join (
            select
                "Daily_Links".daily_id as daily_id,
                count("Daily_Links".link_id) as link_count
            from "Daily_Links"
            join "Dailies" on "Dailies".id = "Daily_Links".daily_id
            group by "Daily_Links".daily_id
        ) as daily_count on daily_count.daily_id ="Dailies".id
        where "Projects".id = %(project_id)s %(additional_condition)s
    """

    additional_condition = ''
    if status:
        additional_condition = 'and "Dailies_Statuses".id=%s' % status.id

    sql_query = sql_query % {
        'project_id': project_id,
        'additional_condition': additional_condition
    }

    result = DBSession.connection().execute(sql_query)

    dailies = []

    update_daily_permission = \
        PermissionChecker(request)('Update_Daily')

    for r in result.fetchall():
        daily = {
            'id': r[0],
            'name': r[1],
            'status_code': r[2].lower(),
            'status_id': r[3],
            'status_name': r[4],
            'created_by_id': r[5],
            'created_by_name': r[6],
            'link_count': r[7] if r[7] else 0,
            'item_view_link': '/dailies/%s/view' % r[0],
            'date_created': r[8]
        }
        if update_daily_permission:
            daily['item_update_link'] = \
                '/dailies/%s/update/dialog' % daily['id']
            daily['item_remove_link'] =\
                '/entities/%s/delete/dialog?came_from=%s' % (
                    daily['id'],
                    request.current_route_path()
                )

        dailies.append(daily)

    resp = Response(json_body=dailies)

    return resp