def studio_scheduling_mode(request): """Sets the system to "in schedule" mode or "normal" mode. When the system is "in schedule" mode (Studio.is_scheduling == True) it is not allowed to schedule the system again until the previous one is finishes. """ logged_in_user = get_logged_in_user(request) # get the studio studio = Studio.query.first() mode = request.params.get('mode') logger.debug('schedule mode: %s' % mode) if not studio: transaction.abort() return Response("There is no Studio instance\n" "Please create a studio first", 500) if mode: # set the mode mode = bool(int(mode)) studio.is_scheduling = mode studio.is_scheduling_by = logged_in_user studio.scheduling_started_at = local_to_utc(datetime.datetime.now()) return Response( "Successfully, set the scheduling mode to: %s" % mode )
def create_good(request): """creates a new Good """ logger.debug('***create good method starts ***') logged_in_user = get_logged_in_user(request) utc_now = local_to_utc(datetime.datetime.now()) came_from = request.params.get('came_from', '/') name = request.params.get('name', None) msrp = request.params.get('msrp', None) unit = request.params.get('unit', None) cost = request.params.get('cost', None) price_list_name = request.params.get('price_list_name', None) logger.debug('came_from : %s' % came_from) logger.debug('name : %s' % name) logger.debug('msrp : %s' % msrp) logger.debug('unit : %s' % unit) logger.debug('cost : %s' % cost) logger.debug('price_list_name : %s' % price_list_name) # create and add a new good if name and msrp and unit and cost and price_list_name: price_list = query_price_list(price_list_name) try: # create the new group new_good = Good(name=name, msrp=int(msrp), unit=unit, cost=int(cost), price_lists=[price_list]) new_good.created_by = logged_in_user new_good.date_created = utc_now new_good.date_updated = utc_now new_good.price_lists = [price_list] DBSession.add(new_good) logger.debug('added new good successfully') request.session.flash('success:Good <strong>%s</strong> is ' 'created successfully' % name) logger.debug('***create good method ends ***') except BaseException as e: request.session.flash('error: %s' % e) HTTPFound(location=came_from) else: logger.debug('not all parameters are in request.params') transaction.abort() return Response('There are missing parameters: ' 'name: %s' % name, 500) return Response('successfully created %s!' % name)
def auto_schedule_tasks(request): """schedules all the tasks of active projects """ logged_in_user = get_logged_in_user(request) # get the studio studio = Studio.query.first() if not studio: transaction.abort() return Response("There is no Studio instance\n" "Please create a studio first", 500) project_id = request.params.get('project', -1) project = Project.query.filter(Project.id == project_id).first() logger.debug('project_id: %s' % project_id) tj_scheduler = TaskJugglerScheduler() studio.scheduler = tj_scheduler if project: studio.scheduler.projects = [project] try: stderr = studio.schedule(scheduled_by=logged_in_user) # update schedule timings to UTC studio.scheduling_started_at = \ local_to_utc(studio.scheduling_started_at) studio.last_scheduled_at = local_to_utc(studio.last_scheduled_at) # invalidate cache regions from stalker_pyramid.views.task import cached_query_tasks,\ get_cached_user_tasks, get_cached_tasks_count invalidate_all_caches() check_all_tasks_status_by_schedule_model() c = StdErrToHTMLConverter(stderr) return Response(c.html(replace_links=True)) except RuntimeError as e: c = StdErrToHTMLConverter(e) transaction.abort() return Response(c.html(replace_links=True), 500)
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))
def create_entity_note(request): logger.debug('create_entity_note is running') entity_id = request.matchdict.get('id', -1) entity = Entity.query.filter(Entity.id == entity_id).first() content = request.params.get('message', '') utc_now = local_to_utc(datetime.datetime.now()) logged_in_user = get_logged_in_user(request) if not entity: transaction.abort() return Response('There is no entity with id: %s' % entity_id, 500) logger.debug('content %s' % content) if content != '': note_type = Type.query.filter_by(name='Simple Text').first() if note_type is None: # create a new Type note_type = Type( name='Simple Text', code='Simple_Text', target_entity_type='Note', html_class='grey' ) note = Note( content=content, created_by=logged_in_user, date_created=utc_now, date_updated=utc_now, type = note_type ) DBSession.add(note) entity.notes.append(note) return Response('Task note is created')
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)
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))
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)
def create_daily(request): """runs when creating a daily """ logged_in_user = get_logged_in_user(request) utc_now = local_to_utc(datetime.datetime.now()) name = request.params.get('name') description = request.params.get('description') status_id = request.params.get('status_id') status = Status.query.filter(Status.id == status_id).first() project_id = request.params.get('project_id', None) project = Project.query.filter(Project.id == project_id).first() if not name: return Response('Please supply a name', 500) if not description: return Response('Please supply a description', 500) if not status: return Response('There is no status with code: %s' % status_id, 500) if not project: return Response('There is no project with id: %s' % project_id, 500) daily = Daily(project=project, name=name, status=status, description=description, created_by=logged_in_user, date_created=utc_now, date_updated=utc_now) from stalker.db.session import DBSession DBSession.add(daily) return Response('Daily Created successfully')
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)
def update_budget(request): """runs when updating a budget """ logged_in_user = get_logged_in_user(request) utc_now = local_to_utc(datetime.datetime.now()) budget_id = request.matchdict.get('id', -1) budget = Budget.query.filter(Budget.id == budget_id).first() if not budget: transaction.abort() return Response('No budget with id : %s' % budget_id, 500) name = request.params.get('name') description = request.params.get('description') status_id = request.params.get('status_id') status = Status.query.filter(Status.id == status_id).first() if not name: return Response('Please supply a name', 500) if not description: return Response('Please supply a description', 500) if not status: return Response('There is no status with code: %s' % status.code, 500) budget.name = name budget.description = description budget.status = status budget.date_updated = utc_now budget.updated_by = logged_in_user request.session.flash('success: Successfully updated budget') return Response('Successfully updated budget')
def update_daily(request): """runs when updating a daily """ logged_in_user = get_logged_in_user(request) utc_now = local_to_utc(datetime.datetime.now()) daily_id = request.matchdict.get('id', -1) daily = Daily.query.filter(Daily.id == daily_id).first() if not daily: transaction.abort() return Response('No daily with id : %s' % daily_id, 500) name = request.params.get('name') description = request.params.get('description') status_id = request.params.get('status_id') status = Status.query.filter(Status.id == status_id).first() if not name: return Response('Please supply a name', 500) if not description: return Response('Please supply a description', 500) if not status: return Response('There is no status with code: %s' % status.code, 500) daily.name = name daily.description = description daily.status = status daily.date_updated = utc_now daily.updated_by = logged_in_user request.session.flash('success: Successfully updated daily') return Response('Successfully updated daily')
def update_good(request): """updates the good with data from request """ logger.debug('***update good method starts ***') logged_in_user = get_logged_in_user(request) utc_now = local_to_utc(datetime.datetime.now()) good_id = request.params.get('id') good = Good.query.filter_by(id=good_id).first() if not good: transaction.abort() return Response('There is no good with id: %s' % good_id, 500) name = request.params.get('name', None) msrp = request.params.get('msrp', None) unit = request.params.get('unit', None) cost = request.params.get('cost', None) price_list_name = request.params.get('price_list_name', None) logger.debug('name : %s' % name) logger.debug('msrp : %s' % msrp) logger.debug('unit : %s' % unit) logger.debug('cost : %s' % cost) if name and msrp and unit and cost: price_list = query_price_list(price_list_name) # update the group assert isinstance(good, Good) good.name = name good.msrp = int(msrp) good.unit = unit good.cost = int(cost) good.price_lists = [price_list] good.updated_by = logged_in_user good.date_updated = utc_now DBSession.add(good) logger.debug('good is updated successfully') request.session.flash( 'success:Good <strong>%s</strong> is updated successfully' % name) logger.debug('***update group method ends ***') else: logger.debug('not all parameters are in request.params') log_param(request, 'group_id') log_param(request, 'name') response = Response( 'There are missing parameters: ' 'good_id: %s, name: %s' % (good_id, name), 500) transaction.abort() return response response = Response('successfully updated %s good!' % name) return response
def update_ticket(request): """runs when updating a ticket """ logged_in_user = get_logged_in_user(request) ticket_id = request.matchdict.get('id', -1) ticket = Ticket.query.filter_by(id=ticket_id).first() #************************************************************************** # collect data comment = request.params.get('comment') comment_as_text = request.params.get('comment_as_text') action = request.params.get('action') logger.debug('updating ticket') if not ticket: transaction.abort() return Response('No ticket with id : %s' % ticket_id, 500) utc_now = local_to_utc(datetime.datetime.now()) ticket_log = None if not action.startswith('leave_as'): if logged_in_user == ticket.owner or \ logged_in_user == ticket.created_by: if action.startswith('resolve_as'): resolution = action.split(':')[1] ticket_log = ticket.resolve(logged_in_user, resolution) elif action.startswith('set_owner'): user_id = int(action.split(':')[1]) assign_to = User.query.get(user_id) ticket_log = ticket.reassign(logged_in_user, assign_to) elif action.startswith('delete_resolution'): ticket_log = ticket.reopen(logged_in_user) ticket.date_updated = utc_now if ticket_log: ticket_log.date_created = utc_now ticket_log.date_updated = utc_now else: transaction.abort() return Response( 'Error: You are not the owner nor the creator of this ticket' '\n\nSo, you do not have permission to update the ticket', 500 ) # mail recipients = [ logged_in_user.email, ticket.created_by.email, ticket.owner.email ] # mail the comment to anybody related to the ticket if comment: # convert images to Links attachments = [] comment, links = replace_img_data_with_links(comment) if links: # update created_by attributes of links for link in links: link.created_by = logged_in_user # manage attachments link_full_path = convert_file_link_to_full_path(link.full_path) link_data = open(link_full_path, "rb").read() link_extension = os.path.splitext(link.filename)[1].lower() mime_type = '' if link_extension in ['.jpeg', '.jpg']: mime_type = 'image/jpg' elif link_extension in ['.png']: mime_type = 'image/png' attachment = Attachment( link.filename, mime_type, link_data ) attachments.append(attachment) DBSession.add_all(links) note = Note( content=comment, created_by=logged_in_user, date_created=utc_now ) ticket.comments.append(note) DBSession.add(note) # send email to the owner about the new comment mailer = get_mailer(request) # also inform ticket commenter for t_comment in ticket.comments: recipients.append(t_comment.created_by.email) message_body_text = "%(who)s has added a the following comment to " \ "%(ticket)s:\n\n%(comment)s" message_body_html = "<div>%(who)s has added a the following comment " \ "to %(ticket)s:<br><br>%(comment)s</div>" message_body_text = message_body_text % { 'who': logged_in_user.name, 'ticket': "Ticket #%s" % ticket.number, 'comment': comment_as_text } message_body_html = message_body_html % { 'who': '<a href="%(link)s">%(name)s</a>' % { 'link': request.route_url('view_user', id=logged_in_user.id), 'name': logged_in_user.name }, 'ticket': '<a href="%(link)s">%(name)s</a>' % { 'link': request.route_url('view_ticket', id=ticket.id), 'name': "Ticket #%(number)s - %(summary)s" % { 'number': ticket.number, 'summary': ticket.summary } }, 'comment': re.sub( r'/SPL/[a-z0-9]+/[a-z0-9]+/', 'cid:', comment ) } # make recipients unique recipients = list(set(recipients)) message = Message( subject="Stalker Pyramid: New comment on Ticket #%s" % ticket.number, sender=dummy_email_address, recipients=recipients, body=message_body_text, html=message_body_html, attachments=attachments ) mailer.send(message) # mail about changes in ticket status if ticket_log: from stalker import TicketLog assert isinstance(ticket_log, TicketLog) mailer = get_mailer(request) # just inform anybody in the previously created recipients list message_body_text = \ '%(user)s has changed the status of %(ticket)s\n\n' \ 'from "%(from)s" to "%(to)s"' message_body_html = \ '<div>%(user)s has changed the status of ' \ '%(ticket)s:<br><br>from %(from)s to %(to)s</div>' message_body_text = message_body_text % { 'user': ticket_log.created_by.name, 'ticket': "Ticket #%s" % ticket.number, 'from': ticket_log.from_status.name, 'to': ticket_log.to_status.name } message_body_html = message_body_html % { 'user': '******' % { 'name': ticket_log.created_by.name }, 'ticket': "<strong>Ticket #%(number)s - %(summary)s</strong>" % { 'number': ticket.number, 'summary': ticket.summary }, 'from': '<strong>%s</strong>' % ticket_log.from_status.name, 'to': '<strong>%s</strong>' % ticket_log.to_status.name } message = Message( subject="Stalker Pyramid: Status Update on " "Ticket #%(ticket_number)s - %(ticket_summary)s" % { 'ticket_number': ticket.number, 'ticket_summary': ticket.summary }, sender=dummy_email_address, recipients=recipients, body=message_body_text, html=message_body_html ) mailer.send(message) logger.debug('successfully updated ticket') request.session.flash('Success: Successfully updated ticket') return Response('Successfully updated ticket')
def create_ticket(request): """runs when creating a ticket """ logged_in_user = get_logged_in_user(request) #************************************************************************** # collect data description = request.params.get('description') summary = request.params.get('summary') project_id = request.params.get('project_id', None) project = Project.query.filter(Project.id == project_id).first() owner_id = request.params.get('owner_id', None) owner = User.query.filter(User.id == owner_id).first() priority = request.params.get('priority', "TRIVIAL") type_name = request.params.get('type') send_email = request.params.get('send_email', 1) # for testing purposes logger.debug('*******************************') logger.debug('create_ticket is running') logger.debug('project_id : %s' % project_id) logger.debug('owner_id : %s' % owner_id) logger.debug('owner: %s' % owner) if not summary: return Response('Please supply a summary', 500) if not description: return Response('Please supply a description', 500) if not type_name: return Response('Please supply a type for this ticket', 500) type_ = Type.query.filter_by(name=type_name).first() if not project: return Response('There is no project with id: %s' % project_id, 500) if owner_id: if not owner: # there is an owner id but no resource found return Response('There is no user with id: %s' % owner_id, 500) else: return Response('Please supply an owner for this ticket', 500) link_ids = get_multi_integer(request, 'link_ids') links = Task.query.filter(Task.id.in_(link_ids)).all() # we are ready to create the time log # Ticket should handle the extension of the effort utc_now = local_to_utc(datetime.datetime.now()) ticket = Ticket( project=project, summary=summary, description=description, priority=priority, type=type_, created_by=logged_in_user, date_created=utc_now, date_updated=utc_now ) ticket.links = links ticket.set_owner(owner) # email the ticket to the owner and to the created by if send_email: # send email to responsible and resources of the task mailer = get_mailer(request) recipients = [logged_in_user.email, owner.email] # append link resources for link in links: for resource in link.resources: recipients.append(resource.email) # make recipients unique recipients = list(set(recipients)) description_text = \ 'A New Ticket for project "%s" has been created by %s with the ' \ 'following description:\n\n%s' % ( project.name, logged_in_user.name, description ) # TODO: add project link, after the server can be reached outside description_html = \ 'A <strong>New Ticket</strong> for project <strong>%s</strong> ' \ 'has been created by <strong>%s</strong> and assigned to ' \ '<strong>%s</strong> with the following description:<br><br>%s' % ( project.name, logged_in_user.name, owner.name, description.replace('\n', '<br>') ) message = Message( subject='New Ticket: %s' % summary, sender=dummy_email_address, recipients=recipients, body=description_text, html=description_html ) mailer.send(message) DBSession.add(ticket) return Response('Ticket Created successfully')