Skip to content

Commit

Permalink
expect media field replaces bulky setup
Browse files Browse the repository at this point in the history
  • Loading branch information
frimpongopoku committed Oct 13, 2023
1 parent f8ad1ee commit 723fc5b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 49 deletions.
17 changes: 4 additions & 13 deletions src/api/handlers/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#from types import FunctionType as function
from _main_.utils.context import Context
from api.decorators import admins_only, super_admins_only, login_required
from api.store.common import expect_media_fields


class ActionHandler(RouteHandler):
Expand Down Expand Up @@ -90,20 +91,8 @@ def submit(self, request):
.expect("image", "file", is_required=False, options={"is_logo": True})
.expect("vendors", list, is_required=False)
.expect("action_id", str, is_required=False)
.expect(
"underAge", bool,
).expect(
"copyright", bool,
).expect(
"copyright_att", str,
).expect(
"guardian_info", str,
).expect(
"size", str
).expect(
"size_text", str
)
)
self = expect_media_fields(self)

args, err = self.validator.verify(args)
if err:
Expand Down Expand Up @@ -164,6 +153,8 @@ def update(self, request):
.expect("vendors", list, is_required=False)
)

self = expect_media_fields(self)

args, err = self.validator.verify(args)
if err:
return err
Expand Down
9 changes: 2 additions & 7 deletions src/api/handlers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from types import FunctionType as function
from _main_.utils.context import Context
from api.decorators import admins_only, super_admins_only, login_required
from api.store.common import expect_media_fields


class EventHandler(RouteHandler):
Expand Down Expand Up @@ -221,13 +222,7 @@ def submit(self, request):
self.validator.expect('rsvp_enabled', bool)
self.validator.expect('rsvp_email', bool)
self.validator.expect('event_id', str)
self.validator.expect("size", str)
self.validator.expect("size_text", str)
self.validator.expect("description")
self.validator.expect("underAge", bool)
self.validator.expect("copyright", bool)
self.validator.expect("copyright_att", str)
self.validator.expect("guardian_info", str)
self = expect_media_fields(self)
args, err = self.validator.verify(args)

if err:
Expand Down
9 changes: 2 additions & 7 deletions src/api/handlers/testimonial.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from _main_.utils.context import Context
from _main_.utils.validator import Validator
from api.decorators import admins_only, super_admins_only, login_required
from api.store.common import expect_media_fields


class TestimonialHandler(RouteHandler):
Expand Down Expand Up @@ -94,13 +95,7 @@ def submit(self, request):
self.validator.rename('preferredName', 'preferred_name')
self.validator.expect('testimonial_id', str)
self.validator.expect("image", "file", is_required=False)
self.validator.expect("size", str)
self.validator.expect("size_text", str)
self.validator.expect("description")
self.validator.expect("underAge", bool)
self.validator.expect("copyright", bool)
self.validator.expect("copyright_att", str)
self.validator.expect("guardian_info", str)
self = expect_media_fields(self)
args, err = self.validator.verify(args)

if err:
Expand Down
17 changes: 10 additions & 7 deletions src/api/handlers/vendor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from _main_.utils.context import Context
from _main_.utils.validator import Validator
from api.decorators import admins_only, super_admins_only, login_required
from api.store.common import expect_media_fields



Expand Down Expand Up @@ -108,13 +109,15 @@ def submit(self, request):
.expect("vendor_id", str)

)
self.validator.expect("size", str)
self.validator.expect("size_text", str)
self.validator.expect("description")
self.validator.expect("underAge", bool)
self.validator.expect("copyright", bool)
self.validator.expect("copyright_att", str)
self.validator.expect("guardian_info", str)
# self.validator.expect("size", str)
# self.validator.expect("size_text", str)
# self.validator.expect("description")
# self.validator.expect("underAge", bool)
# self.validator.expect("copyright", bool)
# self.validator.expect("copyright_att", str)
# self.validator.expect("guardian_info", str)

self = expect_media_fields(self)

args, err = self.validator.verify(args)
if err:
Expand Down
43 changes: 28 additions & 15 deletions src/api/store/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ def sign_mou(mou_rich_text, user=None, date=None):
)



def expect_media_fields(self):
self.validator.expect("size", str).expect("size_text", str).expect(
"description"
Expand All @@ -161,24 +160,38 @@ def expect_media_fields(self):
"permission_notes", str
)
return self



def make_media_info(args):
fields = ["copyright","copyright_att", "underAge","size", "size_text", "guardian_info"]
name_to_obj_name = {"underAge":"has_children", "copyright": "has_copyright_permission"}
obj = {}
for name in fields:
def make_media_info(args):
"""Request arg names are different from how they are stored on the media object, so this function corrects the naming and makes sure it matches the structure that is actually saved on the media object"""
fields = [
"copyright",
"copyright_att",
"underAge",
"size",
"size_text",
"guardian_info",
"permission_key",
"permission_notes"
]
name_to_obj_name = {
"underAge": "has_children",
"copyright": "has_copyright_permission",
}
obj = {}
for name in fields:
value = args.pop(name, None)
name = name_to_obj_name.get(name,name)
if value:
obj[name] = value
return obj
name = name_to_obj_name.get(name, name)
if value:
obj[name] = value
return obj


def get_media_info(media):
if not media:
def get_media_info(media):
"""Retrieves media info from media object"""
if not media:
return {}, False
if hasattr(media,"user_upload"):
if hasattr(media.user_upload, "info"):
if hasattr(media, "user_upload"):
if hasattr(media.user_upload, "info"):
return media.user_upload.info or {}, True
return {}, False

0 comments on commit 723fc5b

Please sign in to comment.