def add_time(user_id, date, bug_id, project_id, hours, subject): # try finding existing entry for this bug session = DBSession() bug_id = str(bug_id) entry = TimeEntry.query.filter(TimeEntry.user_id==user_id) \ .filter(TimeEntry.date==date.date()) \ .filter(TimeEntry.ticket_id==bug_id) \ .filter(TimeEntry.project_id==project_id).first() if not entry: # create new entry entry = TimeEntry(user_id=user_id, date=date.date(), time=hours, description=subject, ticket_id=bug_id, project_id=project_id, modified_ts=date) session.add(entry) LOG(u'Adding new entry') else: # update existing entry if not entry.frozen: entry.time += hours entry.modified_ts = date # TODO: this might remove an already existing lateness session.add(entry) LOG(u'Updating existing entry') else: LOG(u'Omission of an existing entry because it is frozen')
def make_admin(config_path): from intranet3.models import DBSession, Base from intranet3.models import User user_login = sys.argv[-1] if len(sys.argv) < 4: print u"Provide user login" return session = DBSession() user = session.query(User).filter(User.email == user_login).first() if not user: print u"No such user: %s" % user_login return if 'admin' in user.groups: print u'Removing %s from group admin' % user.name groups = list(user.groups) groups.remove('admin') user.groups = groups else: print u'Adding %s to group admin' % user.name groups = list(user.groups) groups.append('admin') user.groups = groups session.add(user) transaction.commit()
def get_current_config(cls, allow_empty=False): """ @rtype: ApplicationConfig """ config = DBSession().query(cls).first() if config is not None: return config elif allow_empty: return None else: raise ValueError(u"Application config not found")
def on_retrieve(self, lines): """ When single message was retrieved """ msg = email.message_from_string('\n'.join(lines)) sender = decode(msg['From']) tracker = self.match_tracker(msg) if tracker is None: DEBUG(u'Email from %s ignored, no tracker matched' % (sender, )) return # find appopriate handler handler = getattr(self, 'handle_%s_email' % tracker.type) # handler should parse the response and return essential info or None data = handler(msg, tracker) if data is None: # email should be ignored return user_id, date, bug_id, project_id, hours, subject = data # try finding existing entry for this bug session = DBSession() bug_id = str(bug_id) entry = TimeEntry.query.filter(TimeEntry.user_id==user_id)\ .filter(TimeEntry.date==date.date())\ .filter(TimeEntry.ticket_id==bug_id)\ .filter(TimeEntry.project_id==project_id)\ .first() if not entry: # create new entry entry = TimeEntry( user_id=user_id, date=date.date(), time=hours, description=subject, ticket_id=bug_id, project_id = project_id, modified_ts=date ) session.add(entry) LOG(u'Adding new entry') else: # update existing entry if not entry.frozen: entry.time += hours entry.modified_ts = date # TODO: this might remove an already existing lateness session.add(entry) LOG(u'Updating existing entry') else: LOG(u'Omission of an existing entry because it is frozen') transaction.commit()
def migrate(config_path): from intranet3 import config from intranet3.models import * from sqlalchemy import func session = DBSession() results = session.query(User, func.count(Project.id), func.count(Client.id)) \ .outerjoin(Project, Project.coordinator_id==User.id) \ .outerjoin(Client, Client.coordinator_id==User.id) \ .group_by(User.id).all() for user, pc, cc in results: if pc + cc > 0 and 'coordinator' not in user.groups: groups = user.groups[:] groups.append('coordinator') user.groups = groups transaction.commit()
def __iter__(self): query = DBSession().query(Client.id, Client.name).order_by(Client.name) for client_id, client_name in query: yield str(client_id), client_name
def remove(config_path): from intranet3.models import * user_login = sys.argv[-1] session = DBSession() TrackerCredentials.query.delete() Sprint.query.delete() ac = ApplicationConfig.query.first() ac.office_ip = '127.0.0' ac.google_user_email = '' ac.google_user_password = '******' ac.freelancers = '' ac.hours_employee_project = '' ac.holidays_spreadsheet = '' session.add(ac) for i, client in enumerate(Client.query): print 'client %s' % i client.name = 'Client_%s' % i client.emails = '' client.google_card = '' client.google_wiki = '' client.selector = '' client.street = '' client.city = ' ' client.postcode = '' client.nip = '' client.mailing_url = '' client.wiki_url = '' client.note = '' session.add(client) for i, project in enumerate(Project.query): print 'project %s' % i project.name = 'Project_%s' % i project.project_selector = '' project.component_selector = '' project.version_selector = '' project.ticket_id_selector = '' project.google_card = '' project.google_wiki = '' project.mailing_url = '' project.working_agreement = '' project.definition_of_done = '' project.definition_of_ready = '' project.continuous_integration_url = '' project.backlog_url = '' session.add(project) for i, user in enumerate(User.query): print 'user %s' % i user.email = '*****@*****.**' % i user.name = 'User_%s' % i user.availability_link = '' user.tasks_link = '' user.skype = '' user.irc = '' user.phone = '' user.phone_on_desk = '' user.description = '' user.refresh_token = '' user._access_token = '' session.add(user) for i, timeentry in enumerate(TimeEntry.query): if i % 1000 == 0: print 'timeentry %s' % i timeentry.description = 'description %s' % i timeentry.ticket_id = i timeentry.time = round(random.uniform(0.1, 1.2), 2) session.add(timeentry) for i, tracker in enumerate(Tracker.query): print 'tracker %s' % i tracker.name = 'Tracker_%s' % i tracker.url = 'http://tracker%s.url.com' % i tracker.mailer = 'tracker_mailer_%s' % i session.add(tracker) transaction.commit()
def __init__(self, *args, **kwargs): self.db_session = DBSession() #those will be added to templates context, see subscribers.py self.tmpl_ctx = {} super(Request, self).__init__(*args, **kwargs)
def __init__(self, empty=False, inactive=False): self.empty = empty self.inactive = inactive self.session = DBSession()