def migrate_bus100(crawl_db, city=""): # Line.objects.filter(crawl_source='bus100').delete() # Destination.objects.filter(crawl_source='bus100').delete() # Starting.objects.filter(crawl_source='bus100').delete() query = { "departure_time": { "$gte": str(datetime.now()) }, } if city: query.update({"start_city_name": city}) for d in crawl_db.line_bus100.find(query): crawl_source = d["crawl_source"] target_city_name = d["target_city_name"] end_station = d["end_station"] d_city_code = d["target_short_name"] if not d_city_code: d_city_code = "".join(map(lambda x:x[0], pypinyin.pinyin(unicode(target_city_name), style=pypinyin.FIRST_LETTER))) drv_date, drv_time = d["departure_time"].split(" ") attrs = { "line_id": d["line_id"], "crawl_source": crawl_source, "s_province": d["province_name"], "s_city_id": d["start_city_id"], "s_city_name": d["start_city_name"], "s_sta_id": d["start_city_id"], "s_sta_name": d["start_station"], "s_city_code": d["start_short_name"], "d_city_name": target_city_name, "d_city_code": d_city_code, "d_sta_name": end_station, "drv_date": drv_date, "drv_time": drv_time, "drv_datetime": datetime.strptime(d["departure_time"], "%Y-%m-%d %H:%M"), "distance": str(d["distance"]), "vehicle_type": '', "seat_type": '', "bus_num": str(d["banci"]), "full_price": float(str(d["price"]).split('¥')[-1]), "half_price": 0, "crawl_datetime": d["crawl_time"], "fee": 0, "left_tickets": 50 if d["flag"] else 0, "extra_info": {"flag": d["flag"]}, 'update_datetime': datetime.now(), "shift_id": str(d["shiftid"]), } try: line_obj = Line.objects.get(line_id=attrs["line_id"]) line_obj.update(**attrs) except Line.DoesNotExist: line_obj = Line(**attrs) line_obj.save() print line_obj.line_id, d["start_city_name"]
def setUp(self): ''' Set up method that will run before every Test ''' # self.group_ditch = Group( name="Ditch Lines" ) # self.user_boe = User(username = "******", password = "******", email = "*****@*****.**" ) self.new_line = Line(line_content="I am Groot")
def line_list(): params = request.values.to_dict() lineid = params.get("line_id", "") starting_name = params.get("starting", "") dest_name = params.get("destination", "") crawl_source = params.get("crawl_source", "") drv_date = params.get("drv_date", "") query = {} if lineid: query.update(line_id=lineid) if starting_name: query.update(s_city_name__startswith=starting_name) if dest_name: query.update(d_city_name__startswith=dest_name) if crawl_source: query.update(crawl_source=crawl_source) if drv_date: query.update(drv_date=drv_date) queryset = Line.objects(**query) return render_template('dashboard/lines.html', source_info=SOURCE_INFO, sites=queryset.distinct("crawl_source"), page=parse_page_data(queryset), starting=starting_name, destination=dest_name, line_id=lineid, crawl_source=crawl_source, drv_date=drv_date, condition=params, )
def init_db(db: Session) -> None: # Tables should be created with Alembic migrations # But if you don't want to use migrations, create # the tables un-commenting the next line # Base.metadata.create_all(bind=engine) user = crud.user.get_by_email(db, email=settings.FIRST_SUPERUSER) print("INITIAL USER", user) if not user: user_in = schemas.UserCreate( email=settings.FIRST_SUPERUSER, password=settings.FIRST_SUPERUSER_PASSWORD, is_superuser=True, ) user = crud.user.create(db, obj_in=user_in) # noqa: F841 other_user = crud.user.get_by_email( db, email="*****@*****.**" ) print("OTHER USER", other_user) if not other_user: user_in = schemas.UserCreate( email="*****@*****.**", password='******', is_superuser=False, ) other_user = crud.user.create(db, obj_in=user_in) # noqa: F841 lines = [] print("USERS", user.__dict__, other_user.__dict__) print("GENERATING DATA") for current_user in [user, other_user]: print("\t FOR USER ", current_user.__dict__) for line_num in range(10): line: Line = Line( title=f"Line_{uuid4()}", description=f"Line description {uuid4()}", owner_id=current_user.id ) print("\t\t GENERATED LINE", line.__dict__) for item_num in range(100): item = Item( title=f"Item_{uuid4()}", description=f"Item description {uuid4()}", owner_id=current_user.id ) line_item: LineItem = LineItem( owner_id=current_user.id, line=line, item=item ) line.line_items.append(line_item) print("\t\t\t GENERATED LINE ITEM", item.__dict__) lines.append(line) db.add_all(lines) db.commit()
def test_get_lines(self): ''' Test case to check if a line and its information is returned by the get_lines function that takes in an id and match it to id in the group table ''' # Line.query.delete() # User.query.delete() # Group.query.delete() self.new_line.save_line() gotten_lines = Line.get_lines(4990826417581240726341234) self.assertFalse(len(gotten_lines) == 1)
def setUp(self): ''' Set up method that will run before every Test ''' # Comment.query.delete() # Line.query.delete() # User.query.delete() # Group.query.delete() self.group_pick_up = Group( name="Pick-up lines" ) self.user_jane = User(username = "******", password = "******", email = "*****@*****.**" ) self.new_line = Line( line_content="I am Groot", group = self.group_pick_up, user = self.user_jane ) self.new_comment = Comment(comment_content="You need more practice")
class TestLine(unittest.TestCase): ''' Test class to test behaviours of the Line class Args: unittest.TestCase : Test case class that helps create test cases ''' def setUp(self): ''' Set up method that will run before every Test ''' # self.group_ditch = Group( name="Ditch Lines" ) # self.user_boe = User(username = "******", password = "******", email = "*****@*****.**" ) self.new_line = Line(line_content="I am Groot") # def tearDown(self): # ''' # Using query.delete to delete elements in the database after each test # ''' # Line.query.delete() # User.query.delete() # Group.query.delete() def test_instance(self): ''' Test case to check if new_line is an instance of Line ''' self.assertTrue(isinstance(self.new_line, Line)) def test_save_line(self): ''' Test case to check if a line is saved to the databse ''' # user_dan = User(username = "******", password = "******", email = "*****@*****.**" ) # test_line = Line( line_content="I am Groot", group = self.group_ditch, user = user_dan ) self.new_line.save_line() self.assertTrue(len(Line.query.all()) > 0) # Line.query.delete() # User.query.delete() # Group.query.delete() def test_get_lines(self): ''' Test case to check if a line and its information is returned by the get_lines function that takes in an id and match it to id in the group table ''' # Line.query.delete() # User.query.delete() # Group.query.delete() self.new_line.save_line() gotten_lines = Line.get_lines(4990826417581240726341234) self.assertFalse(len(gotten_lines) == 1)
from app.models import Speaker, Play, Line data = [] with open( os.path.join(os.getcwd(), 'app', 'static', 'resources', 'henry_iv.json'), 'r') as f: for line in f: data.append(json.loads(line)) play = data[0] db_play = Play.objects.filter(name=play[0]['play_name'], author='William Shakespeare').first() if db_play is None: db_play = Play(name=play[0]['play_name'], author='William Shakespeare') db_play.save() for line in play: db_speaker = Speaker.objects.filter(name=line['speaker']).first() if db_speaker is None: db_speaker = Speaker(name=line['speaker']) db_speaker.save() speech_number = None if line['speech_number'] != '': speech_number = line['speech_number'] db_line = Line(id=line['line_id'], number=line['line_number'], text=line['text_entry'], speech_no=speech_number, speaker=db_speaker, play=db_play) db_line.save()