Skip to content

Instantly share code, notes, and snippets.

@mjamroz
Created August 17, 2021 06:02
Show Gist options
  • Save mjamroz/d18617f7632de4e9663371903b812e14 to your computer and use it in GitHub Desktop.
Save mjamroz/d18617f7632de4e9663371903b812e14 to your computer and use it in GitHub Desktop.
tags = [
{"p": 1, "fav":False},
{"p": 2, "fav":False},
{"p": 5, "fav":False},
{"p": 5.5, "fav":True},
{"p": 5.6, "fav":True},
{"p": 6, "fav":False},
]
# after changing position in frontend, we applying this function to update tags positions in db:
def change_db_pos_after_moving_tag(tags, curr_pos, tag):
if not tag["fav"]:
tag["pos"] = curr_pos
else:
# if fav tag, then check neighbours:
tag_idx = tags.index(tag)
# take neighbours
lo_bo = tag_idx -1 if tag_idx >0 else 0
hi_bo = tag_idx +1 if tag_idx < len(tags) -2 else len(tags) - 1
# put private fav tag in the middle between neighbours but in float numbers space
tag["pos"] = (tags[lo_bo]["pos"] + tags[hi_bo]["pos"])/2.0
tags[tag_idx] = tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment