forked from productscience/constellate
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Let users download profiles as vCards #241
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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
Empty file.
This file contains 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,68 @@ | ||
import pytest | ||
from vobject.vcard import Name | ||
|
||
from .. import vcard_util | ||
|
||
|
||
class TestVcardUtil: | ||
def test_full_name_to_name(self): | ||
EvanHahn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
all_in_given_name = [ | ||
"", | ||
"Biscuit", | ||
"Dr.", | ||
"Dr. Gravy Biscuit", | ||
"Sweet Gravy Biscuit", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good names. |
||
"Ms. Dr. Cheese Gravy Biscuit Jr. M.D.", | ||
"Biscuit Jr.", | ||
"宮本 茂", | ||
"أبو بکر محمد بن زکریاء الرازي", | ||
] | ||
for full_name in all_in_given_name: | ||
actual = vcard_util.full_name_to_name(full_name) | ||
expected = Name(given=full_name) | ||
assert actual == expected | ||
|
||
split = [ | ||
("Gravy", "Biscuit"), | ||
("Großer", "Keks"), | ||
] | ||
for given, family in split: | ||
full_name = f"{given} {family}" | ||
actual = vcard_util.full_name_to_name(full_name) | ||
expected = Name(given=given, family=family) | ||
assert actual == expected | ||
|
||
def test_content_disposition_filename(self): | ||
EvanHahn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use_fallback = [ | ||
"", | ||
" ", | ||
"a" * 256, | ||
"A\nB", | ||
"ÀBC", | ||
"A%B", | ||
"A\\B", | ||
"A\tB", | ||
"A$B", | ||
"AB123", | ||
] | ||
for full_name in use_fallback: | ||
actual = vcard_util.content_disposition_filename(full_name) | ||
expected = "contact.vcf" | ||
assert actual == expected | ||
|
||
ok_to_serialize = [ | ||
"Biscuit", | ||
"Gravy Biscuit", | ||
"Big Gravy Biscuit", | ||
"Gravy-Biscuit", | ||
] | ||
for full_name in ok_to_serialize: | ||
actual = vcard_util.content_disposition_filename(full_name) | ||
expected = f"{full_name}.vcf" | ||
assert actual == expected | ||
|
||
filtered_actual = vcard_util.content_disposition_filename( | ||
"Dr. Gravy Biscuit, Jr." | ||
) | ||
filtered_expected = "Dr Gravy Biscuit Jr" | ||
assert actual == expected |
This file contains 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,107 @@ | ||
import itertools | ||
import unicodedata | ||
import vobject | ||
|
||
|
||
def _is_all_latin_characters(string: str) -> bool: | ||
""" | ||
Returns True if every character in the string meets the following criteria: | ||
|
||
- It's in the [Basic Latin][1] or [Latin-1 Supplement][2] Unicode block | ||
(U+0000 to U+00FF). | ||
- It's a hyphen or [categorized][3] as a Unicode Letter. | ||
|
||
Returns False otherwise. | ||
|
||
[1]: https://www.unicode.org/charts/PDF/U0000.pdf | ||
[2]: https://www.unicode.org/charts/PDF/U0080.pdf | ||
[3]: https://www.unicode.org/reports/tr44/#General_Category_Values | ||
""" | ||
for character in string: | ||
if character == "-": | ||
continue | ||
|
||
# Characters < 0x41 are in the Basic Latin block, | ||
# but 0x41 is the first letter. | ||
is_in_latin_block = 0x41 <= ord(character) <= 0xFF | ||
if not is_in_latin_block: | ||
return False | ||
|
||
is_in_valid_category = unicodedata.category(character).startswith("L") | ||
if not is_in_valid_category: | ||
return False | ||
|
||
return True | ||
|
||
|
||
def full_name_to_name(full_name: str) -> vobject.vcard.Name: | ||
""" | ||
Convert free-form full name strings (like "Alice McBiscuit") to structured | ||
vCard names. | ||
|
||
vCard has two relevant name fields: | ||
|
||
1. [FN], which is effectively a string | ||
2. [N], which breaks names into pieces: given name, surname, etc. | ||
|
||
We should always provide FN. Unfortunately, we also need to provide N | ||
because (1) some vCard parsers require it (2) our vCard library produces | ||
vCard version 3 files where [N is required][0]. | ||
|
||
Because we don't store these structured fields, we use a simple heuristic | ||
to build structure from free-form name strings. | ||
|
||
If this function receives a two-word name made up of Latin characters, it | ||
will break it into given + family name. Otherwise, it will "give up" and | ||
put everything into the given name. See the tests for examples, as well as | ||
some edge cases not documented here. | ||
|
||
This heuristic could be made more sophisticated but should work well enough | ||
given that many contact apps prefer FN and use N as a fallback. | ||
|
||
[FN]: https://datatracker.ietf.org/doc/html/rfc6350#section-6.2.1 | ||
[N]: https://datatracker.ietf.org/doc/html/rfc6350#section-6.2.2 | ||
[0]: https://www.rfc-editor.org/rfc/rfc2426#section-3.1.2 | ||
""" | ||
|
||
give_up_result = vobject.vcard.Name(given=full_name) | ||
|
||
if len(full_name) >= 1024: | ||
return give_up_result | ||
|
||
words = full_name.split() | ||
|
||
if len(words) != 2: | ||
return give_up_result | ||
|
||
if not all(_is_all_latin_characters(word) for word in words): | ||
return give_up_result | ||
|
||
return vobject.vcard.Name(given=words[0], family=words[1]) | ||
|
||
|
||
def content_disposition_filename(full_name: str) -> str: | ||
""" | ||
Generate the .vcf filename given a full name, suitable for use in the | ||
`Content-Disposition` header. Should be quoted. | ||
""" | ||
|
||
fallback = "contact.vcf" | ||
|
||
if len(full_name) >= 256: | ||
return fallback | ||
|
||
if not full_name.isascii(): | ||
return fallback | ||
|
||
full_name = full_name.replace(".", "").replace(",", "").strip() | ||
|
||
if len(full_name) == 0: | ||
return fallback | ||
|
||
for character in full_name: | ||
is_valid = character == "-" or character == " " or character.isalpha() | ||
if not is_valid: | ||
return fallback | ||
|
||
return f"{full_name}.vcf" |
This file contains 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
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.
We could add additional properties in the future, but I think this is a good starter set. The vCard Wikipedia page has a good list of these properties, as does the spec.