Skip to content

Commit

Permalink
[FIX] auto_backup: bad reference to field sftp_private_key
Browse files Browse the repository at this point in the history
- Sort imports
- Add ensure_one() on single record methods
  • Loading branch information
jabibi committed May 9, 2016
1 parent d058d5d commit 6ebf1cd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion auto_backup/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
" Grupo ESOC Ingeniería de Servicios,"
" Odoo Community Association (OCA)"
),
'license': "AGPL-3",
"license": "AGPL-3",
"website": "http://www.vanroey.be/applications/bedrijfsbeheer/odoo",
"category": "Tools",
"depends": ['email_template'],
Expand Down
50 changes: 26 additions & 24 deletions auto_backup/models/db_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@
# © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
# License AGPL-3.0 or later (http://www.gnu.org/licenses/gpl.html).

from contextlib import contextmanager
from datetime import datetime, timedelta
from glob import iglob
import logging
import os
import shutil
import tempfile
import traceback
from contextlib import contextmanager
from datetime import datetime, timedelta
from glob import iglob
from openerp import exceptions, models, fields, api, _, tools

from openerp import api, exceptions, fields, models, tools, _
from openerp.service import db
import logging

_logger = logging.getLogger(__name__)
try:
import pysftp
except ImportError:
_logger.warning('Cannot import pysftp')

_logger.warning("Cannot import pysftp")

class DbBackup(models.Model):
_name = 'db.backup'

_name = "db.backup"
_inherit = "mail.thread"

_sql_constraints = [
Expand All @@ -40,8 +42,8 @@ class DbBackup(models.Model):
folder = fields.Char(
default=lambda self: self._default_folder(),
oldname="bkp_dir",
help='Absolute path for storing the backups',
required=True
help="Absolute path for storing the backups",
required=True,
)
days_to_keep = fields.Integer(
oldname="daystokeep",
Expand All @@ -56,26 +58,22 @@ class DbBackup(models.Model):
help="Choose the storage method for this backup.",
)
sftp_host = fields.Char(
string='SFTP Server',
string="SFTP Server",
oldname="sftpip",
help=(
"The host name or IP address from your remote"
" server. For example 192.168.0.1"
)
help= "The host name or IP address from your remote "
"server. For example 192.168.0.1",
)
sftp_port = fields.Integer(
string="SFTP Port",
default=22,
oldname="sftpport",
help="The port on the FTP server that accepts SSH/SFTP calls."
help="The port on the FTP server that accepts SSH/SFTP calls.",
)
sftp_user = fields.Char(
string='Username in the SFTP Server',
string="Username in the SFTP Server",
oldname="sftpusername",
help=(
"The username where the SFTP connection "
"should be made with. This is the user on the external server."
)
help="The username where the SFTP connection "
"should be made with. This is the user on the external server.",
)
sftp_password = fields.Char(
string="SFTP Password",
Expand Down Expand Up @@ -105,7 +103,7 @@ def _compute_name(self):
if rec.method == "local":
rec.name = "%s @ localhost" % rec.folder
elif rec.method == "sftp":
rec.name = "sftp://%s@%s:%d%s" % (
rec.name = "sftp://%s@%s:%s%s" % (
rec.sftp_user, rec.sftp_host, rec.sftp_port, rec.folder)

@api.constrains("folder", "method")
Expand All @@ -123,6 +121,7 @@ def _check_folder(self):
@api.multi
def action_sftp_test_connection(self):
"""Check if the SFTP settings are correct."""
self.ensure_one()
try:
# Just open and close the connection
with self.sftp_connection():
Expand All @@ -148,7 +147,7 @@ def action_backup(self):
pass

with open(os.path.join(rec.folder, filename),
'wb') as destiny:
"wb") as destiny:
# Copy the cached backup
if backup:
with open(backup) as cached:
Expand Down Expand Up @@ -197,6 +196,7 @@ def action_backup_all(self):
@contextmanager
def backup_log(self):
"""Log a backup result."""
self.ensure_one()
try:
_logger.info("Starting database backup: %s", self.name)
yield
Expand Down Expand Up @@ -237,6 +237,7 @@ def cleanup(self):
@contextmanager
def cleanup_log(self):
"""Log a possible cleanup failure."""
self.ensure_one()
try:
_logger.info("Starting cleanup process after database backup: %s",
self.name)
Expand Down Expand Up @@ -265,6 +266,7 @@ def filename(self, when):
@api.multi
def sftp_connection(self):
"""Return a new SFTP connection with found parameters."""
self.ensure_one()
params = {
"host": self.sftp_host,
"username": self.sftp_user,
Expand All @@ -274,7 +276,7 @@ def sftp_connection(self):
"Trying to connect to sftp://%(username)s@%(host)s:%(port)d",
extra=params)
if self.sftp_private_key:
params["private_key"] = self.stfpprivatekey
params["private_key"] = self.sftp_private_key
if self.sftp_password:
params["private_key_pass"] = self.sftp_password
else:
Expand Down

0 comments on commit 6ebf1cd

Please sign in to comment.