Skip to content

Commit

Permalink
Fix #372
Browse files Browse the repository at this point in the history
Fixed the export / import issue with flag attachments
  • Loading branch information
eljeffeg committed May 6, 2020
1 parent 8a9798b commit 722e694
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
11 changes: 11 additions & 0 deletions models/FlagAttachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""

import os
import xml.etree.cElementTree as ET

from uuid import uuid4
from sqlalchemy import Column, ForeignKey
Expand Down Expand Up @@ -69,3 +70,13 @@ def delete_data(self):
fpath = options.flag_attachment_dir + "/" + self.uuid
if os.path.exists(fpath) and os.path.isfile(fpath):
os.unlink(fpath)

def to_xml(self, parent):
attachment_elem = ET.SubElement(parent, "flag_attachment")
ET.SubElement(attachment_elem, "file_name").text = self.file_name
with open(options.flag_attachment_dir + "/" + self.uuid, mode="rb") as fp:
data = fp.read()
ET.SubElement(attachment_elem, "data").text = encode(data, "base64")

def to_dict(self):
return {"file_name": self.file_name, "data": self.data}
2 changes: 1 addition & 1 deletion setup/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# -*- coding: utf-8 -*-
__version__ = "3.5.1"
__version__ = "3.5.2"
23 changes: 22 additions & 1 deletion setup/xmlsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ def create_flags(parent, box):
flag.case_sensitive = get_child_text(flag_elem, "case_sensitive", 0)
flag.description = get_child_text(flag_elem, "description")
flag.capture_message = get_child_text(flag_elem, "capture_message")
flag.type = get_child_text(flag_elem, "type", "static")
flag.type = flag_elem.get("type", "static")
flag.order = get_child_text(flag_elem, "order", None)
if flag.type == "file":
add_attachments(
get_child_by_tag(flag_elem, "flag_attachments"), flag
)
dbsession.add(flag)
dbsession.flush()
depend = get_child_text(flag_elem, "depends_on", None)
Expand All @@ -158,6 +162,23 @@ def create_flags(parent, box):
continue


def add_attachments(parent, flag):
""" Add uploaded files as attachments to flags """
logging.info("Found %s attachment(s)" % parent.get("count"))
for index, attachement_elem in enumerate(parent.getchildren()):
try:
flag_attachment = FlagAttachment(
file_name=get_child_text(attachement_elem, "flag_name")
)
flag_attachment.data = bytearray(
b64decode(get_child_text(attachement_elem, "data"))
)
flag.flag_attachments.append(flag_attachment)
dbsession.add(flag_attachment)
except:
logging.exception("Failed to import attachement #%d in flag" % (index + 1))


def create_choices(parent, flag):
""" Create multiple choice flag objects """
logging.info("Found %s choice(s)" % parent.get("count"))
Expand Down

0 comments on commit 722e694

Please sign in to comment.