-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_script.py
More file actions
149 lines (132 loc) · 4.78 KB
/
Copy pathreplace_script.py
File metadata and controls
149 lines (132 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import sys
with open('tracker/templates/tracker/dashboard.html', 'r', encoding='utf-8') as f:
content = f.read()
old_func = """function uploadNativeSnap() {
if (!selectedFile) {
alert("Please select a photo first.");
return;
}
const caption = document.getElementById('snapCaption').value.trim();
const fd = new FormData();
fd.append('title', caption);
const now = new Date();
const yyyy = now.getFullYear();
const mm = String(now.getMonth() + 1).padStart(2, '0');
const dd = String(now.getDate()).padStart(2, '0');
fd.append('date', `${yyyy}-${mm}-${dd}`);
fd.append('images', selectedFile);
fd.append('captions', caption);
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
const btn = document.querySelector('button[onclick="uploadNativeSnap()"]');
const origText = btn.innerHTML;
btn.innerHTML = 'Saving... ⏳';
btn.disabled = true;
fetch('/dashboard/snap/upload/', {
method: 'POST',
headers: {
'X-CSRFToken': csrftoken,
'X-Requested-With': 'XMLHttpRequest'
},
body: fd
})
.then(res => res.json())
.then(data => {
if (data.status === 'success') {
location.reload();
} else {
alert("Error saving memory.");
btn.innerHTML = origText;
btn.disabled = false;
}
}).catch(err => {
console.error(err);
alert("Network error.");
btn.innerHTML = origText;
btn.disabled = false;
});
}"""
new_func = """function compressImage(file, maxWidth, maxHeight, quality, callback) {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(event) {
const img = new Image();
img.src = event.target.result;
img.onload = function() {
let width = img.width;
let height = img.height;
if (width > height) {
if (width > maxWidth) {
height *= maxWidth / width;
width = maxWidth;
}
} else {
if (height > maxHeight) {
width *= maxHeight / height;
height = maxHeight;
}
}
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, width, height);
canvas.toBlob(function(blob) {
callback(blob);
}, 'image/jpeg', quality);
};
};
}
function uploadNativeSnap() {
if (!selectedFile) {
alert("Please select a photo first.");
return;
}
const caption = document.getElementById('snapCaption').value.trim();
const btn = document.querySelector('button[onclick="uploadNativeSnap()"]') || document.querySelector('.snap-upload-btn');
const origText = btn ? btn.innerHTML : 'Save';
if(btn) {
btn.innerHTML = 'Optimizing... ⏳';
btn.disabled = true;
}
compressImage(selectedFile, 1200, 1200, 0.8, function(compressedBlob) {
const fd = new FormData();
fd.append('title', caption);
const now = new Date();
const yyyy = now.getFullYear();
const mm = String(now.getMonth() + 1).padStart(2, '0');
const dd = String(now.getDate()).padStart(2, '0');
fd.append('date', `${yyyy}-${mm}-${dd}`);
fd.append('images', compressedBlob, selectedFile.name || 'snap.jpg');
fd.append('captions', caption);
const csrftoken = document.querySelector('[name=csrfmiddlewaretoken]').value;
if(btn) btn.innerHTML = 'Uploading... 🚀';
fetch('/dashboard/snap/upload/', {
method: 'POST',
headers: {
'X-CSRFToken': csrftoken,
'X-Requested-With': 'XMLHttpRequest'
},
body: fd
})
.then(res => res.json())
.then(data => {
if (data.status === 'success') {
location.reload();
} else {
alert("Error saving memory.");
if(btn) { btn.innerHTML = origText; btn.disabled = false; }
}
}).catch(err => {
console.error(err);
alert("Network error.");
if(btn) { btn.innerHTML = origText; btn.disabled = false; }
});
});
}"""
if old_func in content:
content = content.replace(old_func, new_func)
with open('tracker/templates/tracker/dashboard.html', 'w', encoding='utf-8') as f:
f.write(content)
print("Successfully replaced.")
else:
print("Did not find old function in dashboard.html. It might be slightly different.")