def test_intersection(self): self.assertJson(query.intersection(), '{"intersection":[]}') self.assertJson(query.intersection(query.index("widget")), '{"intersection":{"index":"widget"}}') self.assertJson( query.intersection(query.index("widget"), query.index("things")), '{"intersection":[{"index":"widget"},{"index":"things"}]}')
def fetch_matches_from_db(afterPtr, isFirstPage): try: all_matches = [] if isFirstPage: all_matches = client.query( q.paginate(q.match(q.index("all_raw_matches")), size=10000)) else: all_matches = client.query( q.paginate(q.match(q.index("all_raw_matches")), size=10000, after=afterPtr)) all_matches_data = all_matches['data'] match_ids = [] for match in all_matches_data: match_id = match.value['id'] match_ids.append(match_id) enqueue_match_ids(match_ids) if 'after' in all_matches: all_matches_after = all_matches['after'] fetch_matches_from_db(all_matches_after, False) except Exception as e: logging.error(f'Error occurred {str(e)}')
def _fetch_column_info_refs(table_name: str, column_name: str): convert_to_collection_ref_set = functools.partial( fql.convert_to_ref_set, "information_schema_columns_") return q.intersection( convert_to_collection_ref_set( 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, ), ), convert_to_collection_ref_set( q.range( q.match( q.index( fql.index_name( "information_schema_columns_", column_name="name_", index_type=fql.IndexType.VALUE, )), ), column_name, column_name, ), ), )
def get_multiple(index, data=None): """ Get multiple records by ID """ try: serverClient = FaunaClient( secret=os.environ.get("FAUNA_SERVER_SECRET")) res_arr = [] if data is None: res = serverClient.query( q.map_(q.lambda_("data", q.get(q.var("data"))), q.paginate(q.match(q.index(index))))) res_arr.extend(res["data"]) elif isinstance(data, list): for x in data: res = serverClient.query( q.map_(q.lambda_("data", q.get(q.var("data"))), q.paginate(q.match(q.index(index), q.casefold(x))))) res_arr.extend(res["data"]) else: res = serverClient.query( q.map_(q.lambda_("data", q.get(q.var("data"))), q.paginate(q.match(q.index(index), q.casefold(data))))) res_arr.extend(res["data"]) arr = [] for x in res_arr: x["data"]["ref_id"] = x["ref"].id() arr.append(x["data"]) return arr except Exception as ex: raise ex
def questions(): username = session["user"]["username"] question_type = request.args.get("type", "all").lower() if question_type == "answered": question_indexes = client.query( q.paginate(q.match(q.index("questions_index"), True, username), size=100_000)) elif question_type == "unanswered": question_indexes = client.query( q.paginate(q.match(q.index("questions_index"), False, username), size=100_000)) elif question_type == "all": question_indexes = client.query( q.paginate(q.union( q.match(q.index("questions_index"), True, username), q.match(q.index("questions_index"), False, username)), size=100_000)) else: return redirect(url_for("questions")) questions = [ q.get(q.ref(q.collection("questions"), i.id())) for i in question_indexes["data"] ] return render_template("questions.html", questions=client.query(questions)[::-1])
def create_reviews(): reviews = [] conferences = client.query( q.paginate(q.match(q.index("allConferences")), size=1000)) for conf in conferences['data']: reviewers = client.query( q.paginate(q.match(q.index("conference_reviewers_by_conference"), conf), size=1000)) for reviewer in reviewers['data']: submissions = client.query( q.paginate(q.match( q.index("conference_submissions_by_conference"), conf), size=1000)) for submission in submissions['data']: new_review = { "reviewer": reviewer, "submission": submission, "score": random.randint(0, 5), "public": fake.paragraph(), "private": fake.paragraph() } reviews.append(new_review) return reviews
def create_question(request): quiz_all=client.query(q.paginate(q.match(q.index("quiz_get_index"), "active"))) all_quiz=[] for i in quiz_all["data"]: all_quiz.append(q.get(q.ref(q.collection("Quiz"),i.id()))) context = {"quiz_all":client.query(all_quiz)} if request.method=="POST": quiz_name=request.POST.get("quiz_name") question_asked=request.POST.get("question") answer_1=request.POST.get("answer_1") answer_2=request.POST.get("answer_2") answer_3=request.POST.get("answer_3") answer_4=request.POST.get("answer_4") correct_answer=request.POST.get("correct_answer") try: question_create = client.query(q.get(q.match(q.index("question_index"), question_asked))) messages.add_message(request, messages.INFO, 'This question already exists') return redirect("App:create-question") except: question_create = client.query(q.create(q.collection("Question"), { "data": { "quiz_name": quiz_name, "question_asked": question_asked, "answer_1": answer_1, "answer_2": answer_2, "answer_3": answer_3, "answer_4": answer_4, "correct_answer": correct_answer, } })) messages.add_message(request, messages.INFO, 'Question Created Successfully.') return redirect("App:create-question") return render(request,"create-questions.html",context)
def test_difference(self): self.assertJson(query.difference(), '{"difference":[]}') self.assertJson(query.difference(query.index("widget")), '{"difference":{"index":"widget"}}') self.assertJson( query.difference(query.index("widget"), query.index("things")), '{"difference":[{"index":"widget"},{"index":"things"}]}')
def get(self, message_hash=None): if not message_hash: page = q.paginate(q.match(q.index("confirmed"), 'True')) ns = q.map_(lambda a: q.select(["data"], q.get(a)), page) result = client.query(ns)['data'] for message in result: messages[message['hash']] = message message_hash = random.choice(messages.keys()) message = fill_line(messages[message_hash]['title']) suggester = messages[message_hash]['suggester'] else: page = q.paginate(q.match(q.index("hash"), message_hash)) ns = q.map_(lambda a: q.select(["data"], q.get(a)), page) result = client.query(ns)['data'] if len(result) > 0: message = fill_line(result[0]['title']) suggester = result[0]['suggester'] else: raise tornado.web.HTTPError(404) self.output_message(message, message_hash, suggester) image = Image.new("RGBA", (300, 150), (255, 255, 255)) image_size = image.size # load the font and image font = ImageFont.truetype(fontFile, 18) # image = Image.open(imageFile) # firts you must prepare your text (you dont need this for english text) # text = unicode(message, "utf-8") text = message # start drawing on image draw = ImageDraw.Draw(image) lines = text_wrap(text, font, image_size[0] - 10) line_height = font.getsize('hg')[1] x = 10 y = 20 for line in lines: reshaped_text = arabic_reshaper.reshape(line) # correct its shape bidi_text = get_display(reshaped_text) # correct its direction # draw the line on the image x = image_size[0] - font.getsize(bidi_text)[0] - x draw.text((x, y), bidi_text, (0, 0, 0), font=font) # update the y position so that we can use it for next line y = y + line_height x = 10 # draw.text((10, 0), bidi_text, (0,0,0), font=font) draw = ImageDraw.Draw(image) # save it image.save("thumbnails/{}.png".format(message_hash))
def _build_intersecting_query( filter_group: sql.FilterGroup, acc_query: typing.Optional[QueryExpression], table: sql.Table, direction: str, ) -> QueryExpression: opposite_direction = "left" if direction == "right" else "right" document_set = build_document_set_intersection(table, filter_group) if acc_query is None: intersection = document_set else: intersection = q.intersection(acc_query, document_set) next_table = getattr(table, f"{direction}_join_table") if next_table is None or table.has_columns: return intersection next_join_key = getattr(table, f"{direction}_join_key") assert next_join_key is not None if next_join_key.name == "ref": next_foreign_key = getattr(next_table, f"{opposite_direction}_join_key") assert next_foreign_key is not None return _build_intersecting_query( filter_group, q.join( intersection, q.index( index_name( next_table.name, column_name=next_foreign_key.name, index_type=IndexType.REF, )), ), next_table, direction, ) return _build_intersecting_query( filter_group, q.join( intersection, q.index( index_name( table.name, index_type=IndexType.REF, foreign_key_name=next_join_key.name, )), ), next_table, direction, )
def create_authors(): authors = [] users = client.query(q.paginate(q.match(q.index("allUsers")), size=1000)) submissions = client.query( q.paginate(q.match(q.index("allSubmissions")), size=1000)) for paper in submissions['data']: for author in random.sample(users['data'], random.randint(0, 1) + 1): new_author = {"submissionID": paper, "userID": author} authors.append(new_author) return authors
def create_reviewers(): reviewers = [] users = client.query(q.paginate(q.match(q.index("allUsers")), size=1000)) conferences = client.query( q.paginate(q.match(q.index("allConferences")), size=1000)) for conf in conferences['data']: for user in random.sample(users['data'], 7): new_committee_member = {"conference": conf, "reviewer": user} reviewers.append(new_committee_member) return reviewers
def read_three_customers(client, cust_id1, cust_id2, cust_id3): # # Here is a more general use case where we retrieve multiple class references # by id and return the actual data underlying them. # res = client.query( q.map_( lambda x: q.select("data", q.get(x)), q.paginate( q.union(q.match(q.index("customer_by_id"), cust_id1), q.match(q.index("customer_by_id"), cust_id2), q.match(q.index("customer_by_id"), cust_id3))))) print('Union specific \'customer\' 1, 3, 8: {0}'.format(res))
def test_count_mean_sum(self): data = [1, 2, 3, 4, 5, 6, 7, 8, 9] self._q(query.create_collection({"name": "countmeansum_test"})) self._q( query.create_index({ "name": "countmeansum_idx", "source": query.collection("countmeansum_test"), "active": True, "values": [{ "field": ["data", "value"] }] })) self._q( query.foreach( query.lambda_( "x", query.create( query.collection("countmeansum_test"), {"data": { "value": query.add(query.var("x"), 2) }})), data)) m = query.match(query.index("countmeansum_idx")) expected = [9, 5.0, 45, 9, 7.0, 63] self.assertEqual( self._q([ query.count(data), query.mean(data), query.sum(data), query.count(m), query.mean(m), query.sum(m) ]), expected)
def build_document_set_intersection( table: sql.Table, filter_group: typing.Optional[sql.FilterGroup]) -> QueryExpression: """Build FQL match query based on intersection of filtered results from given group. Params: ------- table: A Table object associated with a Fauna collection. filter_group: A group of filters representing an intersection of filtered results. Returns: -------- FQL query expression that matches on the same conditions as the Table's filters. """ filter_group_filters = [] if filter_group is None else filter_group.filters group_filter_names = [ sql_filter.name for sql_filter in filter_group_filters ] filters = [ sql_filter for sql_filter in table.filters if sql_filter.name in group_filter_names ] if not any(filters): return q.intersection(q.match(q.index(index_name(table.name)))) document_sets = [ _define_match_set(query_filter) for query_filter in filters ] return q.intersection(*document_sets)
def create_appointment(request): if "user" in request.session: if request.method == "POST": name = request.POST.get("name") description = request.POST.get("description") time = request.POST.get("time") date = request.POST.get("date") try: user = client.query( q.get(q.match(q.index("events_index"), date, time))) messages.add_message( request, messages.INFO, 'An Event is already scheduled for the specified time.') return redirect("core:create-appointment") except: user = client.query( q.create( q.collection("Events"), { "data": { "name": name, "description": description, "time": time, "date": date, "user": request.session["user"]["username"], "status": 'False', } })) messages.add_message(request, messages.INFO, 'Appointment Scheduled Successfully.') return redirect("core:create-appointment") return render(request, "appoint/create-appointment.html") else: return HttpResponseNotFound("Page not found!")
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 with that username already exists.') return redirect("core: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("core:login") return render(request, "register.html")
def login(): if "user" in session: return redirect(url_for("dashboard")) if request.method == "POST": username = request.form.get("username").strip().lower() password = request.form.get("password") try: user = client.query( q.get(q.match(q.index("users_index"), username))) if encrypt_password(password) == user["data"]["password"]: session["user"] = { "id": user["ref"].id(), "username": user["data"]["username"] } return redirect(url_for("dashboard")) else: raise Exception() except: flash( "You have supplied invalid login credentials, please try again!", "danger") return redirect(url_for("login")) return render_template("login.html")
def login(data): try: return current_app.fauna_client.query( q.let( { 'response': q.login( q.match(q.index('unique_account_username_type'), [data.get('username'), 'EMAIL']), {'password': data.get('password')}), 'user': q.select_with_default( ['data', 'user'], q.get(q.select(['instance'], q.var('response'))), None) }, { 'data': { 'token': q.select('secret', q.var('response')), 'user': q.if_( q.is_ref(q.var('user')), q.select(['data', 'alias'], q.get(q.var('user'))), None) } })) except Exception as e: print(e)
def start_quiz(update, context): ''' Starts the quiz and gets the questions from aloc API, then saves to database ''' chat_id = update.effective_chat.id message = update.message.text try: name = update["message"]["chat"]["first_name"] except: name = update["message"]["chat"]["username"] subject = message.split('_')[1] questions = get_questions(subject=subject, mode='utme') quiz = client.query( q.create( q.collection("quiz"), { "data": { "user_id": chat_id, "subject": subject, "answered": 0, "score": 0, "id_": random.randint(0, 100), 'completed': False, 'questions': questions, } })) user = client.query(q.get(q.match(q.index("users"), chat_id))) client.query( q.update(q.ref(q.collection("users"), user["ref"].id()), {"data": { "last_command": "start_quiz" }})) msg = f"{subject.upper()}\n\n10 questions, enter 'q' to continue\n\n/end_quiz to end the quiz session." context.bot.send_message(chat_id=chat_id, text=msg)
def test_range(self): data = list(range(1, 20)) self._q(query.create_collection({"name": "range_test"})) self._q( query.create_index({ "name": "range_idx", "source": query.collection("range_test"), "active": True, "values": [{ "field": ["data", "value"] }] })) self._q( query.foreach( query.lambda_query(lambda x: query.create( query.collection("range_test"), {"data": { "value": x }})), data)) m = query.match(query.index("range_idx")) q1 = query.select("data", query.paginate(query.range(m, 3, 8))) q2 = query.select("data", query.paginate(query.range(m, 17, 18))) q3 = query.select("data", query.paginate(query.range(m, 19, 0))) self.assertEqual(self._q(q1), [3, 4, 5, 6, 7, 8]) self.assertEqual(self._q(q2), [17, 18]) self.assertEqual(self._q(q3), [])
def test_create_index(self): self._q(query.create_index({ "name": "index_for_test", "active": True, "source": query.collection("widgets")})) self.assertTrue(self._q(query.exists(query.index("index_for_test"))))
def add_book(request): if request.method=="POST": title= request.POST.get("title") genres=request.POST.get("genres") summary=request.POST.get("summary") pages=request.POST.get("pages") copies=request.POST.get("copies") author= request.POST.get("author") about=request.POST.get("about") try: book = client.query(q.get(q.match(q.index("book_index"), title))) messages.add_message(request, messages.INFO, 'Book Already Exists') return redirect("App:add_book") except: book = client.query(q.create(q.collection("Books"), { "data": { "title":title, "genres": genres, "summary": summary, "pages":pages, "copies":copies, "author":author, "about":about, "availability":"True" } })) messages.add_message(request, messages.INFO, 'Book added Successfully') return redirect("App:add_book") return render(request,"add-book.html")
def detail(request,slug): try: book= client.query(q.get(q.match(q.index("book_index"), slug)))["data"] context={"book":book} return render(request,"detail.html",context) except: return render(request,"detail.html")
def test_reverse(self): #arrays list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(self._q(query.reverse(list)), [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]) self.assertEqual(self._q(query.reverse(query.reverse(list))), list) self.assertEqual(self._q(query.reverse([])), []) self._q(query.create_collection({"name": "reverse_collection"})) self._q(query.create_index({ "name": "rev_coll_idx", "source": query.collection("reverse_collection"), "values": [{"field": ["data", "val"]}] })) index = query.index("rev_coll_idx") for i in range(100): self._q(query.create(query.collection( "reverse_collection"), {"data": {"val": i}})) assertPaginate = lambda q, expected, size = None : self.assertEqual(self._q(query.select("data", query.paginate(q, size))), expected) assertPaginate(query.reverse(query.match(index)), [99, 98, 97, 96, 95], 5) assertPaginate(query.reverse(query.reverse(query.match(index))), list, 11) q1 = query.select(["data", 0], query.reverse(query.paginate(query.match(index), size=50))) self.assertEqual(self._q(q1), 49) self._assert_bad_query(query.reverse("a string")) self._assert_bad_query(query.reverse(index)) self._assert_bad_query(query.reverse({"a": 1, "b": 2}))
def start(update, context): ''' Starts the bot and saves the user details if it is not existing ''' chat_id = update.effective_chat.id try: name = update["message"]["chat"]["first_name"] except: name = update["message"]["chat"]["username"] try: client.query(q.get(q.match(q.index("users"), chat_id))) except: client.query( q.create( q.collection("users"), { "data": { "id": chat_id, "name": name, "last_command": "", "date": datetime.now(pytz.UTC) }, })) botler = Botler().get() msg = messenger.get(name, botler) context.bot.send_message(chat_id=chat_id, text=msg)
def echo(update, context): 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"] if last_command == "add_todo": todo = client.query( q.create( q.collection("todo"), { "data": { "user_id": chat_id, "todo": message, "completed": False, "date": datetime.now(pytz.UTC) } })) client.query( q.update(q.ref(q.collection("users"), user["ref"].id()), {"data": { "last_command": "" }})) context.bot.send_message( chat_id=chat_id, text= "Successfully added todo task 👍\n\nSee all your todo with /list_todo" )
async def scrape_article(article: ReadingReference, response: Response): if NHK_PREFIX in article.url: try: # see if an article exists client.query(q.get(q.match(q.index('reading_by_url'), article.url))) return {"success": True, "status": "DocumentExists"} except: # scrape article and save article = requests.get(article.url) soup = BeautifulSoup(article.content, 'html.parser') article_body_text = soup.find( class_='article-main__body').get_text() article_title_text = soup.find( class_='article-main__title').get_text() client.query( q.create( q.collection('Reading'), { "data": { "reading_url": article.url, "article_title": article_title_text, "text": article_body_text } })) response.status_code = status.HTTP_201_CREATED return {"success": True, "status": "CreatedDocument"} else: response.status_code = status.HTTP_400_BAD_REQUEST return {"success": False, "status": "UnknownArticleType"}
def products(): if request.method == "GET": category = request.args.get("category") or None if category: data = client.query( q.map_( lambda x: q.get(x), q.paginate( q.match(q.index("products_by_category"), category)))) response = [i["data"] for i in data["data"]] for i in range(0, len(response)): response[i]['id'] = str(data["data"][i]["ref"].id()) return jsonify(data=response) data = client.query( q.map_(lambda x: q.get(x), q.paginate(q.documents(q.collection("products"))))) response = [i["data"] for i in data["data"]] for i in range(0, len(response)): response[i]['id'] = str(data["data"][i]["ref"].id()) return jsonify(data=response) elif request.method == "POST": request_data = request.get_json() response = client.query( q.create(q.collection("products"), {"data": request_data})) return jsonify(id=response["ref"].id())
def all_appointment(request): if "user" in request.session: try: appointments = client.query( q.paginate( q.match(q.index("events_index_paginate"), request.session["user"]["username"])))["data"] appointments_count = len(appointments) page_number = int(request.GET.get('page', 1)) appointment = client.query( q.get( q.ref(q.collection("Events"), appointments[page_number - 1].id()))) if request.GET.get("delete"): client.query( q.delete( q.ref(q.collection("Events"), appointments[page_number - 1].id()))) return redirect("core:all-appointment") context = { "count": appointments_count, "appointment": appointment, "page_num": page_number, "next_page": min(appointments_count, page_number + 1), "prev_page": max(1, page_number - 1) } return render(request, "all-appointments.html", context) except: return render(request, "all-appointments.html") else: return HttpResponseNotFound("Page not found!") return render(request, "all-appointments.html")
import json import logging import os from faunadb.client import FaunaClient from faunadb.objects import Ref from faunadb import query client = FaunaClient(secret=os.environ['FAUNADB_SECRET']) TODOS = Ref('classes/todos') ALL_TODOS = query.index('all_todos')