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 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 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 test_collection_predicates(self): self.assertTrue(self._q(query.is_empty([]))) self.assertFalse(self._q(query.is_empty([1, 2, 3]))) self.assertFalse(self._q(query.is_nonempty([]))) self.assertTrue(self._q(query.is_nonempty([1, 2, 3]))) self._create(n=111) self.assertFalse( self._q( query.is_empty( query.paginate(query.match(self.n_index_ref, 111))))) self.assertTrue( self._q( query.is_empty( query.paginate(query.match(self.n_index_ref, 112))))) self.assertTrue( self._q( query.is_nonempty( query.paginate(query.match(self.n_index_ref, 111))))) self.assertFalse( self._q( query.is_nonempty( query.paginate(query.match(self.n_index_ref, 112)))))
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 test_nested_keys(self): client = self.create_new_database(self.admin_client, "db-for-keys") client.query(query.create_database({"name": "db-test"})) server_key = client.query( query.create_key({ "database": query.database("db-test"), "role": "server" })) admin_key = client.query( query.create_key({ "database": query.database("db-test"), "role": "admin" })) self.assertEqual( client.query(query.paginate(query.keys()))["data"], [server_key["ref"], admin_key["ref"]]) self.assertEqual( self.admin_client.query( query.paginate(query.keys( query.database("db-for-keys"))))["data"], [server_key["ref"], admin_key["ref"]])
def reviews(): if request.method == "GET": product = request.args.get('product') if product: data = client.query( q.map_( lambda x: q.get(x), q.paginate(q.match(q.index("reviews_by_product"), product)))) 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("reviews"))))) 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("reviews"), {"data": request_data})) return jsonify(id=response["ref"].id())
def test_paginate(self): n_value = 200 refs = [ self._create(n=n_value)["ref"], self._create(n=n_value)["ref"], self._create(n=n_value)["ref"] ] test_set = query.match(self.n_index_ref, n_value) self.assertEqual(self._q(query.paginate(test_set)), {"data": refs}) data = [] page1 = self._q(query.paginate(test_set, size=1)) data.extend(page1["data"]) page2 = self._q(query.paginate(test_set, size=1, after=page1["after"])) data.extend(page2["data"]) self.assertEqual(data, [refs[0], refs[1]]) self.assertEqual( self._q(query.paginate(test_set, sources=True)), { "data": [{ "sources": [SetRef(test_set)], "value": refs[0] }, { "sources": [SetRef(test_set)], "value": refs[1] }, { "sources": [SetRef(test_set)], "value": refs[2] }] })
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 _sort_document_set(document_set: QueryExpression, order_by: typing.Optional[sql.OrderBy]): if order_by is None: return q.paginate(document_set, size=common.MAX_PAGE_SIZE) if len(order_by.columns) > 1: raise exceptions.NotSupportedError( "Ordering by multiple columns is not yet supported.") ordered_column = order_by.columns[0] assert ordered_column.table_name is not None ordered_document_set = q.join( document_set, q.index( common.index_name( ordered_column.table_name, column_name=ordered_column.name, index_type=common.IndexType.SORT, )), ) if order_by.direction == sql.OrderDirection.DESC: ordered_document_set = q.reverse(ordered_document_set) return q.map_( q.lambda_(["_", "ref"], q.var("ref")), q.paginate(ordered_document_set, size=common.MAX_PAGE_SIZE), )
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 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 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 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 test_paginate(self): self.assertJson(query.paginate(query.collection("widget")), '{"paginate":{"collection":"widget"}}') self.assertJson(query.paginate(query.collection("widget"), size=1, ts=123, after=query.ref(query.collection("widget"), "1"), before=query.ref(query.collection("widget"), "10"), events=True, sources=True), ('{"after":{"id":"1","ref":{"collection":"widget"}},' '"before":{"id":"10","ref":{"collection":"widget"}},' '"events":true,"paginate":{"collection":"widget"},' '"size":1,"sources":true,"ts":123}'))
def test_documents(self): aCollection = "col_test_documents" anIndex = "idx_test_documents" self._q(query.create_collection({"name": aCollection})) self._q( query.create_index({ "name": anIndex, "source": query.collection(aCollection), "active": True })) count = 56 data = [{} for x in range(count)] self._q( query.foreach( query.lambda_( "x", query.create(query.collection(aCollection), {"data": query.var("x")})), data)) self.assertEqual( self._q( query.select([0], query.count( query.paginate( query.documents( query.collection(aCollection)))))), count) self.assertEqual( self._q(query.count(query.documents( query.collection(aCollection)))), count)
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 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")
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 dashboard(): username = session["user"]["username"] queries = [ q.count( q.paginate(q.match(q.index("questions_index"), True, username), size=100_000)), q.count( q.paginate(q.match(q.index("questions_index"), False, username), size=100_000)) ] answered_questions, unanswered_questions = client.query(queries) return render_template( "dashboard.html", answered_questions=answered_questions["data"][0], unanswered_questions=unanswered_questions["data"][0])
def read_all_customers(client): # # Read all the records that we created. This is a more generalized usage of the # paginate functionality. Notice the capture and passing of the "after" cursor # that is part of the return of the paginate if records are remaining. # # Use a small'ish page size so that we can demonstrate a paging example. # # NOTE: after is inclusive of the value. # pageSize = 8 cursorPos = None while True: res = client.query( q.map_( lambda x: q.select("data", q.get(q.select(1, x))), q.paginate(q.match(q.index("customer_id_filter")), after=cursorPos, size=pageSize))) for i in res['data']: print(i) if 'after' in res: cursorPos = res['after'] else: break
def user_contacts(user): contacts = s_client.query( q.map_( q.paginate( q.match(q.index("contacts_by_username"), user['data']['username'])), lambda x: q.get(x))) print(contacts)
def get_documents_in_index(self, index="tweets", size=100000): """Assume index name exists Validation not needed personal script unique_halts, unique_news, unique_short_news """ documents = self.client.query( q.paginate(q.match(q.index(index)), size=size)) return documents
def fauna_paginate_database(self) -> Tuple[bool, Any, str]: """Fauna paginate database.""" client = self.get_fauna_connection() # database: str = kwargs["database"] try: return client.query(q.paginate(Ref("databases"))) except (BadRequest) as _error: # pragma: no cover raise ValueError("Fauna error.") from _error
def fauna_read_database(self) -> Dict[List[str], Any]: """Read from fauna database.""" try: client = self.get_fauna_connection() indexes = client.query(q.paginate(q.indexes())) return indexes except (Exception) as _error: # pragma: no cover raise ValueError("Fauna error - read database.") from _error
def get_categories(after=None, before=None, size=5): return fauna.query( q.map_( lambda ref: q.get(ref), q.paginate(q.documents(q.collection('categories')), size=size, after=after, before=before)))
def get_orders(secret, after=None, before=None, size=5): client = FaunaClient(secret=secret) return client.query( q.map_( lambda ref: q.get(ref), q.paginate(q.documents(q.collection('orders')), size=size, after=after, before=before) ) )
async def read_get_snippet() -> dict: result = client.query( q.map_(lambda x: q.get(x), q.paginate(q.match(q.index("all_snippets"))))) result = result['data'] obj = result[random.randint(0, len(result) - 1)] return obj['data']
def _delete_data(): resources = [q.functions(), q.indexes(), q.collections()] delete = lambda res: q.foreach( q.lambda_("res", q.delete(q.var("res"))), q.paginate(res) ) delete_queries = [delete(res) for res in resources] _execute_with_retries(q.do(*delete_queries))
def get_all_students(self): result = self.clientf.query( query.map_( query.lambda_("x", query.get(query.var('x'))), query.paginate(query.match(query.index('all_students')), size=1000))) students = result['data'] students = [student['data'] for student in students] return students
def list(event, context): # fetch all todos from the database results = client.query( query.map_expr(lambda ref: query.get(ref), query.paginate(query.match(ALL_TODOS)))) # create a response response = { "statusCode": 200, "body": json.dumps(map(make_result, results['data'])) } return response