def update_location_by_id(id, ld): with get_db().transaction() as t: q = t.query(_Location).filter(_Location.id == id) location = q.one() ld.map_out(location) return get_location_by_id(id)
def create_card(**kwargs): with get_db().transaction() as t: card = _Card(**kwargs) t.add(card) t.commit() id = card.id return get_card_by_id(id)
def get_user_by_name(name): with get_db().transaction() as t: try: user = (t.query(_User).filter(_User.name == name).one()) return User.map_in(user) except NoResultFound: raise bottle.HTTPError(404)
def map_out(self, entry, system=False): entry.original_filename = self.original_filename entry.export_filename = self.export_filename entry.source = self.source entry.import_location_id = self.import_location_id entry.state = self.state entry.taken_ts = (datetime.datetime.strptime( self.taken_ts, '%Y-%m-%d %H:%M:%S').replace(microsecond=0) if self.taken_ts else None ) if self.deleted and self.delete_ts is None: self.delete_ts = (( datetime.datetime.utcnow() + datetime.timedelta(hours=DELETE_AFTER) ).strftime('%Y-%m-%d %H:%M:%S')) elif self.deleted is False and self.delete_ts is not None: self.delete_ts = None entry.delete_ts = (datetime.datetime.strptime( self.delete_ts, '%Y-%m-%d %H:%M:%S').replace(microsecond=0) if self.delete_ts else None ) entry.access = self.access entry.data = self.metadata.to_json() if self.metadata else None entry.physical_data = ( self.physical_metadata.to_json() if self.physical_metadata else None ) entry.user_id = self.user_id entry.parent_entry_id = self.parent_entry_id entry.latitude = self.latitude entry.longitude = self.longitude with get_db().transaction() as t: entry.tags = [t.query(_Tag).get(tag.id) for tag in self.tags] entry.files = [t.query(_File).get(file.id) for file in self.files]
def fail_import(entry, reason): logging.error(reason) with get_db().transaction() as t: e = t.query(_Entry).get(entry.id) e.state = _Entry.State.import_failed metadata = wrap_raw_json(e.data) or _Entry.DefaultMetadata() metadata.error = reason e.data = metadata.to_json()
def delete_entry_by_id(id, system=False): with get_db().transaction() as t: q = t.query(_Entry).filter(_Entry.id == id) if not system: q = q.filter( (_Entry.user_id == current_user_id()) | (_Entry.access >= _Entry.Access.common) ) q.delete()
def create_location(ld): with get_db().transaction() as t: location = _Location() ld.map_out(location) t.add(location) t.commit() id = location.id return get_location_by_id(id)
def get_names_by_run(run): authenticate_cookie() with get_db().transaction() as t: tokens = t.query(_Token).filter(_Token.run == run).all() return { '*schema': 'Names', 'entries': [token.name for token in tokens], 'count': len(tokens), }
def get_tag_by_id(id): with get_db().transaction() as t: tag = t.query(_Tag).filter(_Tag.id == id).one() return Tag( id=tag.id, color_id=tag.color, background_color=colors[tag.color][0], foreground_color=colors[tag.color][1], color_name=colors[tag.color][2], )
def update_entry_by_id(id, ed, system=False): with get_db().transaction() as t: q = t.query(_Entry).filter(_Entry.id == id) if not system: q = q.filter( (_Entry.user_id == current_user_id()) | (_Entry.access >= _Entry.Access.common) ) entry = q.one() ed.map_out(entry) return get_entry_by_id(id)
def create_file(f): with get_db().transaction() as t: try: _f = _File() f.map_out(_f) t.add(_f) t.commit() id = _f.id return get_file_by_id(id) except IntegrityError: t.rollback() raise _File.ConflictException()
def get_entry_by_source(source, filename, system=False): with get_db().transaction() as t: q = t.query(_Entry).filter( _Entry.source == source, _Entry.original_filename == filename ) if not system: q = q.filter( (_Entry.user_id == current_user_id()) | (_Entry.access >= _Entry.Access.public) ) entry = q.one() return Entry.map_in(entry)
def create_entry(ed, system=False): with get_db().transaction() as t: entry = _Entry() for tag in ed.tags: ensure_tag(tag) ed.map_out(entry, system=system) t.add(entry) t.commit() id = entry.id return get_entry_by_id(id)
def add_tag(td): if not td.id: raise ValueError("Tag id must not be empty") with get_db().transaction() as t: tag = _Tag() tag.id = td.id.lower() tag.color = td.color_id if td.color_id is not None else random.randrange( 0, len(colors)) t.add(tag) t.commit() id = tag.id return get_tag_by_id(id)
def get_tags(): with get_db().transaction() as t: tags = t.query(_Tag).order_by(_Tag.id).all() return TagFeed(count=len(tags), entries=[ Tag( id=tag.id, color_id=tag.color, background_color=colors[tag.color][0], foreground_color=colors[tag.color][1], color_title=colors[tag.color][2], ) for tag in tags ])
def create_token(name, run): with get_db().transaction() as t: _token = _Token( name=name, run=run, token=''.join(random.SystemRandom().choice( string.ascii_lowercase + string.ascii_uppercase + string.digits ) for _ in range(32)) ) t.add(_token) token = Token.map_in(_token) logging.debug("Adding token for %s (running as %s).", token.name, token.run) return token
def pick_up_import_ready_entry(location_id): with get_db().transaction() as t: try: entry = t.query(_Entry).filter( _Entry.import_location_id == location_id, _Entry.state == _Entry.State.import_ready, ).order_by(_Entry.create_ts).first() if entry is None: return None entry.state = _Entry.State.importing return Entry.map_in(entry) except NoResultFound: return None
def ensure_tag(tag_id): """ Check if a tag with id `tag_id` exists. If not, create it. """ if not tag_id: raise ValueError("Tag id must not be empty") with get_db().transaction() as t: try: t.query(_Tag).filter(_Tag.id == tag_id).one() except NoResultFound: tag = _Tag() tag.id = tag_id tag.color = random.randrange(0, len(colors)) t.add(tag)
def create_token(name, run): with get_db().transaction() as t: _token = _Token( name=name, run=run, token=''.join(random.SystemRandom().choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(32))) t.add(_token) token = Token.map_in(_token) logging.debug("Adding token for %s (running as %s).", token.name, token.run) return token
def authenticate(token_string): if hasattr(bottle.request, 'token'): token = bottle.request.token logging.debug("Already authenticated as %s (running as %s).", token.name, token.run) return with get_db().transaction() as t: token = t.query(_Token).get(token_string) if token is None: raise bottle.HTTPError(401) bottle.request.token = Token.map_in(token) bottle.response.set_cookie('token', token_string, path='/') logging.debug("Authenticated as %s (running as %s).", token.name, token.run)
def sync_records(): run = bottle.request.token.run test_cases = list_testcases() for category in test_cases.entries: for test_case in category.entries: with get_db().transaction() as t: q = (t.query(_Record).filter( _Record.run == run, _Record.test_category == category.name, _Record.test_name == test_case.name)) if q.count() == 0: logging.debug('Creating record for %s/%s/%s' % (run, category.name, test_case.name)) r = _Record( run=run, test_category=category.name, test_name=test_case.name, state=int(State.not_run), ) t.add(r)
def basic_auth(username, password): """ Bottle-compatible simple-checker that stores the user descriptor of the currently logged in user onto the request. """ with get_db().transaction() as t: try: user = (t.query(_User).filter(_User.name == username).filter( _User.password == password_hash(password)).filter( _User.status == _User.Status.enabled).one()) bottle.request.user = User( id=user.id, status=_User.Status(user.status), name=user.name, fullname=user.fullname, user_class=_User.Class(user.user_class), ) logging.debug("Logged in as %s", user.name) return True except NoResultFound: return False
def op_record(category, test, state): if category == 'undefined' or test == 'undefined': raise bottle.HTTPError(400) run = bottle.request.token.run assignee = bottle.request.token.name state = getattr(State, state) if bottle.request.json is not None: comment = bottle.request.json.get('comment', None) or None jira = bottle.request.json.get('jira', None) or None else: comment = None jira = None with get_db().transaction() as t: r = _Record( run=run, assignee=assignee, test_category=category, test_name=test, state=int(state), comment=comment, jira=jira, ) t.add(r) return Record( run=run, assignee=assignee, test_name=test, test_category=category, state=state, comment=comment, jira=jira, url='%s/%s/%s' % (TestcaseApp.BASE, category, test), history_url='%s/history/%s/%s' % (App.BASE, category, test), ).to_dict()
def sync_records(): run = bottle.request.token.run test_cases = list_testcases() for category in test_cases.entries: for test_case in category.entries: with get_db().transaction() as t: q = ( t.query(_Record) .filter( _Record.run == run, _Record.test_category == category.name, _Record.test_name == test_case.name ) ) if q.count() == 0: logging.debug('Creating record for %s/%s/%s' % (run, category.name, test_case.name)) r = _Record( run=run, test_category=category.name, test_name=test_case.name, state=int(State.not_run), ) t.add(r)
def delete_location_by_id(id): with get_db().transaction() as t: t.query(_Location).filter(_Location.id == id).delete()
def create_box(**kwargs): with get_db().transaction() as t: _box = _Box(**kwargs) t.add(_box) box = Box.map_in(_box) return box
def get_locations(): with get_db().transaction() as t: locations = t.query(_Location).all() return LocationFeed( count=len(locations), entries=[Location.map_in(location) for location in locations])
def get_locations_by_type(*types): with get_db().transaction() as t: locations = t.query(_Location).filter(_Location.type.in_(types)).all() return LocationFeed( count=len(locations), entries=[Location.map_in(location) for location in locations])
def get_location_by_name(name): with get_db().transaction() as t: location = t.query(_Location).filter(_Location.name == name).one() return Location.map_in(location)
def get_location_by_id(id): with get_db().transaction() as t: location = t.query(_Location).filter(_Location.id == id).one() return Location.map_in(location)
def get_card_by_id(card_id): with get_db().transaction() as t: _card = t.query(_Card).filter(_Card.id == card_id).one() card = Card.map_in(_card) return card
def get_box_by_id(box_id): with get_db().transaction() as t: _box = t.query(_Box).filter(Box.id == box_id).one() box = Box.map_in(_box) return box
def get_records(history=False, only_category=None, only_test_case=None): run = bottle.request.token.run with get_db().transaction() as t: q = t.query(_Record).filter(_Record.run == run) if only_category is not None: q = q.filter(_Record.test_category == only_category) if only_test_case is not None: q = q.filter(_Record.test_name == only_test_case) q = q.order_by( _Record.test_category.desc(), _Record.test_name.desc(), _Record.ts.asc(), ) records = q.all() per_category = {} count = 0 for _record in records: record = Record.map_in(_record) if record.test_category not in per_category.keys(): per_category[record.test_category] = {} if history: if record.test_name not in per_category[record.test_category].keys(): per_category[record.test_category][record.test_name] = [] count += 1 per_category[record.test_category][record.test_name].append(record) else: if record.test_name not in per_category[record.test_category]: count += 1 per_category[record.test_category][record.test_name] = record if history: return RecordFeedWithHistory( history=True, count=count, entries=[ RecordCategoryWithHistory( name=category, entries=[ RecordHistory( name=name, entries=records, current=records[-1], ) for name, records in sorted(per_name.items()) ] ) for category, per_name in sorted(per_category.items()) ] ) else: for category, records in sorted(per_category.items()): print(category) print(records) return RecordFeed( history=False, count=count, entries=[ RecordCategory( name=category, entries=[record for name, record in sorted(records.items())], ) for category, records in sorted(per_category.items()) ] )
def get_file_by_id(id): with get_db().transaction() as t: f = t.query(_File).get(id) return File.map_in(f)
def delete_file_by_id(id): with get_db().transaction() as t: t.query(_File).filter(_File.id == id).delete()