Answer the question
In order to leave comments, you need to log in
Are files duplicated on the web page when added (provided that they are deleted using the delete sublist button)?
When deleting files with the "x" button, that is, one at a time, there are no problems with adding. But when deleting with the delete_sublist button, when adding, they are duplicated, and the more you delete with the delete sublist button, the more are duplicated.
def playlist_index(request):
# start_time = time.time()
template = loader.get_template('playlist_index.html')
last_add = {}
last_add['dayslot'] = ''
last_add['box_name'] = ''
last_add['monitor_num'] = ''
last_add['media'] = ''
last_add['play_duration'] = '0'
if request.method == 'POST':
if 'up' in request.POST.keys():
rec = request.POST['up']
print(f'rec = {rec}')
box_id, day_slot_id, monitor_num, order_in_slot = rec.split(';')
order_in_slot = int(order_in_slot)
if order_in_slot > 1:
query = 'SELECT id FROM config_playlist WHERE box_id="{}" AND day_slot_id="{}" '+\
'AND monitor_num="{}" AND order_in_slot>="{}" AND order_in_slot<="{}" ORDER BY order_in_slot'
query = query.format(box_id, day_slot_id, monitor_num, order_in_slot-1, order_in_slot)
neighbour_rows = Playlist.objects.raw(query)
ids = []
for u in neighbour_rows:
ids.append(u.id)
# print('ids = {}'.format(ids))
upper = Playlist.objects.get(id=ids[0])
upper.order_in_slot += 1
upper.save()
this = Playlist.objects.get(id=ids[1])
this.order_in_slot -= 1
this.save()
elif 'down' in request.POST.keys():
rec = request.POST['down']
print(f'rec = {rec}')
box_id, day_slot_id, monitor_num, order_in_slot = rec.split(';')
order_in_slot = int(order_in_slot)
query = 'SELECT id FROM config_playlist WHERE box_id="{}" AND day_slot_id="{}" '+\
'AND monitor_num="{}" AND order_in_slot>="{}" AND order_in_slot<="{}" ORDER BY order_in_slot'
query = query.format(box_id, day_slot_id, monitor_num, order_in_slot, order_in_slot+1)
neighbour_rows = Playlist.objects.raw(query)
ids = []
for u in neighbour_rows:
ids.append(u.id)
if len(ids) > 1: # not last in slot list
this = Playlist.objects.get(id=ids[0])
this.order_in_slot += 1
this.save()
lower = Playlist.objects.get(id=ids[1])
lower.order_in_slot -= 1
lower.save()
elif 'delete' in request.POST.keys():
print('delete')
rec = request.POST['delete']
print(f'rec = {rec}')
box_id, day_slot_id, monitor_num, order_in_slot = rec.split(';')
order_in_slot = int(order_in_slot)
q = f'SELECT id FROM config_playlist WHERE box_id="{box_id}" AND day_slot_id="{day_slot_id}" '+\
f'AND monitor_num="{monitor_num}" AND order_in_slot>="{order_in_slot}" ORDER BY order_in_slot'
neighbour_rows = Playlist.objects.raw(q)
ids = []
for u in neighbour_rows:
ids.append(u.id)
for i in range(len(ids)):
if i == 0: # one that need to delete
this = Playlist.objects.get(id=ids[0])
this.delete()
else:
lower = Playlist.objects.get(id=ids[i])
lower.order_in_slot -= 1
lower.save()
cursor = connection.cursor()
q = f'SELECT COUNT(*) FROM config_playlist WHERE box_id="{box_id}" AND day_slot_id="{day_slot_id}" '+\
f'AND monitor_num="{monitor_num}"'
cursor.execute(q)
playlist_count = cursor.fetchone()[0]
print(f'playlist_count = {playlist_count}')
if playlist_count == 0: # no media in playlist
q = f'SELECT id, is_shuffle FROM config_pl_shuffle WHERE box_id="{box_id}" AND day_slot_id="{day_slot_id}" AND monitor_num="{monitor_num}"'
shuffle_rows = Pl_shuffle.objects.raw(q)
for s in shuffle_rows:
shuffle = Pl_shuffle.objects.get(id=s.id)
shuffle.delete()
elif 'delete_sublist' in request.POST.keys():
print('delete_sublist')
rec = request.POST['delete_sublist']
print(f'rec = {rec}')
box_id, day_slot_id, monitor_num = rec.split(';')
q = f'DELETE FROM config_playlist WHERE box_id="{box_id}" AND day_slot_id="{day_slot_id}" '+\
f'AND monitor_num="{monitor_num}"'
cursor = connection.cursor()
cursor.execute(q)
# ids = []
# for u in neighbour_rows:
# ids.append(u.id)
#
# for i in range(len(ids)):
# if i == 0: # one that need to delete
# this = Playlist.objects.get(id=ids[0])
# this.delete()
# else:
# lower = Playlist.objects.get(id=ids[i])
# lower.order_in_slot -= 1
# lower.save()
#
# cursor = connection.cursor()
# q = f'SELECT COUNT(*) FROM config_playlist WHERE box_id="{box_id}" AND day_slot_id="{day_slot_id}" '+\
# f'AND monitor_num="{monitor_num}"'
# cursor.execute(q)
# playlist_count = cursor.fetchone()[0]
# print(f'playlist_count = {playlist_count}')
# if playlist_count == 0: # no media in playlist
# q = f'SELECT id, is_shuffle FROM config_pl_shuffle WHERE box_id="{box_id}" AND day_slot_id="{day_slot_id}" AND monitor_num="{monitor_num}"'
# shuffle_rows = Pl_shuffle.objects.raw(q)
# for s in shuffle_rows:
# shuffle = Pl_shuffle.objects.get(id=s.id)
# shuffle.delete()
elif 'shuffle_switch' in request.POST.keys():
print('shuffle_switch')
rec = request.POST['shuffle_switch']
print(f'rec = {rec}')
box_id, day_slot_id, monitor_num, is_shuffle = rec.split(';')
q = f'SELECT id, is_shuffle FROM config_pl_shuffle WHERE box_id="{box_id}" AND day_slot_id="{day_slot_id}" AND monitor_num="{monitor_num}"'
shuffle_rows = Pl_shuffle.objects.raw(q)
for s in shuffle_rows:
shuffle = Pl_shuffle.objects.get(id=s.id)
shuffle.is_shuffle = not shuffle.is_shuffle
shuffle.save()
else: #adding new item
dayslot_item = DaySlots()
box_item = Boxes()
print(request.POST)
q = 'SELECT id, order_in_slot FROM config_playlist '
q += f"WHERE box_id='{request.POST['box']}' AND day_slot_id='{request.POST['dayslot']}'"
q += f" AND monitor_num='{request.POST['monitor_num']}' ORDER BY order_in_slot DESC LIMIT 1"
last_in_slot_set = Playlist.objects.raw(q)
last_in_slot = 0
print(last_in_slot_set)
for l in last_in_slot_set:
# print('l.order_in_slot = {}, type = {}'.format(l.order_in_slot, type(l.order_in_slot)))
last_in_slot = l.order_in_slot
dayslot_item.id = request.POST['dayslot']
box_item.id = request.POST['box']
last_in_slot += 1
print(f'last_in_slot = {last_in_slot}')
if last_in_slot == 1: # no media in playlist
shuffle_item = Pl_shuffle()
shuffle_item.day_slot = dayslot_item
shuffle_item.box = box_item
shuffle_item.monitor_num = request.POST['monitor_num']
shuffle_item.is_shuffle = False
shuffle_item.save()
media_list = request.POST.getlist('media')
for i in range(len(media_list)):
playlist_item = Playlist()
playlist_item.day_slot = dayslot_item
playlist_item.box = box_item
playlist_item.monitor_num = request.POST['monitor_num']
playlist_item.order_in_slot = last_in_slot + i
playlist_item.media = media_list[i]
playlist_item.play_duration = request.POST['play_duration']
playlist_item.save()
last_add['dayslot'] = int(request.POST['dayslot'])
last_add['box'] = int(request.POST['box'])
last_add['monitor_num'] = request.POST['monitor_num']
last_add['media'] = media_list
last_add['play_duration'] = request.POST['play_duration']
subprocess.run(['cp', '/home/pi/tvnet/db.sqlite3', '/home/pi/tvnet/media/'])
media = os.listdir('media/')
media = sorted(set(media) - {'db.sqlite3', 'listdir'})
dayslots = DaySlots.objects.order_by('start_time')
boxes = Boxes.objects.order_by('box_name')
query = 'SELECT * FROM config_playlist AS pl ' + \
'JOIN config_dayslots AS ds ON pl.day_slot_id=ds.id ' + \
'JOIN config_boxes AS bx ON pl.box_id=bx.id ' + \
'JOIN config_pl_shuffle as sh ON pl.day_slot_id=sh.day_slot_id AND pl.box_id=sh.box_id AND pl.monitor_num=sh.monitor_num ' + \
'ORDER BY start_time, end_time, box_name, monitor_num, order_in_slot'
playlistslots = Playlist.objects.raw(query)
master_ip = get_master_ip()
context = {
'master_ip': master_ip,
'last_add': last_add,
'media': media,
'dayslots': dayslots,
'boxes' : boxes,
'playlistslots': playlistslots,
}
# print(f"time = {time.time() - start_time}")
return HttpResponse(template.render(context, request))
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question