def create_election(): if request.method == "POST": title = request.form.get("title").strip() voting_options = request.form.get("voting-options").strip() options = {} for i in voting_options.split("\n"): options[i.strip()] = 0 election = client.query( q.create( q.collection("elections"), { "data": { "creator": session["user"]["id"], "title": title, "voting_options": options, "date": datetime.now(pytz.UTC) } })) return redirect(url_for("vote", election_id=election["ref"].id())) return render_template("create-election.html")
def register(request): if request.method == "POST": username = request.POST.get("username").strip().lower() email = request.POST.get("email").strip().lower() password = request.POST.get("password") try: user = client.query(q.get(q.match(q.index("users_index"), username))) messages.add_message(request, messages.INFO, 'User already exists with that username.') return redirect("App:register") except: user = client.query(q.create(q.collection("Users"), { "data": { "username": username, "email": email, "password": hashlib.sha512(password.encode()).hexdigest(), "date": datetime.datetime.now(pytz.UTC) } })) messages.add_message(request, messages.INFO, 'Registration successful.') return redirect("App:login") return render(request,"register.html")
def getTopItems(hero_id, n): logging.info(f'Query: Fetching top {n} items for hero: {hero_id}') try: item_data = client.query(q.get(q.ref(q.collection("heroes"), hero_id))) logging.debug('Finished querying the heroes collection successfully') item_data = item_data['data']['items'] item_list = [] for i in range(1, len(item_data)): item_dict = {'item_id': i, 'item_count': item_data[i]} item_list.append(item_dict) item_list = sorted(item_list, key=lambda i: i['item_count'], reverse=True) logging.info('Returning from the function getTopItems') return item_list[0:n] except Exception as e: logging.error(e) logging.error('Could not fetch from heroes collection')
def do_GET(self): query = self.path.split('?', 1) params = parse_qs(query[1]) theme = params.get('theme', '')[0] m = pymorphy2.MorphAnalyzer() client = FaunaClient(secret=os.environ.get('DBSECRET')) futr_news = [] # futr_news = self.getNews('world', client, m) + self.getNews('nation', client, m) + self.getNews('scitech', client, m) futr_news = self.getNews(theme, client, m) if len(futr_news) > 0: client.query( q.map_( lambda post: q.create(q.collection('NewsPast'), {'data': post}), futr_news)) self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write(json.dumps(futr_news).encode()) return
def extractMatchesFeatureMatrix(prediction_ref_ids): try: logging.info(f'[START] Extracting Matches Feature Matrix') predictionData = client.query( q.map_( q.lambda_( 'pred_ref_id', q.get( q.ref(q.collection('match_prediction'), q.var('pred_ref_id')))), prediction_ref_ids)) featuresLists = [] for pred in predictionData: featuresLists.append(pred['data']['vector']) dataFrame = pd.DataFrame(featuresLists) dataFrame = dataFrame.iloc[:, :-2] logging.info(f'[FINISHED] Extracting Matches Feature Matrix') trainModel(dataFrame) except Exception as e: logging.error(f'Error occurred {str(e)}')
def testPrediction(): matchPreds = client.query( q.paginate(q.match(q.index('all_match_prediction')), size=100)) matchPredsData = matchPreds['data'] pred_ids = [] for pred in matchPredsData: pred_id = pred.value['id'] pred_ids.append(pred_id) predictionData = client.query( q.map_( q.lambda_( 'matchPred', q.get( q.ref(q.collection('match_prediction'), q.var('matchPred')))), pred_ids)) featuresLists = [] for pred in predictionData: featuresLists.append(pred['data']['vector']) dataFrame = pd.DataFrame(featuresLists) dataFrame = dataFrame.iloc[:, :-2] X_test = dataFrame.iloc[:, :-1] y_test = dataFrame.iloc[:, -1] clf = pickle.load(open('../data/model_file.p', 'rb')) print("Trained Model parameters:") print("Kernel: ", clf.kernel) print("Win Labels:", clf.classes_) print("Gamma:", clf.gamma) y_pred = clf.predict(X_test) acc = clf.score(X_test, y_test) print("Model Accuracy = ", acc * 100, "%")
def _translate_create_table( statement: token_groups.Statement, table_token_idx: int) -> typing.List[QueryExpression]: idx, table_identifier = statement.token_next_by(i=token_groups.Identifier, idx=table_token_idx) table_name = table_identifier.value idx, column_identifiers = statement.token_next_by( i=token_groups.Parenthesis, idx=idx) field_metadata = _extract_column_definitions(column_identifiers) index_queries = _create_table_indices(table_name, field_metadata) collection_metadata: CollectionMetadata = { "fields": field_metadata, "indexes": _create_index_metadata(table_name, field_metadata), } information_metadata_query = _update_information_metadata( table_name, collection_metadata) # Fauna creates resources asynchronously, so we cannot create and use a collection # in the same transaction, so we have to run the expressions that create # the collection and the indices that depend on it separately return [ *_make_sure_information_schema_exists(), q.create_collection({"name": table_name}), q.do( *index_queries, information_metadata_query, q.let( {"collection": q.collection(table_name)}, {"data": [{ "id": q.var("collection") }]}, ), ), ]
def add_activity(activity, event_id): data = client.query(q.get(q.ref(q.collection("event"), event_id)))['data'] data['damage'] *= 1.1 data['damage'] = int(data['damage']) activity['damage'] = data['damage'] data['activityLog'].append(activity) data['base'] -= data['damage'] if data['base'] <= 0: data['maxHealth'] *= 1.5 data['maxHealth'] = int(data['maxHealth']) data['base'] = data['maxHealth'] data['targetId'] = random.randint(0, 4) data['activityLog'].append({ "rawString": f"{activity['region']} has slain the dragon! A stronger foe has occupied {activity['region']}" }) print("\n\n\n\n", data, "\n\n\n\n") if not update_event(event_id, data): return 'f' print("\n\n\n\n", data, "\n\n\n\n") return 'u'
def start(update, context): chat_id = update.effective_chat.id first_name = update["message"]["chat"]["first_name"] username = update["message"]["chat"]["username"] try: client.query(q.get(q.match(q.index("users"), chat_id))) except: user = client.query( q.create( q.collection("users"), { "data": { "id": chat_id, "first_name": first_name, "username": username, "last_command": "", "date": datetime.now(pytz.UTC) } })) context.bot.send_message( chat_id=chat_id, text= "Welcome to Fauna TO-DO, your details have been saved 😊\n\nHere are a few commands to guide you\n/add_todo - add a todo task\n/list_todo - list a todo task" )
def __init__(self, collection_name: str, model): """ Each CRUD requires that: - Fauna client is connected to db - collection_name is created / exists - Collection is created / exists """ self.collection_name = collection_name self.model = model self.client = FaunaClient(secret=settings.FAUNADB_SECRET) if not self.client.query( query.exists(query.database( db_name=settings.FAUNADB_DBNAME, ))): self.database = self.client.query( query.create_database(db_params={ 'name': settings.FAUNADB_DBNAME, }), ) if not self.client.query( query.exists( query.collection(collection_name=collection_name, ))): self.collection = self.client.query( query.create_collection(collection_params={ 'name': collection_name, }), )
def create_document_in_collection(self, collection_data, collection="tweets"): """Assumes that collection name exists collections are halts and news and short_news Validation not needed, personal project <3 """ try: result = self.client.query( q.create(q.collection(collection), {"data": collection_data})) print(result) return True except BadRequest as error: # get properties of bad request # print(dir(bad)) if hasattr(error, "_get_description"): if error._get_description() == "document is not unique.": ticker = collection_data.get("ticker") print(f"skipping {ticker} since doc is not unique") return False # unknown error, stop everything except Exception as error: print(collection_data) raise Exception(error)
def test_remove(self): self.assertJson( query.remove(query.collection("widget"), ts=123, action="create"), '{"action":"create","remove":{"collection":"widget"},"ts":123}')
def test_delete(self): self.assertJson(query.delete(query.collection("widget")), '{"delete":{"collection":"widget"}}')
def test_singleton(self): self.assertJson( query.singleton(query.ref(query.collection("widget"), "1")), '{"singleton":{"id":"1","ref":{"collection":"widget"}}}')
def number_of_anime(update: Update, context: CallbackContext): result = client.query( q.count(q.paginate(q.documents(q.collection(animes)), size=100000))) context.bot.send_message(chat_id=update.effective_chat.id, text='Number of anime: ' + str(result['data'][0]))
def common_message(update, context): """ General response handler for quiz """ chat_id = update.effective_chat.id message = update.message.text user = client.query(q.get(q.match(q.index("users"), chat_id))) last_command = user["data"]["last_command"] try: name = update["message"]["chat"]["first_name"] except: name = update["message"]["chat"]["username"] quizzes = client.query(q.paginate(q.match(q.index("quiz"), chat_id))) #Gets uncompleted quiz quizzz = [] for i in quizzes["data"]: quiz_ = client.query(q.get(q.ref(q.collection("quiz"), i.id()))) if not quiz_["data"]["completed"]: quizzz.append(quiz_) quiz = quizzz[-1] #Gets questions' details questions_left = 0 questions = [] try: questions_left = 10 - int(quiz['data']['answered']) questions = quiz['data']['questions'] except: pass #Hnadles the last question and ends the quiz if questions_left == 1: if message == user['data']['answer']: score = int(quiz['data']['score']) + 1 else: score = int(quiz['data']['score']) answered = int(quiz['data']['answered']) + 1 client.query( q.update(q.ref(q.collection("quiz"), quiz["ref"].id()), {"data": { "score": score, "answered": answered }})) client.query( q.update(q.ref(q.collection("users"), user["ref"].id()), {"data": { "last_command": "end_quiz" }})) client.query( q.update(q.ref(q.collection("quiz"), quiz["ref"].id()), {"data": { "completed": True }})) corrections = '' x = 1 for question in questions: ans = f"{x}. {question['answer']}\n" corrections += ans x += 1 msg = f"Quiz completed\nScore: {score}/10 \nAnswers:\n{corrections}" context.bot.send_message(chat_id=chat_id, text=msg, reply_markup=telegram.ReplyKeyboardRemove()) botler = Botler().get() msg = message.get(name, botler) context.bot.send_message(chat_id=chat_id, text=msg) #Handles starting the quiz elif last_command == 'start_quiz' or message == 'q': print(questions_left) if questions_left > 1: answered = int(quiz['data']['answered']) question = questions[answered] title = question['question'] section = str(question['section']).capitalize() if section: try: context.bot.send_message(chat_id=chat_id, text=f"{section}\n\n{int(quiz['data']['answered']) + 1}. {title}\n\n" + \ '\n'.join(f'{aid}. {text}' for aid, text in sorted(question['option'].items())), reply_markup=telegram.ReplyKeyboardMarkup([[aid for aid in sorted(question['option'])]]), parse_mode="html") except: title = bs(title, 'lxml').txt context.bot.send_message(chat_id=chat_id, text=f"{section}\n\n{int(quiz['data']['answered']) + 1}. {title}\n\n" + \ '\n'.join(f'{aid}. {text}' for aid, text in sorted(question['option'].items())), reply_markup=telegram.ReplyKeyboardMarkup([[aid for aid in sorted(question['option'])]]), ) else: try: context.bot.send_message(chat_id=chat_id, text=f"{int(quiz['data']['answered']) + 1}. {title}\n\n" + \ '\n'.join(f'{aid}. {text}' for aid, text in sorted(question['option'].items())), reply_markup=telegram.ReplyKeyboardMarkup([[aid for aid in sorted(question['option'])]]), parse_mode="html") except: title = bs(title, 'lxml').txt context.bot.send_message(chat_id=chat_id, text=f"{int(quiz['data']['answered']) + 1}. {title}\n\n" + \ '\n'.join(f'{aid}. {text}' for aid, text in sorted(question['option'].items())), reply_markup=telegram.ReplyKeyboardMarkup([[aid for aid in sorted(question['option'])]]), ) client.query( q.update( q.ref(q.collection("users"), user["ref"].id()), { "data": { "last_command": "answer_quiz", "answer": question['answer'] } })) else: corrections = '' x = 1 for question in questions: ans = f"{x}. {question['answer']}\n" corrections += ans x += 1 msg = f"Quiz completed\nScore: {int(quiz['data']['score'])}/10 \nAnswers:\n{corrections}" context.bot.send_message( chat_id=chat_id, text=msg, reply_markup=telegram.ReplyKeyboardRemove()) client.query( q.update(q.ref(q.collection("quiz"), quiz["ref"].id()), {"data": { "completed": True }})) botler = Botler().get() msg = messenger.get(name, botler) context.bot.send_message(chat_id=chat_id, text=msg) #Receives answers and send next questions elif last_command == 'answer_quiz': print(message) if message == user['data']['answer']: score = int(quiz['data']['score']) + 1 else: score = int(quiz['data']['score']) answered = int(quiz['data']['answered']) + 1 client.query( q.update(q.ref(q.collection("quiz"), quiz["ref"].id()), {"data": { "score": score, "answered": answered }})) client.query( q.update(q.ref(q.collection("users"), user["ref"].id()), {"data": { "last_command": "start_quiz" }})) if questions_left > 1: answered = int(quiz['data']['answered']) question = questions[answered + 1] title = question['question'] section = str(question['section']).capitalize() if section: try: context.bot.send_message(chat_id=chat_id, text=f"{section}\n\n{int(quiz['data']['answered']) + 2}. {title}\n\n" + \ '\n'.join(f'{aid}. {text}' for aid, text in sorted(question['option'].items())), reply_markup=telegram.ReplyKeyboardMarkup([[aid for aid in sorted(question['option'])]]), parse_mode="html") except: title = bs(title, 'lxml').txt context.bot.send_message(chat_id=chat_id, text=f"{section}\n\n{int(quiz['data']['answered']) + 2}. {title}\n\n" + \ '\n'.join(f'{aid}. {text}' for aid, text in sorted(question['option'].items())), reply_markup=telegram.ReplyKeyboardMarkup([[aid for aid in sorted(question['option'])]]), ) else: try: context.bot.send_message(chat_id=chat_id, text=f"{int(quiz['data']['answered']) + 2}. {title}\n\n" + \ '\n'.join(f'{aid}. {text}' for aid, text in sorted(question['option'].items())), reply_markup=telegram.ReplyKeyboardMarkup([[aid for aid in sorted(question['option'])]]), parse_mode="html") except: title = bs(title, 'lxml').txt context.bot.send_message(chat_id=chat_id, text=f"{int(quiz['data']['answered']) + 2}. {title}\n\n" + \ '\n'.join(f'{aid}. {text}' for aid, text in sorted(question['option'].items())), reply_markup=telegram.ReplyKeyboardMarkup([[aid for aid in sorted(question['option'])]]), ) client.query( q.update( q.ref(q.collection("users"), user["ref"].id()), { "data": { "last_command": "answer_quiz", "answer": question['answer'] } })) else: corrections = '' x = 1 for question in questions: ans = f"{x}. {question['answer']}\n" corrections += ans x += 1 msg = f"Quiz completed\nScore: {score}/10 \nAnswerss:\n{corrections}" context.bot.send_message( chat_id=chat_id, text=msg, reply_markup=telegram.ReplyKeyboardRemove()) botler = botlers[random.randint(0, 9)] msg = f"Hello {name} \U0001F600,\nWelcome to Jambito, my name is {botler} and i will be your Quizbotler.\n \ \nMaths: /start_mathematics\nEnglish: /start_english\nChemistry: /start_chemistry\nPhysics: /start_physics\ \nCommerce: /start_commerce\nAccounting: /start_accounting\nGovernment: /start_government\nGeography: /start_geography \ \nEnglish Lit: /start_englishlit\nBiology: /start_biology\nCRK: /start_crk\nEconomics: /start_economics\nIRK: /start_irk \ \nInsurance: /start_insurance" context.bot.send_message(chat_id=chat_id, text=msg) else: context.bot.send_message(chat_id=chat_id, text="Unknown Command", reply_markup=telegram.ReplyKeyboardRemove())
def test_collection(self): self.assertJson(query.collection("collection-name"), '{"collection":"collection-name"}')
def test_login(self): self.assertJson( query.login(query.ref(query.collection("widget"), "1"), {"password": "******"}), '{"login":{"id":"1","ref":{"collection":"widget"}},"params":{"object":{"password":"******"}}}' )
def test_events(self): self.assertJson( query.events(query.ref(query.collection("widget"), "1")), '{"events":{"id":"1","ref":{"collection":"widget"}}}') self.assertJson(query.events(query.match(query.index("widget"))), '{"events":{"match":{"index":"widget"}}}')
### Delete a key #client.query(q.delete(q.ref(q.keys(), "243725029947212288"))) client.query(q.delete(q.ref(q.keys(), ref["ref"].id()))) client.query(q.delete(q.database("caledonia"))) ## Collections ### Create a collection client.query(q.create_collection({"name": "spells"})) ### Paginate all collections client.query(q.paginate(q.collections())) ### Get a collection client.query(q.get(q.collection("spells"))) ### Rename a collection client.query(q.update(q.collection("spells"), {"name": "dilapidated_huts"})) ### Delete a collection client.query(q.delete(q.collection("dilapidated_huts"))) ### Create a document in a collection client.query(q.create_collection({"name": "spells"})) client.query( q.create(q.collection("spells"), {"data": { "name": "Fire Beak", "element": ["air", "fire"] }}))
def test_identify(self): self.assertJson( query.identify(query.ref(query.collection("widget"), "1"), "abracadabra"), '{"identify":{"id":"1","ref":{"collection":"widget"}},"password":"******"}' )
def _define_match_set(query_filter: sql.Filter) -> QueryExpression: field_name = query_filter.column.name comparison_value = query_filter.value index_name_for_collection = functools.partial(index_name, query_filter.table_name) convert_to_collection_ref_set = functools.partial(convert_to_ref_set, query_filter.table_name) get_info_indexes_with_references = lambda collection_name, field_name: q.map_( q.lambda_("info_index_ref", q.get(q.var("info_index_ref"))), q.paginate( q.match( q.index( index_name( "information_schema_indexes_", column_name="name_", index_type=IndexType.TERM, )), index_name( collection_name, column_name=field_name, index_type=IndexType.REF, ), ), ), ) index_name_for_field = functools.partial(index_name_for_collection, field_name) equality_range = q.range( q.match(q.index(index_name_for_field(IndexType.VALUE))), [comparison_value], [comparison_value], ) if query_filter.checks_whether_equal: if field_name == "ref": assert isinstance(comparison_value, str) return q.singleton( q.ref(q.collection(query_filter.table_name), comparison_value)) return q.let( { "ref_index": q.index(index_name_for_field(IndexType.REF)), "term_index": q.index(index_name_for_field(IndexType.TERM)), "info_indexes": get_info_indexes_with_references(query_filter.table_name, field_name), "comparison_value": comparison_value, }, q.if_( q.exists(q.var("ref_index")), q.match( q.var("ref_index"), get_foreign_key_ref( q.var("comparison_value"), # Assumes that there is only one reference per foreign key # and that it refers to the associated collection's ID field # (e.g. {'associated_table': 'id'}). # This is enforced via NotSupported errors when creating collections. q.select([0, DATA, "referred_table_"], q.var("info_indexes")), ), ), q.if_( q.exists(q.var("term_index")), q.match( q.var("term_index"), q.var("comparison_value"), ), convert_to_collection_ref_set(equality_range), ), ), ) # In the building of Filter objects from SQL tokens, we enforce the convention # of <column name> <operator> <value> for WHERE clauses, so we build the FQL queries # assuming that '>' means 'column value greater than literal value'. I can't think # of a good way to centralize the knowledge of this convention across # all query translation, so I'm leaving this note as a warning. if query_filter.checks_whether_greater_than: inclusive_comparison_range = q.range( q.match(q.index(index_name_for_field(IndexType.VALUE))), [comparison_value], [], ) return convert_to_collection_ref_set( q.difference(inclusive_comparison_range, equality_range)) if query_filter.checks_whether_greater_than_or_equal: inclusive_comparison_range = q.range( q.match(q.index(index_name_for_field(IndexType.VALUE))), [comparison_value], [], ) return convert_to_collection_ref_set(inclusive_comparison_range) if query_filter.checks_whether_less_than: inclusive_comparison_range = q.range( q.match(q.index(index_name_for_field(IndexType.VALUE))), [], [comparison_value], ) return convert_to_collection_ref_set( q.difference(inclusive_comparison_range, equality_range)) if query_filter.checks_whether_less_than_or_equal: inclusive_comparison_range = q.range( q.match(q.index(index_name_for_field(IndexType.VALUE))), [], [comparison_value], ) return convert_to_collection_ref_set(inclusive_comparison_range) raise exceptions.NotSupportedError( f"Unsupported comparison {query_filter.comparison} was received.")
def test_documents(self): self.assertJson(query.documents(query.collection("users")), '{"documents":{"collection":"users"}}')
def test_create_collection(self): self._q(query.create_collection({"name": "collection_for_test"})) self.assertTrue( self._q(query.exists(query.collection("collection_for_test"))))
def _expr_collection(self): """ Returns collection query :return: query_collection: Expr - FaunaDB expression for collection """ return query.collection(collection_name=self.collection_name, )
def test_collection(self): self.assertEqual(self._q(query.collection("cls-name")), Ref("cls-name", Native.COLLECTIONS))
def number_of_users(update: Update, context: CallbackContext): user = User(update.effective_chat.id) result = client.query( q.count(q.paginate(q.documents(q.collection(users)), size=100000), )) context.bot.send_message(chat_id=user.chat_id, text='Number of users: ' + str(result['data'][0]))
def test_get(self): self.assertJson(query.get(query.collection("widget")), '{"get":{"collection":"widget"}}') self.assertJson(query.get(query.collection("widget"), ts=123), '{"get":{"collection":"widget"},"ts":123}')
def translate_drop( statement: token_groups.Statement) -> typing.List[QueryExpression]: """Translate a DROP SQL query into an equivalent FQL query. Params: ------- statement: An SQL statement returned by sqlparse. Returns: -------- An FQL query expression. """ idx, _ = statement.token_next_by(m=(token_types.Keyword, "TABLE")) _, table_identifier = statement.token_next_by(i=token_groups.Identifier, idx=idx) table_name = table_identifier.value deleted_collection = q.select("ref", q.delete(q.collection(table_name))) return [ q.do( q.map_( q.lambda_("ref", q.delete(q.var("ref"))), q.paginate( q.union( q.match( q.index( fql.index_name( "information_schema_tables_", column_name="name_", index_type=fql.IndexType.TERM, )), table_name, ), fql.convert_to_ref_set( "information_schema_columns_", q.range( q.match( q.index( fql.index_name( "information_schema_columns_", column_name="table_name_", index_type=fql.IndexType.VALUE, ))), [table_name], [table_name], ), ), fql.convert_to_ref_set( "information_schema_indexes_", q.range( q.match( q.index( fql.index_name( "information_schema_indexes_", column_name="table_name_", index_type=fql.IndexType.VALUE, ))), [table_name], [table_name], ), ), ), ), ), q.let( {"collection": deleted_collection}, {"data": [{ "id": q.var("collection") }]}, ), ) ]
def test_exists(self): self.assertJson(query.exists(query.collection("widget")), '{"exists":{"collection":"widget"}}') self.assertJson(query.exists(query.collection("widget"), ts=123), '{"exists":{"collection":"widget"},"ts":123}')