Skip to content

Commit

Permalink
[IMP] sign_oca: Allow to store geolocalization
Browse files Browse the repository at this point in the history
  • Loading branch information
etobella committed Aug 25, 2024
1 parent 425ebf8 commit b490bad
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 14 deletions.
8 changes: 6 additions & 2 deletions sign_oca/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,15 @@ def get_sign_oca_info_access(self, signer_id, access_token):
auth="public",
website=True,
)
def get_sign_oca_sign_access(self, signer_id, access_token, items):
def get_sign_oca_sign_access(
self, signer_id, access_token, items, latitude=False, longitude=False
):
try:
signer_sudo = self._document_check_access(
"sign.oca.request.signer", signer_id, access_token
)
except (AccessError, MissingError):
return request.redirect("/my")
return signer_sudo.action_sign(items, access_token=access_token)
return signer_sudo.action_sign(

Check warning on line 104 in sign_oca/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

sign_oca/controllers/main.py#L104

Added line #L104 was not covered by tests
items, access_token=access_token, latitude=latitude, longitude=longitude
)
8 changes: 7 additions & 1 deletion sign_oca/models/sign_oca_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class SignOcaRequest(models.Model):
states={"draft": [("readonly", False)]},
)
next_item_id = fields.Integer(compute="_compute_next_item_id")
ask_location = fields.Boolean()

@api.depends("signer_ids")
@api.depends_context("uid")
Expand Down Expand Up @@ -368,6 +369,8 @@ class SignOcaRequestSigner(models.Model):
"ir.sequence", copy=False, default=lambda r: r._get_sequence()
)
altered_hash = fields.Boolean(compute="_compute_altered_hash")
latitude = fields.Float()
longitude = fields.Float()

@api.depends("request_id.record_ref")
def _compute_model(self):
Expand Down Expand Up @@ -412,6 +415,7 @@ def get_info(self, access_token=False):
"name": self.request_id.template_id.name,
"items": self.request_id.signatory_data,
"to_sign": self.request_id.to_sign,
"ask_location": self.request_id.ask_location,
"partner": {
"id": self.partner_id.id,
"name": self.partner_id.name,
Expand All @@ -430,7 +434,7 @@ def sign(self):
"url": self.access_url,
}

def action_sign(self, items, access_token=False):
def action_sign(self, items, access_token=False, latitude=False, longitude=False):
self.ensure_one()
if self.signed_on:
raise ValidationError(
Expand Down Expand Up @@ -475,6 +479,8 @@ def action_sign(self, items, access_token=False):
}
)
self.signature_hash = final_hash
self.latitude = latitude
self.longitude = longitude
self.request_id._check_signed()
self._set_action_log("sign", access_token=access_token)
if self.sequence_id:
Expand Down
1 change: 1 addition & 0 deletions sign_oca/models/sign_oca_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class SignOcaTemplate(models.Model):

name = fields.Char(required=True)
data = fields.Binary(attachment=True, required=True)
ask_location = fields.Boolean()
filename = fields.Char()
item_ids = fields.One2many("sign.oca.template.item", inverse_name="template_id")
request_count = fields.Integer(compute="_compute_request_count")
Expand Down
51 changes: 42 additions & 9 deletions sign_oca/static/src/components/sign_oca_pdf/sign_oca_pdf.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,51 @@ export default class SignOcaPdf extends SignOcaPdfCommon {
})
);
$buttons.on("click.o_sign_oca_button_sign", () => {
this.env.services
.rpc({
model: this.props.model,
method: "action_sign",
args: [[this.props.res_id], this.info.items],
})
.then(() => {
this.props.trigger("history_back");
});
this.signOca();
});
return $buttons;
}
async getLocation() {
if (!this.info.ask_location || !navigator.geolocation) {
return {};
}
try {
return await new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject);
});

// Do something with the latitude and longitude
} catch (error) {
switch (error.code) {
case error.PERMISSION_DENIED:
console.error("User denied the request for geolocation.");
break;
case error.POSITION_UNAVAILABLE:
console.error("Location information is unavailable.");
break;
case error.TIMEOUT:
console.error("The request to get user location timed out.");
break;
default:
console.error("An unknown error occurred.");
break;
}
}
return {};
}
async signOca() {
const position = await this.getLocation();
await this.env.services.rpc({
model: this.props.model,
method: "action_sign",
args: [[this.props.res_id], this.info.items],
kwargs: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
},
});
this.props.trigger("history_back");
}
_trigger_up(ev) {
const evType = ev.name;
const payload = ev.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,20 @@ export class SignOcaPdfPortal extends SignOcaPdf {
super.postIframeFields(...arguments);
this.checkFilledAll();
}
_onClickSign() {
async _onClickSign() {
const position = await this.getLocation();
this.env.services
.rpc({
route:
"/sign_oca/sign/" +
this.props.signer_id +
"/" +
this.props.access_token,
params: {items: this.info.items},
params: {
items: this.info.items,
latitude: position.coords.latitude,
longitude: position.coords.longitude,
},
})
.then((action) => {
// As we are on frontend env, it is not possible to use do_action(), so we
Expand Down
2 changes: 2 additions & 0 deletions sign_oca/views/sign_oca_request.xml
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@
<field name="request_id" readonly="1" />
<field name="signed_on" />
<field name="inalterable_hash" />
<field name="latitude" />
<field name="longitude" />
</group>
</sheet>
</form>
Expand Down
1 change: 1 addition & 0 deletions sign_oca/views/sign_oca_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<field name="data" filename="filename" />
<field name="filename" invisible="1" />
<field name="model_id" groups="sign_oca.sign_oca_group_admin" />
<field name="ask_location" />
</group>
<notebook>
<page name="items" string="Fields">
Expand Down
1 change: 1 addition & 0 deletions sign_oca/wizards/sign_oca_template_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def _generate_vals(self):
"template_id": self.template_id.id,
"signatory_data": self.template_id._get_signatory_data(),
"data": self.template_id.data,
"ask_location": self.template_id.ask_location,
"signer_ids": [
(
0,
Expand Down

0 comments on commit b490bad

Please sign in to comment.