changes of the upddating of library Items
This commit is contained in:
+57
@@ -6627,6 +6627,63 @@ def edit_item(id):
|
|||||||
return redirect(url_for('home_admin'))
|
return redirect(url_for('home_admin'))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/update_group', methods=['POST'])
|
||||||
|
def update_group():
|
||||||
|
data = request.get_json()
|
||||||
|
series_group_id = data.get('series_group_id')
|
||||||
|
|
||||||
|
if not series_group_id:
|
||||||
|
return jsonify({'success': False, 'message': 'Keine Gruppen-ID'}), 400
|
||||||
|
|
||||||
|
# 1. Shared Fields (Group Logic)
|
||||||
|
# These apply to every item in the group
|
||||||
|
shared_update = {
|
||||||
|
'Name': data.get('name'),
|
||||||
|
'Ort': data.get('ort'),
|
||||||
|
'Beschreibung': data.get('beschreibung'),
|
||||||
|
'Anschaffungsjahr': data.get('ansch_jahr'),
|
||||||
|
'Anschaffungskosten': data.get('ansch_kost'),
|
||||||
|
'Reservierbar': data.get('reservierbar'),
|
||||||
|
'ISBN': data.get('isbn'),
|
||||||
|
'ItemType': data.get('item_type'),
|
||||||
|
'LastUpdated': datetime.datetime.now()
|
||||||
|
}
|
||||||
|
|
||||||
|
# 2. Individual Updates (Specific Code Logic)
|
||||||
|
# Expected format: [{'id': '...', 'code_4': '...'}, ...]
|
||||||
|
individual_items = data.get('items', [])
|
||||||
|
|
||||||
|
try:
|
||||||
|
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
|
||||||
|
db = client[cfg.MONGODB_DB]
|
||||||
|
items_col = db['items']
|
||||||
|
|
||||||
|
# A. Apply Shared Attributes to the whole group
|
||||||
|
items_col.update_many(
|
||||||
|
{'SeriesGroupId': series_group_id},
|
||||||
|
{'$set': shared_update}
|
||||||
|
)
|
||||||
|
|
||||||
|
# B. Apply Unique Codes to specific items
|
||||||
|
# We iterate through the provided list to update the specific code for each ID
|
||||||
|
for item in individual_items:
|
||||||
|
item_id = item.get('id')
|
||||||
|
new_code = item.get('code_4')
|
||||||
|
|
||||||
|
if item_id:
|
||||||
|
items_col.update_one(
|
||||||
|
{'_id': ObjectId(item_id), 'SeriesGroupId': series_group_id},
|
||||||
|
{'$set': {'Code_4': new_code}}
|
||||||
|
)
|
||||||
|
|
||||||
|
client.close()
|
||||||
|
return jsonify({'success': True, 'message': 'Gruppe und individuelle Codes aktualisiert'})
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
app.logger.error(f"Error updating group {series_group_id}: {e}")
|
||||||
|
return jsonify({'success': False, 'message': 'Systemfehler beim Update'}), 500
|
||||||
|
|
||||||
|
|
||||||
@app.route('/report_damage/<id>', methods=['POST'])
|
@app.route('/report_damage/<id>', methods=['POST'])
|
||||||
def report_damage(id):
|
def report_damage(id):
|
||||||
"""Register a damage report entry for an item (admin only)."""
|
"""Register a damage report entry for an item (admin only)."""
|
||||||
|
|||||||
@@ -1257,80 +1257,101 @@
|
|||||||
document.getElementById('editLibraryModal').style.display = 'none';
|
document.getElementById('editLibraryModal').style.display = 'none';
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Sicherer Event-Listener für das Formular ---
|
document.getElementById('editLibraryForm').addEventListener('submit', async function(e) {
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
e.preventDefault();
|
||||||
const editForm = document.getElementById('editLibraryForm');
|
|
||||||
|
|
||||||
if (editForm) {
|
const itemId = document.getElementById('editLibraryItemId').value;
|
||||||
editForm.addEventListener('submit', async function(e) {
|
const currentItem = libraryItems.find(i => i._id === itemId);
|
||||||
e.preventDefault();
|
|
||||||
|
if (!currentItem || !currentItem.SeriesGroupId) {
|
||||||
|
alert("Fehler: Gruppen-ID nicht gefunden.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const itemId = document.getElementById('editLibraryItemId').value;
|
// 1. Alle Items der Gruppe sammeln, um die Struktur für das Backend zu bauen
|
||||||
const updateData = {
|
// Hier bauen wir die Liste für 'individual_items'
|
||||||
id: itemId,
|
const groupMembers = libraryItems.filter(i => i.SeriesGroupId === currentItem.SeriesGroupId);
|
||||||
name: document.getElementById('editLibraryName').value,
|
const individualUpdates = groupMembers.map(member => {
|
||||||
item_type: document.getElementById('editLibraryType').value,
|
return {
|
||||||
isbn: document.getElementById('editLibraryIsbn').value,
|
id: member._id,
|
||||||
code_4: document.getElementById('editLibraryCode4').value,
|
// Wenn es das aktuell bearbeitete Item ist, nimm den Wert aus dem Formular
|
||||||
ort: document.getElementById('editLibraryLocation').value,
|
// Sonst behalte den alten Code bei (oder lass ihn so, wie er ist)
|
||||||
beschreibung: document.getElementById('editLibraryDescription').value
|
code_4: (member._id === itemId) ? document.getElementById('editLibraryCode4').value : member.Code_4
|
||||||
};
|
};
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
// 2. Payload zusammenstellen
|
||||||
const response = await fetch('/api/update-item', {
|
const payload = {
|
||||||
method: 'POST',
|
series_group_id: currentItem.SeriesGroupId,
|
||||||
headers: { 'Content-Type': 'application/json' },
|
name: document.getElementById('editLibraryName').value,
|
||||||
body: JSON.stringify(updateData)
|
ort: document.getElementById('editLibraryLocation').value,
|
||||||
});
|
beschreibung: document.getElementById('editLibraryDescription').value,
|
||||||
|
ansch_jahr: null, // Falls du diese Felder im Formular hast, hier einfügen
|
||||||
|
ansch_kost: null,
|
||||||
|
reservierbar: true,
|
||||||
|
isbn: document.getElementById('editLibraryIsbn').value,
|
||||||
|
item_type: document.getElementById('editLibraryType').value,
|
||||||
|
items: individualUpdates
|
||||||
|
};
|
||||||
|
|
||||||
if (response.ok) {
|
// 3. Request senden
|
||||||
const editedItem = libraryItems.find(i => i._id === itemId);
|
try {
|
||||||
|
const response = await fetch('/update_group', {
|
||||||
// Lokale Daten im Array synchronisieren
|
method: 'POST',
|
||||||
if (editedItem && editedItem.SeriesGroupId) {
|
headers: { 'Content-Type': 'application/json' },
|
||||||
libraryItems.forEach(item => {
|
body: JSON.stringify(payload)
|
||||||
if (item.SeriesGroupId === editedItem.SeriesGroupId) {
|
|
||||||
item.Name = updateData.name;
|
|
||||||
item.ItemType = updateData.item_type;
|
|
||||||
item.ISBN = updateData.isbn;
|
|
||||||
item.Ort = updateData.ort;
|
|
||||||
item.Beschreibung = updateData.beschreibung;
|
|
||||||
|
|
||||||
if (item._id === itemId) item.Code_4 = updateData.code_4;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else if (editedItem) {
|
|
||||||
Object.assign(editedItem, updateData);
|
|
||||||
}
|
|
||||||
|
|
||||||
closeEditLibraryModal();
|
|
||||||
// Optional: renderLibraryTable();
|
|
||||||
} else {
|
|
||||||
const err = await response.json();
|
|
||||||
alert('Fehler beim Speichern: ' + (err.error || 'Unbekannter Fehler'));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Fetch error:', error);
|
|
||||||
alert('Netzwerkfehler.');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const result = await response.json();
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
// UI Update: Daten im Frontend Array aktualisieren
|
||||||
|
libraryItems.forEach(item => {
|
||||||
|
if (item.SeriesGroupId === currentItem.SeriesGroupId) {
|
||||||
|
item.Name = payload.name;
|
||||||
|
item.Ort = payload.ort;
|
||||||
|
item.Beschreibung = payload.beschreibung;
|
||||||
|
item.ISBN = payload.isbn;
|
||||||
|
item.ItemType = payload.item_type;
|
||||||
|
|
||||||
|
// Code nur für das aktive Item updaten
|
||||||
|
if (item._id === itemId) item.Code_4 = document.getElementById('editLibraryCode4').value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
closeEditLibraryModal();
|
||||||
|
alert("Gruppe erfolgreich aktualisiert!");
|
||||||
|
// renderTable(); // Deine Render-Funktion aufrufen
|
||||||
|
} else {
|
||||||
|
alert('Fehler: ' + result.message);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Fetch error:', error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div id="editLibraryModal" class="modal" style="display:none;">
|
<div id="editLibraryModal" class="modal" style="display:none;">
|
||||||
<div class="modal-content" style="max-width: 760px;">
|
<div class="modal-content" style="max-width: 760px; padding: 25px; border-radius: 8px;">
|
||||||
<span class="close" onclick="closeEditLibraryModal()">×</span>
|
<span class="close" onclick="closeEditLibraryModal()" style="cursor: pointer; float: right; font-size: 24px;">×</span>
|
||||||
<h3 style="margin-top:0;">Bibliotheksmedium bearbeiten</h3>
|
<h3 style="margin-top:0;">Bibliotheksmedium bearbeiten</h3>
|
||||||
|
|
||||||
<!-- Bereich für Gruppen-Informationen -->
|
<!-- Bereich für Gruppen-Informationen (Hier konsolidiert!) -->
|
||||||
<div id="editLibraryGroupWarning" style="display:none; background-color: #f8f9fa; padding: 15px; border-radius: 6px; margin-bottom: 20px; border: 1px solid #dee2e6;">
|
<div id="editLibraryGroupWarning" style="display:none; background-color: #fff; padding: 15px; border-radius: 6px; margin-bottom: 20px; border: 1px solid #0ea5e9;">
|
||||||
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
|
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; border-bottom: 1px solid #eee; padding-bottom: 10px;">
|
||||||
<strong style="color: #0ea5e9;">Gruppen-Synchronisierung aktiv</strong>
|
<strong style="color: #0ea5e9;">Gruppen-Range (Total: <span id="editLibraryGroupCount"></span>)</strong>
|
||||||
<span style="font-size: 12px; color: #666;">Gesamt: <span id="editLibraryGroupCount"></span> Exemplare</span>
|
</div>
|
||||||
|
|
||||||
|
<p style="margin: 5px 0; font-size: 12px; color: #555;">
|
||||||
|
Alle aufgeführten Codes gehören zu diesem Datensatz:
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<!-- Hier werden die Codes per JS eingefügt -->
|
||||||
|
<div id="editLibraryAllCodes" style="display: flex; flex-wrap: wrap; gap: 5px; margin-top: 10px;"></div>
|
||||||
|
|
||||||
|
<div style="margin-top: 15px; font-size: 11px; background: #e0f2fe; padding: 8px; border-radius: 4px;">
|
||||||
|
<strong>Hinweis:</strong> Änderungen an Titel/Ort/Beschreibung werden auf <strong>alle</strong> Exemplare der Range übertragen.
|
||||||
</div>
|
</div>
|
||||||
<p style="margin: 0 0 10px 0; font-size: 13px;">Codes in dieser Range:</p>
|
|
||||||
<div id="editLibraryAllCodes" style="display: flex; flex-wrap: wrap; gap: 5px;"></div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form id="editLibraryForm">
|
<form id="editLibraryForm">
|
||||||
@@ -1338,11 +1359,11 @@
|
|||||||
<div class="edit-grid">
|
<div class="edit-grid">
|
||||||
<div class="full">
|
<div class="full">
|
||||||
<label for="editLibraryName">Titel</label>
|
<label for="editLibraryName">Titel</label>
|
||||||
<input id="editLibraryName" required>
|
<input id="editLibraryName" required style="width: 100%;">
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="editLibraryType">Medientyp</label>
|
<label for="editLibraryType">Medientyp</label>
|
||||||
<select id="editLibraryType">
|
<select id="editLibraryType" style="width: 100%;">
|
||||||
<option value="book">Buch</option>
|
<option value="book">Buch</option>
|
||||||
<option value="schulbuch">Schulbuch</option>
|
<option value="schulbuch">Schulbuch</option>
|
||||||
<option value="cd">CD</option>
|
<option value="cd">CD</option>
|
||||||
@@ -1352,42 +1373,26 @@
|
|||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="editLibraryIsbn">ISBN</label>
|
<label for="editLibraryIsbn">ISBN</label>
|
||||||
<input id="editLibraryIsbn" placeholder="optional ISBN-10/13">
|
<input id="editLibraryIsbn" placeholder="optional ISBN-10/13" style="width: 100%;">
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="editLibraryCode4">Code</label>
|
<label for="editLibraryCode4">Code</label>
|
||||||
<input id="editLibraryCode4" placeholder="optional Mediencode">
|
<input id="editLibraryCode4" placeholder="optional Mediencode" style="width: 100%;">
|
||||||
</div>
|
</div>
|
||||||
<div class="full">
|
<div class="full">
|
||||||
<label for="editLibraryLocation">Ort</label>
|
<label for="editLibraryLocation">Ort</label>
|
||||||
<input id="editLibraryLocation" required>
|
<input id="editLibraryLocation" required style="width: 100%;">
|
||||||
</div>
|
</div>
|
||||||
<div class="full">
|
<div class="full">
|
||||||
<label for="editLibraryDescription">Beschreibung</label>
|
<label for="editLibraryDescription">Beschreibung</label>
|
||||||
<textarea id="editLibraryDescription" rows="4" required></textarea>
|
<textarea id="editLibraryDescription" rows="4" required style="width: 100%;"></textarea>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="edit-actions" style="margin-top:20px;">
|
<div class="edit-actions" style="margin-top:20px;">
|
||||||
<button type="submit" class="button" style="background:#0ea5e9;color:#fff;">Speichern</button>
|
<button type="submit" class="button" style="background:#0ea5e9;color:#fff;">Speichern & Synchronisieren</button>
|
||||||
<button type="button" class="button" onclick="closeEditLibraryModal()">Abbrechen</button>
|
<button type="button" class="button" onclick="closeEditLibraryModal()">Abbrechen</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="editLibraryGroupWarning" style="display:none; background-color: #fff; padding: 15px; border-radius: 6px; margin-bottom: 20px; border: 1px solid #0ea5e9;">
|
|
||||||
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; border-bottom: 1px solid #eee; padding-bottom: 10px;">
|
|
||||||
<strong style="color: #0ea5e9;">Gruppen-Range (Total: <span id="editLibraryGroupCount"></span>)</strong>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<p style="margin: 5px 0; font-size: 12px; color: #555;">
|
|
||||||
Alle aufgeführten Codes gehören zu diesem Datensatz:
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<!-- Hier werden die Codes per JS eingefügt -->
|
|
||||||
<div id="editLibraryAllCodes"></div>
|
|
||||||
|
|
||||||
<div style="margin-top: 15px; font-size: 11px; background: #e0f2fe; padding: 8px; border-radius: 4px;">
|
|
||||||
<strong>Hinweis:</strong> Änderungen an Titel/Ort/Beschreibung werden auf <strong>alle</strong> Exemplare der Range übertragen.
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user