Compare commits

...

3 Commits

3 changed files with 27 additions and 13 deletions

View File

@ -28,7 +28,6 @@ RUN set -eux &&\
Data::UUID \
HTML::Parse \
HTML::FormatText \
Test::utf8 \
URI::Simple \
HTTP::DAV \
Term::ReadKey \

View File

@ -71,7 +71,6 @@ The following perl modules are needed to run it
- Encode
- HTML::Parse
- HTML::FormatText
- Test::utf8
- URI::Simple
- Term::ReadKey
- HTTP::DAV

View File

@ -9,7 +9,6 @@ use Data::UUID;
use Encode qw(encode decode);
use HTML::Parse;
use HTML::FormatText;
use Test::utf8 qw(is_within_latin_1);
use URI::Simple;
use Term::ReadKey;
use HTTP::DAV;
@ -27,7 +26,9 @@ my $conf = {
user => undef,
password => undef,
database => undef,
filters => []
filters => [
["is_company", "=", "False"]
]
},
dav => {
url => undef,
@ -165,12 +166,10 @@ sub prompt_pass {
}
my %odoo_uuid = ();
foreach my $contact_id (@{$odoo->search('res.partner', [])}){
my $contact_count = 0;
print "Querying Odoo API for contacts\n";
foreach my $contact_id (@{$odoo->search('res.partner', $conf->{odoo}->{filters})}){
my $contact = $odoo->read('res.partner', [ $contact_id ])->[0];
if (defined $contact->{is_company}){
print "Skiping $contact->{name} because it's a company\n";
next;
}
my $vcard =<<_EOV;
BEGIN:VCARD
@ -186,10 +185,11 @@ _EOV
$contact->{$_} = HTML::FormatText->new->format(HTML::Parse::parse_html($contact->{$_}));
$contact->{$_} =~ s/\r?\n/\\n/g;
}
if (is_within_latin_1($contact->{$_})){
$vcard .= "$odoo2vcf->{$_}:" . encode("UTF-8", decode("Latin1", $contact->{$_})) . "\n";
} else {
# Check if the field contains non ASCII characters. If it does, it's UTF-8, else, it's Latin1
if ($contact->{$_} =~ /([^\x{00}-\x{ff}])/){
$vcard .= "$odoo2vcf->{$_}:" . encode("UTF-8", $contact->{$_}) . "\n";
} else {
$vcard .= "$odoo2vcf->{$_}:" . encode("UTF-8", decode("Latin1", $contact->{$_})) . "\n";
}
}
@ -203,8 +203,11 @@ _EOV
open VCARD, ">$conf->{path}->{workdir}/$uuid.vcf";
print VCARD $vcard;
close VCARD;
$contact_count++;
}
my $dav_success = 0;
my $dav_error = 0;
if (defined $conf->{dav}->{url}){
my $d = HTTP::DAV->new();
$d->credentials(
@ -237,10 +240,23 @@ if (defined $conf->{dav}->{url}){
}
print "Uploading vcards to webdav server $conf->{dav}->{url}\n";
$d->put( -local => "$conf->{path}->{workdir}/*.vcf" );
foreach my $vcf (glob("$conf->{path}->{workdir}/*.vcf")){
if ($d->put( -local => "$vcf" ) == 1){
$dav_success++;
} else {
print "An error occured while uploading $vcf : " . $d->message . "\n";
$dav_error++;
}
}
}
if (not $conf->{path}->{keep_vcards}){
print "Deleting vcards from $conf->{path}->{workdir}\n";
rmtree($conf->{path}->{workdir});
}
print "$contact_count contacts found in Odoo\n";
if (defined $conf->{dav}->{url}){
print "$dav_success contacts successfuly uploaded to $conf->{dav}->{url}\n";
print "$dav_error contacts failed while uploading to $conf->{dav}->{url}\n";
}