def handle(self, *args, **kwargs): file_name = kwargs["file_name"] with open(f"{file_name}.txt") as file: for row in file: # Checking The file Contexts.. title = row author_name = generate_author_name() category_name = generate_category_name() publish_date = generate_publish_date() views = generate_view_count() reviewed = generate_is_reviewed() # print(title, author_name, category_name, publish_date, views, reviewed) # OverWriting to The DataBase author = Author.objects.get_or_create(name=author_name) journal = Journal( title=title, author=Author.objects.get(name=author_name), publish_date=publish_date, views=views, reviewed=reviewed, ) journal.save() category = Category.objects.get_or_create(name=category_name) journal.categories.add( Category.objects.get(name=category_name)) # stdout : Printing the Message self.stdout.write(self.style.SUCCESS("Date Imported Successfully"))
def handle(self, *args, **kwargs): file_name = kwargs['file_name'] with open(f'{file_name}.txt') as file: for row in file: title = row author_name = generate_author_name() category_name = generate_category_name() publish_date = generate_publish_date() views = generate_view_count() reviewed = generate_is_reviewed() author = Author.objects.get_or_create(name=author_name) journal = Journal( title=title, author=Author.objects.get(name=author_name), publish_date=publish_date, views=views, reviewed=reviewed ) journal.save() category = Category.objects.get_or_create(name=category_name) journal.categories.add(Category.objects.get(name=category_name)) self.stdout.write(self.style.SUCCESS('Data imported successfully'))
def handle(self, *args, **options): for x in range(0, 50): title = " ".join(faker.words(random.randint(2, 4))) views = random.randint(1, 100) reviewed = random.randint(0, 1) == 1 author_name = random.choice(author_names) published = faker.date_between(start_date='-7y', end_date='now') category_name = random.choice(categories) #create the author instance & save to db author = Author.objects.get_or_create(name=author_name) #create the journal instance journal = Journal(title=title, author=Author.objects.get(name=author_name), published=published, views=views, reviewed=reviewed) #create the journal instance and save journal.save() category = Category.objects.get_or_create(name=category_name) journal.categories.add(Category.objects.get(name=category_name)) self.stdout.write(self.style.SUCCESS('Data imported sucessfully'))
def new(request): try: entry = Journal(title=request.POST['title'], content=request.POST['content']) entry.save() except ValidationError as e: return render(request, 'core/new_entry.html', { 'error_message': 'Please fill out both title and content', 'entry': entry }) else: return HttpResponseRedirect(reverse('detail', args=(entry.id,)))
def new(request): try: entry = Journal(title=request.POST['title'], content=request.POST['content']) entry.save() except ValidationError as e: return render( request, 'core/new_entry.html', { 'error_message': 'Please fill out both title and content', 'entry': entry }) else: return HttpResponseRedirect(reverse('detail', args=(entry.id, )))
def journal( msg: str = Argument(None, help="The journal message to record"), delete: int = Option(None, help="Delete a message by id"), show: bool = Option(True, help="display records for day/week/month/date_key"), period: str = Option( "day", help="The type of time period key to display messages. [day,week," "month] can be combined with 'show' or 'key'.", ), key: str = KEY, id: int = Option(None, help="Add a journal to a specific clok record"), week: bool = WEEK, month: bool = MONTH, all_jobs: bool = ALL_JOBS, ): """Show and manage journal entries""" if delete is not None: id = delete if msg is not None: if id is not None: c = Clok.get_by_id(id) if c is not None: c.add_journal(msg) else: raise ValueError(f"Sorry I couldn't find that id :({id})") else: Clok.get_last_record().add_journal(msg) if week: period = "week" elif month: period = "month" if show: records = get_records_for_period(period, key, all_jobs=all_jobs) print(f"Printing Journal entries for the {key or period.lower()}.") for i in records: for journal in i.journal_entries: print(journal) if delete is not None: print(Journal.get_by_id(id)) typer.confirm( f"Are you sure that you want to delete this record? ({id})?") Journal.delete_by_id(id)
def dump(file_path: str = Argument(None)): """Export the database to a json file""" if file_path is None: date_str = datetime.now().strftime("%Y%m%d_%H%M%S") file_path = f"{APPLICATION_DIRECTORY}/time-clock-dump-{date_str}.json" print(f"Dumping the database to > {file_path}") dump_dict = { "time_clok_jobs": Job.dump(), "time_clok_state": State.dump(), "time_clok": Clok.dump(), "time_clok_journal": Journal.dump(), } s = json.dumps(dump_dict, default=to_json) with open(file_path, "w") as f: f.write(s)
def import_(file_path: str = Argument( None, help="the path of the file to import. Only json files are supported at this " "time.", )): """Import an exported json file to the database.""" if os.path.isfile(file_path): with open(file_path) as f: dump_obj = json.loads(f.read()) time_clok_jobs = dump_obj["time_clok_jobs"] time_clok = dump_obj["time_clok"] time_clok_state = dump_obj["time_clok_state"] time_clok_journal = dump_obj["time_clok_journal"] jobs = [] for job in time_clok_jobs: jobs.append(Job(**job)) add_items_to_database(jobs) cloks = [] for clok in time_clok: cloks.append(Clok(**clok)) add_items_to_database(cloks) journals = [] for journal in time_clok_journal: journals.append(Journal(**journal)) add_items_to_database(journals) try: s = State(**time_clok_state[0]) s.save() except IntegrityError: pass else: raise FileNotFoundError(f"'{file_path}' does not exist.")
def repair(): for j in Journal.query().all(): print(j.clok_id, j.time, j.entry) if j.entry is None or j.entry == "show": Journal.delete_by_id(j.id)