-
Notifications
You must be signed in to change notification settings - Fork 0
story/HOP-63 #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
story/HOP-63 #49
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91f7915
[HOP-60] Added Models and registered in admin for 3 obj types
Girik1105 1eaf9b5
[HOP-63] Added csv templates for admin, added csv import funcs with e…
Girik1105 9b8e010
[HOP-63] Merged develop
Girik1105 0eccab5
[HOP-63] Added metadata to csv
Girik1105 020e614
[HOP-63] Added metadata for zip
Girik1105 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import csv | ||
| import datetime | ||
| import io | ||
| import re | ||
|
|
||
| # A partial ISO date: a 4-digit year, optionally a month, optionally a day. | ||
| # Day can only appear when month does, so "year-day" is impossible to express. | ||
| _PARTIAL_DATE_RE = re.compile(r"^(\d{4})(?:-(\d{1,2})(?:-(\d{1,2}))?)?$") | ||
|
|
||
|
|
||
| def parse_partial_date(value): | ||
| """Parse a partial ISO date string into a ``(date, precision)`` pair. | ||
|
|
||
| Accepts ``YYYY``, ``YYYY-MM`` or ``YYYY-MM-DD``. A missing month/day | ||
| defaults to 1 so the value still fits a ``DateField``; ``precision`` | ||
| ("year", "month" or "day") records how much was actually supplied so the | ||
| padding can be ignored later. Blank or unparseable input returns | ||
| ``(None, "")``. | ||
| """ | ||
| match = _PARTIAL_DATE_RE.match((value or "").strip()) | ||
| if not match: | ||
| return None, "" | ||
| year, month, day = match.groups() | ||
| try: | ||
| if day is not None: | ||
| return datetime.date(int(year), int(month), int(day)), "day" | ||
| if month is not None: | ||
| return datetime.date(int(year), int(month), 1), "month" | ||
| return datetime.date(int(year), 1, 1), "year" | ||
| except ValueError: | ||
| return None, "" | ||
|
|
||
|
|
||
| def import_names_csv(model, file_obj): | ||
| """Import a one-column CSV into a model with a ``name`` field. | ||
|
|
||
| Returns ``(created, skipped)``. Blank rows, a leading header row of ``name``, | ||
| and rows whose name already exists in the table are all counted as skipped. | ||
| """ | ||
| text = file_obj.read().decode("utf-8-sig", errors="replace") | ||
| reader = csv.reader(io.StringIO(text)) | ||
|
|
||
| created = 0 | ||
| skipped = 0 | ||
| for row in reader: | ||
| name = row[0].strip() if row else "" | ||
| if not name or name.lower() == "name": | ||
| skipped += 1 | ||
| continue | ||
| _, was_created = model.objects.get_or_create(name=name) | ||
| if was_created: | ||
| created += 1 | ||
| else: | ||
| skipped += 1 | ||
| return created, skipped | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need to do this by hand. The ISO date standard covers incomplete dates like year month or just year I believe, so my guess is Python's standard date library will be able to handle it. And you could probably just store the iso date as a string in the db (maybe with an extra date column to be able to sort by year).