Skip to content

Commit

Permalink
Merge pull request #93 from BiznetGIO/fix/insert-rdata
Browse files Browse the repository at this point in the history
Fix: RDATA insertion
  • Loading branch information
anak10thn authored Mar 19, 2020
2 parents 69e2c9a + dc55f67 commit c4dd080
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
3 changes: 3 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ COPY ./requirements.txt /restknotapi/requirements.txt
RUN pip3 install -r /restknotapi/requirements.txt
COPY . /restknotapi

RUN apk del gcc linux-headers musl-dev && \
rm -rf /var/cache/apk/*

EXPOSE 5000
CMD ["gunicorn", "autoapp:app", "-b", "0.0.0.0:5000"]
50 changes: 26 additions & 24 deletions api/app/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,23 @@ def get_one(table, field=None, value=None):

def insert(table, data=None):
cursor, connection = get_db()
value = ""
column = ""
str_placeholer = ""
rows = []
rows_value = []

# arrange column and values
# arrange row and values
for row in data:
column += row + ","
value += f"{data[row]},"
str_placeholer += "%s,"
# omit ',' at the end
column = column[:-1]
value = value[:-1]
str_placeholer = str_placeholer[:-1]
rows.append(row)
rows_value.append(str(data[row]))

str_placeholer = ["%s"] * len(rows)

try:
query = (
f'INSERT INTO "{table}" ({column}) VALUES ({str_placeholer}) RETURNING *'
)
value_as_tuple = tuple(value.split(","))
rows = ",".join(rows)
str_placeholer = ",".join(str_placeholer)

query = f'INSERT INTO "{table}" ({rows}) VALUES ({str_placeholer}) RETURNING *'
cursor.prepare(query)
cursor.execute((value_as_tuple))
cursor.execute((tuple(rows_value)))
except (Exception, psycopg2.DatabaseError) as error:
connection.rollback()
raise ValueError(f"{error}")
Expand All @@ -104,15 +100,21 @@ def insert(table, data=None):

def update(table, data=None):
cursor, connection = get_db()
value = ""
rows = data["data"]
for row in rows:
value += row + "='%s'," % str(rows[row])
_set = value[:-1]
field = list(data["where"].keys())[0]
data_ = data["data"]
rows = []
set_value = []

for row in data_:
rows.append(row)
row_value = str(data_[row])
set_value.append(f"{row}='{row_value}'")

field = list(data["where"].keys())[0] # must be one
field_data = data["where"][field]

try:
field_data = data["where"][field]
query = f'UPDATE "{table}" SET {_set} WHERE {field}=%(field_data)s'
set_ = ",".join(set_value)
query = f'UPDATE "{table}" SET {set_} WHERE {field}=%(field_data)s'
cursor.prepare(query)
cursor.execute({"field_data": field_data})
except (Exception, psycopg2.DatabaseError) as error:
Expand Down
7 changes: 5 additions & 2 deletions api/app/models/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ def get_other_data(record):
ttl = model.get_one(table="ttl", field="id", value=record["ttl_id"])
type_ = model.get_one(table="type", field="id", value=record["type_id"])

rdata = helpers.exclude_keys(rdata, {"id", "record_id"})
if rdata:
rdata = helpers.exclude_keys(rdata, {"id", "record_id"})
rdata = rdata.get("rdata")

zone = helpers.exclude_keys(
zone, {"id", "is_committed", "user_id", "record_id"}
)

data = {
"id": record["id"],
"owner": record["owner"],
"rdata": rdata["rdata"],
"rdata": rdata,
"zone": zone["zone"],
"type": type_["type"],
"ttl": ttl["ttl"],
Expand Down

0 comments on commit c4dd080

Please sign in to comment.