def update_instance(modelname, instid): """Calls the update method in a single instance The ``request.data`` var will be loaded as a JSON and all of the fields are going to be passed to the .update() method. """ model = getattr(CONFIG['models'], modelname) try: data = loads(request.data) except JSONDecodeError: return dumps({'status': 'error', 'message': 'Unable to decode data'}) inst = model.get_by(id=instid) relations = set(update_relations(model, [inst], data)) field_list = set(data.keys()) ^ relations params = _validate_field_list(modelname, data, field_list) # We got an error :( if isinstance(params, basestring): return params # Let's update field by field for field in field_list: setattr(inst, field, params[field]) session.commit() return dumps({'status': 'ok', 'message': 'You rock!'})
def update(modelname): """Calls the .update() method in a set of results. The ``request.data`` field should be filled with a JSON string that contains an object with two fields: query and form. The first one should have an object that looks the same as the one passed to the ``search`` method. The second field (form) should contain an object with all fields that will be passed to the ``update()`` method. `modelname` The name of the model that the update will be done. """ model = getattr(CONFIG['models'], modelname) try: data = loads(request.data) except JSONDecodeError: return dumps({'status': 'error', 'message': 'Unable to decode data'}) query = _build_query(model, data.get('query', {})) relations = set(update_relations(model, query, data['form'])) field_list = set(data['form'].keys()) ^ relations params = _validate_field_list(modelname, data['form'], field_list) # We got an error :( if isinstance(params, basestring): return params # Let's update all instances present in the query if params: query.update(params, False) session.commit() return dumps({'status': 'ok', 'message': 'You rock!'})
def compress_smallest_box(): last_box = session.query(func.max(sqlalchemy.cast(InvCard.box, sqlalchemy.Integer))).first()[0] box_capacity = list(metadata.bind.execute("select box,60 - count(*) as c from inv_cards where box not null group by box having c>0 order by c desc;")) remove_box = box_capacity[0][0] box_capacity = box_capacity[1:] cards_in_remove_box = InvCard.query.filter_by(box=str(remove_box)).order_by(InvCard.box_index.desc()).all() move_orders = fit_boxes(box_capacity, len(cards_in_remove_box)) i=0 print "********** move %d cards from box %s **********" % (60-box_capacity[0][1], remove_box) print "\tall boxes: %s" % sorted([int(x) for x in [remove_box] + [b for b,o in move_orders]]) for box, count in move_orders: max_index = session.query(func.max(InvCard.box_index)).filter_by(box=box).one()[0] print "======= moving %d cards to box %s ======" % (count, box) for card in cards_in_remove_box[i:count+i]: print u"move %s to %s/%d" % (card, box, max_index) max_index += 1 card.box = box card.box_index = max_index i+=count if remove_box != last_box: cards_in_last_box = InvCard.query.filter_by(box=str(last_box)).order_by(InvCard.box_index).all() print "********** finally, move all %d cards from %s to %s **********" % (len(cards_in_last_box),last_box, remove_box) for card in cards_in_last_box: card.box = remove_box raw_input() session.commit()
def addItemsToDbGrade(self, grade): success = False try: grade = int(grade) if 0 < grade < 11: selection = self.db.character.filter(self.db.character.grade==grade).all() now = datetime.now() grade = u'grade' + str(grade) for kanji in selection: if session.query(Kanji).filter_by(character = kanji.literal).count() == 0: Kanji(character = kanji.literal, tags = grade, next_quiz = now, leitner_grade = Leitner.grades.None.index, active = True, current_session = False, been_in_session = 0) success = True try: session.commit() except IntegrityError: session.rollback() success = False except: success = False return success
def newsmuncher(): logging.basicConfig( level=logging.DEBUG ) logger.info("starting newsmuncher") argv = sys.argv ignore_last_update = False if "--ignore-last-update" in argv: ignore_last_update = True argv.remove("--ignore-last-update") dbfile = os.path.normpath(os.path.abspath(os.path.expanduser(argv[1]))) if dbfile.endswith(".ini"): dburi = dburi_from_ini(dbfile) else: dburi = "sqlite:///%s" % dbfile logger.debug("dburi: %s", dburi) configure_db(dburi) logger.debug("Existing entries so far:") for category, _ in CATEGORIES: logger.debug( "%s: %i", category, NewsEntry.query.filter_by( category=category, ).count() ) config = Config.instance() if not ignore_last_update and not config.can_run: logger.info("Next run possible in %s", config.next_run.isoformat()) return count = fetch_news() logger.info("Added %i news.", count) config.update() session.commit()
def reset_stats(): """ Reset selection statistics for every kanji. """ for kanji in Kanji.query.all(): kanji.picked = 0 session.commit()
def save_value(self, app_id, key, subkey, value): ''' Saves a value in the database. Creates the needed references of key and app if needed. @param app_id: string The identification of the requesting app @param key: string The key that will identify all given values @param subkey: string The subkey that identifies this specific value @param value: string Value to store @raise TypeProviderError: Notifies that the given key matches with another ResourceType ''' app = App.get_by(name=app_id) if app == None: app = App(name=app_id) key_db = Key.get_by(name=key,app_name=app_id) if key_db == None: key_db = Key(name=key,app=app, type_name=self.providerType) if key_db.type_name != self.providerType: raise TypeProviderError(self.providerType, key_db.type_name) value_db = Value() value_db.key = key_db value_db.subkey = subkey value_db.value = value session.commit()
def addSentenceToDb(self, kanji, sentence, translation): Kanji.query.filter_by(character = kanji).one().example.append(Example(sentence = sentence,translation = translation)) try: session.commit() except Exception, e: session.rollback() log.error(e)
def add_user(username, password): '''Add a user to the database. If it already exists, delete it first. Also mark it for cleanup later.''' delete_user(username, password) User(username=username, password=password) session.commit()
def install(): """ Install database an default values """ # Drop tables print("Dropping all tables...") drop_all() # Create tables print("Creating all tables...") create_all() # Create default data buildingA = Localisation(building=u"Batiment A", floor=1) buildingB = Localisation(building=u"Batiment B", floor=1, phone=u"5104") buildingC = Localisation(building=u"Batiment C", floor=2, phone=u"3388") Person(firstname=u"Stéphanie", lastname=u"De Monaco", birthdate=date(1980, 4, 4), localisation=buildingA) Person(firstname=u"Jean", lastname=u"Delarue", birthdate=date(1960, 10, 6), localisation=buildingA) Person(firstname=u"Jean-Pierre", lastname=u"Pernault", birthdate=date(1981, 7, 4), localisation=buildingB) Person(firstname=u"Anne", lastname=u"Sinclair", birthdate=date(1975, 8, 7), localisation=buildingC) Person(firstname=u"Julien", lastname=u"Lepers", birthdate=date(1975, 8, 7), localisation=buildingC) Team(name=u"Manchester") Team(name=u"Barça") Team(name=u"Racing Club de Strasbourg") session.commit() print("Installation success with data test")
def initializeCurrentSession(self, mode, sessionSize): #if mode == 'kanji': if mode == modes.kanji: selection = Kanji.query.filter_by(active = True).all() #elif mode == 'words': elif mode == modes.words: selection = Word.query.filter_by(active = True).all() else: selection = () # Kanji && Words? self.endCurrentSesion() n = sessionSize shuffle(selection) if n > len(selection) : n = len(selection) # without any try's and ValueErrors random_selection = sample(selection, n) # serious stuff ahead! for item in random_selection: # mark n random items as 'current' item.current_session = True item.been_in_session = item.been_in_session + 1 #may be useful later on try: session.commit() except Exception, e: session.rollback() log.error(e)
def add_run_to_db(tool, target, command, output='', output_file_path='', output_filetype='', start_time='', end_time='', subdir=''): if output_file_path: try: with open(output_file_path, "rb") as import_file: output_file = import_file.read() #print "Length = %s" %len(output_file) except: print "Could not read tool output file" output_file = '' else: output_file = '' ToolRun(tool, target, command, output, output_file, output_filetype, start_time, end_time, subdir) session.commit()
def task_stations(self, grab, task): stations = grab.xpath_list('//div[@class="dirlist"]') if grab.xpath_exists('//span[contains(text(), "show more")]'): yield self.new_stations_task(genre=task.genre, start_index=task.last + len(stations)) for dirlist in stations: info = dirlist.xpath('./div[1]/a[1]')[0] url, name = info.get('href'), info.get('name') stream = dirlist.xpath('./div[@class="dirtype"]/text()')[0] bitrate = dirlist.xpath('./div[@class="dirbitrate"]/text()')[0] stream = model.get_or_create(model.Stream, name=stream) bitrate = model.get_or_create(model.Bitrate, name=bitrate) station = model.get_or_create(model.Station, name=name, url=url, stream=stream, bitrate=bitrate) station.genres.append(task.genre) if stations: session.commit()
def updateJobStatus(self, jobID, status, msg): from tbl import EFJBLS from elixir import session session.close() obj = EFJBLS.query.filter_by(JBLSIDNM = jobID).first() pgmName = None if obj: pgmName = obj.JBLSPRPG now = dt.datetime.now() if obj.JBLSPRDT is None: obj.JBLSPRDT = now.date().tointeger() if obj.JBLSPRTM is None: obj.JBLSPRTM = now.time().tointeger() obj.JBLSUPDT = now.date().tointeger() obj.JBLSUPTM = now.time().tointeger() obj.JBLSPRST = status if msg: msg = msg[:64] obj.JBLSPRMS = msg if not session.transaction_started(): session.begin() try: session.update(obj) session.commit() except: session.rollback() raise return pgmName
def verify_scans(): if request.method == 'POST': #split_names = [name.split('_',1) + [val] for name,val in request.form] split_names = [name.split('_',1) + [val] for name,val in request.form.items()] by_rid = lambda (rid,x,y): rid d = {} for rid, args in groupby(sorted(split_names, key=by_rid), by_rid): d[rid] = dict([a[1:] for a in list(args)]) for rid, attribs in d.items(): card = InvCard.query.filter_by(rowid=rid).one() if card.name != attribs['name'] or card.set_name != attribs['set_name']: FixLog( card = card, orig_set = card.set_name, orig_name = card.name, new_set = attribs['set_name'], new_name = attribs['name'], scan_png = card.scan_png ) card.name = attribs['name'] card.set_name = attribs['set_name'] card.recognition_status = 'verified' session.commit() results = InvCard.query.filter_by(recognition_status='candidate_match').order_by('name').limit(50).all() return render_template('verify.html', cards=results)
def addItemsToDbJlpt(self, jlptGrade): success = False try: jlptGrade = int(jlptGrade) if 0 < jlptGrade < 5: selection = self.db.character.filter(self.db.character.jlpt==jlptGrade).all() #time for next quiz now = datetime.now() jlpt = u'jlpt' + str(jlptGrade) for kanji in selection: #check if already exists (time consuming?) #if len(Kanji.query.filter_by(character = kanji.literal).all()) == 0: if session.query(Kanji).filter_by(character = kanji.literal).count() == 0: # for easier management Kanji(character = kanji.literal, tags = jlpt, next_quiz = now, leitner_grade = Leitner.grades.None.index, active = True, current_session = False, been_in_session = 0) success = True try: session.commit() except IntegrityError: session.rollback() #is it ok in case just one item err'd? success = False except ValueError, e: log.error(e)
def test_poc_assocproxy(self): from datetime import datetime article = Article( author='unknown', release=datetime.now(), title='A Thousand and one nights', content='It has been related to me, O happy King, said Shahrazad') session.add(article) session.flush() session.expunge_all() article = Article.get(1) fr = article.add_locale('fr', title='Les mille et une nuits', content=u"J'ai entendu dire, Ô mon roi, dit Scheherazade") assert fr is not None session.commit() session.expunge_all() article = Article.get(1) author = article.get_localized('fr').author assert author is not None release = article.get_localized('fr').release assert release is not None
def synchronize(url=XML_URL_DATA_STATION): dom = parseString(Grabber(url).content) for marker in dom.getElementsByTagName('marker'): values = xml_station_information_wrapper(marker) StationInformation.update_or_create(values, surrogate=False) session.commit()
def compress_smallest_box(): last_box = session.query(func.max(sqlalchemy.cast(InvCard.box, sqlalchemy.Integer))).first()[0] box_capacity = list(metadata.bind.execute("select box,60 - count(*) as c from inv_cards where box not null group by box having c>0 order by c desc;")) if len(box_capacity) <= 0: raise Exception("there are no boxes in inventory to compress") remove_box = box_capacity[0][0] box_capacity = box_capacity[1:] cards_in_remove_box = InvCard.query.filter_by(box=str(remove_box)).order_by(InvCard.box_index.desc()).all() move_orders = fit_boxes(box_capacity, len(cards_in_remove_box)) i=0 print "********** move %d cards from box %s **********" % (60-box_capacity[0][1], remove_box) print "\tall boxes: %s" % sorted([int(x) for x in [remove_box] + [b for b,o in move_orders]]) for box, count in move_orders: max_index = session.query(func.max(InvCard.box_index)).filter_by(box=box).one()[0] print "======= moving %d cards to box %s ======" % (count, box) for card in cards_in_remove_box[i:count+i]: print u"move %s to %s/%d" % (card, box, max_index) max_index += 1 card.box = box card.box_index = max_index i+=count if remove_box != last_box: cards_in_last_box = InvCard.query.filter_by(box=str(last_box)).order_by(InvCard.box_index).all() print "********** finally, move all %d cards from %s to %s **********" % (len(cards_in_last_box),last_box, remove_box) for card in cards_in_last_box: card.box = remove_box raw_input() session.commit()
def addTask(self, title, note=None, tags=[], priority=1): task = Task(title=title, note=note, tags=self._createTags(tags), priority=priority) session.commit() return task
def addExamplesForKanji(self, kanji, examples): for example in examples: kanji.example.append(Example(sentence = example,translation = examples[example])) try: session.commit() except Exception, e: session.rollback() log.error(e)
def clean_up_db(): metadata.bind = 'sqlite:///simplemmo.sqlite' setup_all() create_all() for u in User.query.all(): print "Deleting %r" % u u.delete() session.commit()
def addTask(self, title, note = None, tags = [], priority = 1): task = Task( title = title, note = note, tags = self._createTags(tags), priority = priority) session.commit() return task
def commitChanges(self): try: session.commit() session.flush() except IntegrityError as e: session.rollback() raise DatabaseException(e, self.Entity)
def ensure(cls,title): if not title or title.strip()=='': return None genere = cls.query.filter(cls.title == title).first() if not genere: genere = cls(title = title) session.commit() return genere
def deleteTag(self, tag): """Delete tag by id or name""" if isinstance(tag, basestring): tag = Tag.query.filter_by(title = tag).one() else: tag = Tag.query.filter_by(id = tag).one() tag.delete() session.commit()
def toggleActive(self, item): item.active = not item.active item.current_session = not item.current_session try: session.commit() except Exception, e: session.rollback() log.error(e)
def testQueryRequirement(self): req = Requirement(u"FR", 1, u"New requirement") session.commit() req1 = Requirement.get_by(number=1) self.assertEqual(1, req1.number)
def ensure(cls, title, realname=None): if not title or title.strip() == "": return None artist = cls.query.filter(cls.title == title).first() if not artist: artist = cls(title=title, realname=realname) session.commit() return artist
def ensure(cls,title): if not title or title.strip()=='': return None category = cls.query.filter(cls.title == title).first() if not category: category = cls(title = title) session.commit() return category
def deleteTag(self, tag): """Delete tag by id or name""" if isinstance(tag, basestring): tag = Tag.query.filter_by(title=tag).one() else: tag = Tag.query.filter_by(id=tag).one() tag.delete() session.commit()
def test_delete_entity(self): fr = self.article.add_locale('fr', title='Les mille et une nuits', content=u"J'ai entendu dire, Ô mon roi, dit Scheherazade") session.commit() session.expunge_all() article = Article.get(1) session.delete(article) session.commit() assert session.query(Article.__localized_class__).count() == 0
def save_tweet(text): gt = MayorTweet.get_current() if gt is None: gt = MayorTweet() # Making sure we'll not save anything but unicode text. gt.text = unicode(text) session.commit()
def ensure(cls,title): if not title or title.strip()=='': return None lyricist = cls.query.filter(Lyricist.title == title).first() if not lyricist: lyricist = cls(title = title) session.commit() return lyricist
def patch(self, instid): """Updates the instance specified by ``instid`` of the named model, or updates multiple instances if ``instid`` is ``None``. The :attr:`flask.request.data` attribute will be parsed as a JSON object containing the mapping from field name to value to which to update the specified instance or instances. If ``instid`` is ``None``, the query string will be used to search for instances (using the :func:`_search` method), and all matching instances will be updated according to the content of the request data. See the :func:`_search` documentation on more information about search parameters for restricting the set of instances on which updates will be made in this case. """ patchmany = instid is None # if this was configured not to allow patching many, return HTTP 405 if patchmany and not self.allow_patch_many: return make_response(None, 405) # try to load the fields/values to update from the body of the request try: data = json.loads(request.data) except (TypeError, ValueError, OverflowError): return jsonify_status_code(400, message='Unable to decode data') # If there is no data to update, just return HTTP 204 No Content. if len(data) == 0: return make_response(None, 204) if patchmany: # create a SQLALchemy Query from the query parameter `q` query = create_query(self.model, data) else: # create a SQLAlchemy Query which has exactly the specified row query = self.model.query.filter_by(id=instid) assert query.count() == 1, 'Multiple rows with same ID' relations = self._update_relations(query, data) field_list = frozenset(data) ^ relations params = dict((field, data[field]) for field in field_list) # Special case: if there are any dates, convert the string form of the # date into an instance of the Python ``datetime`` object. params = self._strings_to_dates(params) # Let's update all instances present in the query num_modified = 0 if params: num_modified = query.update(params, False) session.commit() if patchmany: return jsonify(num_modified=num_modified) else: return self.get(instid)
def addKanjiToDb(self, character): if(len(Kanji.query.filter_by(character = character).all()) == 0): Kanji(character = character, tags = u'user', next_quiz = datetime.now(), leitner_grade = Leitner.grades.None.index, active = True, current_session = False, been_in_session = 0) try: session.commit() except Exception, e: session.rollback() log.error(e)
def tearDown(self): """Drops all tables from the temporary database and closes and unlink the temporary file in which it lived. """ drop_all() session.commit() os.close(self.db_fd) os.unlink(self.db_file)
def get_random(number): """ Get random kanji and increment it's stats. If provided number exceeds available kanji rank: will return None. """ # todo: should optimize if number <= Kanji.query.count(): kanji = Kanji.get(number) kanji.picked += 1 session.commit() return kanji
def add_domain_to_db(domain=''): #nothing passed to function - must be a user prompt if not domain: print "Enter domain to add (e.g. google.com)" domain = raw_input(" >> ") db_domain = Domain.query.filter_by(domain=domain).first() if not db_domain: db_domain = Domain(domain) session.commit()
def test_everything(self): '''This is a test version of the 'idealclient.py' file for ease of automation.''' # Make sure the server is up. response = self.fetch('/ping').body expected = 'pong' self.assertEqual(expected, response) # Insert our user. user = User.query.filter_by( username=settings.DEFAULT_USERNAME, password=settings.DEFAULT_PASSWORD).first() if not user: User(username=settings.DEFAULT_USERNAME, password=settings.DEFAULT_PASSWORD) session.commit() # Log in. data = { 'username': settings.DEFAULT_USERNAME, 'password': settings.DEFAULT_PASSWORD } response = self.fetch('/login', body=urlencode(data), method="POST") result = response.body expected = "Login successful." self.assertEqual(expected, result) cookie = response.headers['Set-Cookie'] self.assertTrue(cookie) headers = {'Cookie': cookie} # Get our characters. response = self.fetch('/characters', headers=headers) result = json.loads(response.body) self.assertTrue(len(result) > 0) character = result[0] # Get the zone our character is in: response = self.fetch('/zone/%s/zone' % character, headers=headers) result = json.loads(response.body) expected = {'zone': 'playerinstance-GhibliHills-%s' % (character, )} self.assertEqual(result, expected) zone = result['zone'] # Normally, the client would ask the masterzoneserver for the # url of the zone. This is not necessary for this test # since we already know where it is. # Get the zone's objects. response = self.fetch('/objects', headers=headers) self.fetch('/logout', headers=headers)
def update(self): for item in self.project_generator(): if model.Project.query.filter_by(url=item['link']).first(): continue category = self.get_category(item['category']) model.Project(name=item['title'], url=item['link'], description=item['description'], category=category, date=datetime.datetime.strptime( item['date'], "%a, %d %b %Y %H:%M:%S %Z"), site=self.site) session.commit()
def on_add_clicked(self): ui = self.ui operation = ui.operation.model().operation(ui.operation.currentIndex()) broker = ui.broker.model().getRow(ui.broker.currentIndex()) trade = Trade(stock=ui.stock.text(), date=ui.date.date().toPyDate(), type=operation, price=float(ui.price.text()), quantity=int(ui.quantity.text()), broker=broker) trade.calculateCost() session.commit() self.load_trades()
def reinsert_cards(): if request.method == 'POST': #post. reinsert the given rowids now = datetime.now() reason = request.form["reason"] if not reason: raise Exception("reason required") #get the cards rids=[] for key, val in request.form.items(): if key.startswith("reinsert_"): rids.append(int(key.split("_")[1])) cards = InvCard.query.filter(InvCard.rowid.in_(rids)).order_by('name').all() #make sure we can insert them if any(card.inventory_status != "temporarily_out" for card in cards): raise Exception("card is not temporarily out") box_capacity = list(metadata.bind.execute("select box,60 - count(*) as c from inv_cards where box not null and cast( box as int) != 0 group by box having c>0 order by c desc;")) #fill in each box with count cards i=0 fill_orders = fit_boxes(box_capacity, len(cards)) fill_orders = sorted(fill_orders, key=lambda (box,count): int(box)) for box, count in fill_orders: max_index = session.query(func.max(InvCard.box_index)).filter_by(box=box).one()[0] for card in cards[i:count+i]: max_index += 1 card.box = box card.box_index = max_index card.inventory_status = 'present' InvLog(card=card,date=now,direction='added',reason=reason) i+=count session.commit() #we're done. render the list. return render_template("results.html", cards=cards) else: #get the temporary_out cards to reinsert #it will be a list of ((date, reason), (cardlist)) tuples cards = InvCard.query.filter_by(inventory_status = "temporarily_out") the_key = lambda c: (c.most_recent_log().date, c.most_recent_log().reason) outstanding_cards = groupby(sorted(cards,key=the_key),the_key) outstanding_cards = [(key, sorted(val, key=attrgetter('name'))) for key, val in outstanding_cards] return render_template("outstanding_cards.html",outstanding_cards=outstanding_cards)
def remove_cards(): if request.method == 'POST': results = [] now = datetime.now() reason = request.form['reason'] if not reason: raise Exception("reason required") if not request.form['is_permanent']: raise Exception("is_permanent required") if request.form['is_permanent'] == 'yes': new_status = 'permanently_gone' else: new_status = 'temporarily_out' try: for key, val in request.form.items(): match = re.match('remove_(?P<num>\d+)', key) if not match: #if this isn't remove_id continue if not val: #if the browser passed us an unchecked checkbox continue rid = int(match.group('num')) card = InvCard.query.filter_by(rowid=rid).one() if card.inventory_status != "present": raise Exception("can't remove non-present card") results.append({ 'rowid': card.rowid, 'set_name': card.set_name, 'name': card.name, 'box': card.box, 'box_index': card.box_index}) card.box = None card.box_index = None card.inventory_status = new_status InvLog(card=card, direction='removed', reason=reason,date=now) session.commit() results = sorted(results, key = lambda r: (r['box'],r['box_index'])) return render_template("results.html", cards=results) except Exception as e: session.rollback() raise e
def check_project(self, project): if model.Project.query.filter_by(url=project['url']).first(): return category = None if 'category' in project: category = self.get_category(project['category']) model.Project( name=project.get('name', None), url=project['url'], description=project.get('description', None), project_type=project['type'], category=category, date=project.get('date', None), site=model.free_lance_ru ) session.commit()
def initializeCurrentSession(self, sessionSize): selection = Item.query.filter_by(active = True).all() self.endCurrentSesion() n = sessionSize shuffle(selection) if n > len(selection) : n = len(selection) random_selection = sample(selection, n) for item in random_selection: # mark n random items as 'current' item.current_session = True item.been_in_session = item.been_in_session + 1 #may be useful later on session.commit()
def captures_to_db(captures, box_name): #given an iterable of captures and a box name, #save all the captured images to the database starting_index = session.query(func.max(InvCard.box_index))\ .filter(InvCard.box==box_name).first()[0] if starting_index is None: starting_index = 0 for i, img in enumerate(captures): as_png = cv.EncodeImage(".png", img).tostring() InvCard(box=box_name, box_index=starting_index + i, scan_png=as_png, recognition_status="scanned", inventory_status="present") session.commit()
def add_host_to_db(ip=None, hostnames=[]): #nothing passed to function - must be a user prompt if not ip and not hostnames: print "Enter ip of target to add (leave blank if only a hostname)" ip = raw_input(" >> ") print "Enter hostname(s) of target to add, separated by spaces (leave blank if only an ip)" hostnames = raw_input(" >> ").split() db_host = Host.query.filter_by(ip=ip).first() if not db_host: db_host = Host(ip) for name in hostnames: db_hostname = Hostname.query.filter_by(hostname=name).filter( Hostname.hosts.any(Host.ip == ip)).first() if not db_hostname: db_hostname = Hostname(name, [db_host]) session.commit()
def rate_cards(): box = request.args.get('box') prev_box = str(int(box)-1) next_box = str(int(box)+1) if request.method == 'POST': d = parse_form_args(request) for rid, attribs in d.items(): card = InvCard.query.filter_by(rowid=rid).one() card.name = attribs['name'] card.set_name = attribs['set_name'] card.condition = attribs['condition'] card.is_foil = ('is_foil' in attribs) card.language = attribs['language'] session.commit() return redirect('/rate?box=%s'%next_box) else: cards = InvCard.query.filter_by(box=box).order_by('box_index').all() return render_template('rate.html', box=box, cards=cards, possible_conditions=['mint','near_mint','good','heavy_play'])
def addItemsToDb(self, min, max, exact=False, tag='user'): self.count = 0 success = False items = self.frequency.getFrequencyRange(min, max, exact) now = datetime.now() for item in items: if session.query(Item).filter_by(item = item).count() == 0: Item(item = item.lower(), tags = tag, next_quiz = now, leitner_grade = Leitner.grades.None.index, active = True, current_session = False, been_in_session = 0) self.count = self.count + 1 success = True try: session.commit() except IntegrityError: session.rollback() success = False return success
def extract_comments(): paragraphs = get_paragraphs() for i in paragraphs: # Creating the paragraph object. It will be stored in a # relational database by elixir+sqlalchemy :) paragrafo = Paragrafo() paragrafo.id = i # Building and dispatching the request to get comment data query = { 'method': 'get_paragraph_comments', 'params': [i, HARDCODED_POST_ID] } page = urllib.urlopen(API_URL % simplejson.dumps(query)) json = simplejson.loads(page.read()) for c in json: # Time to save collected things in the database comentario = Comentario() comentario.id = int(c['comment_ID']) comentario.navegador = c['comment_agent'] comentario.autor = c['comment_author'] comentario.autor_url = c['comment_author_url'] comentario.instituicao = c['instituicao'] comentario.contribuicao = c['meta']['contribuicao'] comentario.justificativa = c['meta']['justificativa'] comentario.opiniao = c['meta']['opiniao'] comentario.proposta = c['meta']['proposta'] comentario.data = datetime.strptime(c['comment_date'], '%Y-%m-%d %H:%M:%S') comentario.paragrafo = paragrafo # Time to add the comment tags for t in c['tags']: tag = Tag() tag.nome = t['name'] comentario.tags.append(tag) session.commit()
def verify_scans(): if request.method == 'POST': d = parse_form_args(request) for rid, attribs in d.items(): card = InvCard.query.filter_by(rowid=rid).one() if card.name != attribs['name'] or card.set_name != attribs['set_name']: FixLog( card = card, orig_set = card.set_name, orig_name = card.name, new_set = attribs['set_name'], new_name = attribs['name'], scan_png = card.scan_png ) card.name = attribs['name'] card.set_name = attribs['set_name'] card.recognition_status = 'verified' session.commit() results = InvCard.query.filter_by(recognition_status='candidate_match').order_by('name').limit(50).all() return render_template('verify.html', cards=results)
def save(self, obj): '''Updates object and commit session.''' obj.update() session.commit()
def removeAll(self): for task in Task.query.all(): task.delete() Tag.query.delete() session.commit()
def deleteTask(self, task_id): task = self.getTaskById(task_id) session.delete(task) session.commit()
def delete_user(username, password): '''Delete a user from the database.''' User.query.filter_by(username=username, password=password).delete() session.commit()