More admin change

This commit is contained in:
Tanishq Dubey 2024-10-20 16:22:45 -04:00
parent fee6f755a3
commit f435aedf2b
4 changed files with 203 additions and 18 deletions

71
app.py
View File

@ -205,5 +205,74 @@ def admin_logout():
def serve_thumbnail(filename): def serve_thumbnail(filename):
return send_from_directory(THUMBNAIL_FOLDER, filename) return send_from_directory(THUMBNAIL_FOLDER, filename)
@app.route('/admin/update_photo/<int:photo_id>', methods=['POST'])
def update_photo(photo_id):
if 'logged_in' not in session:
return jsonify({'success': False, 'error': 'Not logged in'}), 401
data = request.json
db_session = DBSession()
photo = db_session.query(Photo).get(photo_id)
if not photo:
db_session.close()
return jsonify({'success': False, 'error': 'Photo not found'}), 404
try:
for field, value in data.items():
if field == 'date_taken':
value = datetime.strptime(value, '%Y-%m-%d %H:%M:%S')
elif field == 'iso':
value = int(value)
setattr(photo, field, value)
db_session.commit()
db_session.close()
return jsonify({'success': True})
except Exception as e:
db_session.rollback()
db_session.close()
return jsonify({'success': False, 'error': str(e)}), 500
@app.route('/admin/delete_photo/<int:photo_id>', methods=['POST'])
def delete_photo(photo_id):
if 'logged_in' not in session:
return jsonify({'success': False, 'error': 'Not logged in'}), 401
db_session = DBSession()
photo = db_session.query(Photo).get(photo_id)
if not photo:
db_session.close()
return jsonify({'success': False, 'error': 'Photo not found'}), 404
try:
# Delete the original file
original_path = os.path.join(UPLOAD_FOLDER, photo.input_filename)
if os.path.exists(original_path):
os.remove(original_path)
# Delete the thumbnail directory
thumb_dir = os.path.join(THUMBNAIL_FOLDER, os.path.splitext(photo.input_filename)[0])
if os.path.exists(thumb_dir):
for thumb_file in os.listdir(thumb_dir):
os.remove(os.path.join(thumb_dir, thumb_file))
os.rmdir(thumb_dir)
# Delete the database entry
db_session.delete(photo)
db_session.commit()
db_session.close()
return jsonify({'success': True})
except Exception as e:
db_session.rollback()
db_session.close()
return jsonify({'success': False, 'error': str(e)}), 500
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, port=5002, host='0.0.0.0') app.run(
debug=True,
port=config['server']['port'],
host=config['server']['host']
)

View File

@ -16,7 +16,11 @@ def load_or_create_config():
'thumbnail': 'thumbnails' 'thumbnail': 'thumbnails'
}, },
'appearance': { 'appearance': {
'accent_color': '{{ accent_color }}' 'accent_color': '#ff6600'
},
'server': {
'host': '0.0.0.0',
'port': 5002
} }
} }
with open(CONFIG_FILE, 'w') as f: with open(CONFIG_FILE, 'w') as f:

View File

@ -90,6 +90,31 @@
.logout a:hover { .logout a:hover {
text-decoration: underline; text-decoration: underline;
} }
.editable {
cursor: pointer;
}
.editable:hover {
background-color: #f0f0f0;
}
.editing {
background-color: #fff;
border: 1px solid #ccc;
padding: 2px;
}
.delete-btn {
background-color: #ff4136;
color: white;
border: none;
padding: 0.25rem 0.5rem;
border-radius: 4px;
cursor: pointer;
font-family: inherit;
font-weight: bold;
margin-left: 0.5rem;
}
.delete-btn:hover {
background-color: #ff1a1a;
}
</style> </style>
</head> </head>
<body> <body>
@ -126,23 +151,105 @@
<th>Shutter Speed</th> <th>Shutter Speed</th>
<th>ISO</th> <th>ISO</th>
<th>Dimensions</th> <th>Dimensions</th>
<th>Actions</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for photo in photos %} {% for photo in photos %}
<tr> <tr data-id="{{ photo.id }}">
<td><img src="{{ url_for('serve_thumbnail', filename=photo.thumbnail_filename) }}" alt="Thumbnail" class="thumbnail"></td> <td><img src="{{ url_for('serve_thumbnail', filename=photo.thumbnail_filename) }}" alt="Thumbnail" class="thumbnail"></td>
<td>{{ photo.input_filename }}</td> <td class="editable" data-field="input_filename">{{ photo.input_filename }}</td>
<td>{{ photo.date_taken.strftime('%Y-%m-%d %H:%M:%S') }}</td> <td class="editable" data-field="date_taken">{{ photo.date_taken.strftime('%Y-%m-%d %H:%M:%S') }}</td>
<td>{{ photo.focal_length }}</td> <td class="editable" data-field="focal_length">{{ photo.focal_length }}</td>
<td>{{ photo.aperture }}</td> <td class="editable" data-field="aperture">{{ photo.aperture }}</td>
<td>{{ photo.shutter_speed }}</td> <td class="editable" data-field="shutter_speed">{{ photo.shutter_speed }}</td>
<td>{{ photo.iso }}</td> <td class="editable" data-field="iso">{{ photo.iso }}</td>
<td>{{ photo.width }}x{{ photo.height }}</td> <td>{{ photo.width }}x{{ photo.height }}</td>
<td>
<button onclick="saveChanges(this)">Save</button>
<button onclick="deletePhoto(this)" class="delete-btn">Delete</button>
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
</div> </div>
<script>
function makeEditable(element) {
const value = element.textContent;
const input = document.createElement('input');
input.value = value;
input.classList.add('editing');
element.textContent = '';
element.appendChild(input);
input.focus();
input.addEventListener('blur', function() {
element.textContent = this.value;
element.classList.remove('editing');
});
}
document.querySelectorAll('.editable').forEach(el => {
el.addEventListener('click', function() {
if (!this.classList.contains('editing')) {
makeEditable(this);
}
});
});
function saveChanges(button) {
const row = button.closest('tr');
const photoId = row.dataset.id;
const updatedData = {};
row.querySelectorAll('.editable').forEach(el => {
updatedData[el.dataset.field] = el.textContent;
});
fetch('/admin/update_photo/' + photoId, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(updatedData),
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('Changes saved successfully!');
} else {
alert('Error saving changes: ' + data.error);
}
})
.catch((error) => {
console.error('Error:', error);
alert('An error occurred while saving changes.');
});
}
function deletePhoto(button) {
if (confirm('Are you sure you want to delete this photo?')) {
const row = button.closest('tr');
const photoId = row.dataset.id;
fetch('/admin/delete_photo/' + photoId, {
method: 'POST',
})
.then(response => response.json())
.then(data => {
if (data.success) {
row.remove();
alert('Photo deleted successfully!');
} else {
alert('Error deleting photo: ' + data.error);
}
})
.catch((error) => {
console.error('Error:', error);
alert('An error occurred while deleting the photo.');
});
}
}
</script>
</body> </body>
</html> </html>

View File

@ -126,15 +126,8 @@
@media (max-width: 768px) { @media (max-width: 768px) {
body {
flex-direction: column;
}
.sidebar {
width: 100%;
height: auto;
position: static;
padding: 10px;
}
.main-content { .main-content {
padding: 10px; padding: 10px;
} }
@ -149,6 +142,18 @@
height: auto; height: auto;
} }
} }
@media (max-width: 1024px) {
body {
flex-direction: column;
}
.sidebar {
width: 100%;
height: auto;
position: static;
padding: 10px;
}
}
</style> </style>
</head> </head>
<body> <body>