Skip to content

Commit ea173d2

Browse files
committed
install updates
1 parent b8fde08 commit ea173d2

4 files changed

Lines changed: 147 additions & 2 deletions

File tree

test_button.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ def test_button():
4141
else:
4242
print("❌ Version icon NOT found")
4343

44-
# Test clicking the button
44+
# Check for install update button
45+
if 'Install Update' in content:
46+
print("✅ Install Update button found!")
47+
else:
48+
print("❌ Install Update button NOT found")
49+
50+
# Test clicking the check updates button
4551
response = client.get('/admin/updates/versioncheck/?check_updates=1')
4652
print(f"Check updates response: {response.status_code}")
4753

@@ -51,6 +57,21 @@ def test_button():
5157
messages = list(get_messages(response.wsgi_request))
5258
for message in messages:
5359
print(f"Message: {message}")
60+
61+
# Now check if install button appears after update check
62+
response = client.get('/admin/updates/versioncheck/')
63+
content = response.content.decode('utf-8')
64+
if 'Install Update' in content:
65+
print("✅ Install Update button appeared after check!")
66+
67+
# Test install button (but don't actually install)
68+
print("🧪 Testing install update URL (simulation mode)")
69+
if 'install_update=1' in content:
70+
print("✅ Install URL parameter found!")
71+
else:
72+
print("❌ Install URL parameter NOT found")
73+
else:
74+
print("❌ Install Update button NOT found after check")
5475

5576
if __name__ == '__main__':
5677
test_button()

updates/admin.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,27 @@ def update_status(self, obj):
5858

5959
def changelist_view(self, request, extra_context=None):
6060
"""Add check updates button to the changelist"""
61+
# Handle install update request
62+
if request.GET.get('install_update'):
63+
from .git_checker import git_checker
64+
from django.contrib import messages
65+
66+
# Get the latest version to install
67+
latest_check = VersionCheck.objects.filter(check_successful=True, update_available=True).first()
68+
if latest_check:
69+
result = git_checker.install_update(latest_check.latest_version)
70+
71+
if result['success']:
72+
messages.success(request,
73+
f"🎉 Successfully updated to version {result['new_version']}! "
74+
f"Please restart the server to apply changes.")
75+
else:
76+
messages.error(request, f"❌ Update failed: {result.get('error', 'Unknown error')}")
77+
else:
78+
messages.error(request, "❌ No available update found to install.")
79+
6180
# Handle check updates request
62-
if request.GET.get('check_updates'):
81+
elif request.GET.get('check_updates'):
6382
from .git_checker import git_checker
6483
from .models import UpdateSettings
6584

@@ -104,6 +123,13 @@ def changelist_view(self, request, extra_context=None):
104123
current_version = git_checker.get_current_version()
105124
extra_context['current_version'] = current_version
106125

126+
# Check if update is available and add install button
127+
latest_check = VersionCheck.objects.filter(check_successful=True).first()
128+
if latest_check and latest_check.update_available:
129+
install_url = request.path + '?install_update=1'
130+
extra_context['install_update_url'] = install_url
131+
extra_context['latest_version'] = latest_check.latest_version
132+
107133
return super().changelist_view(request, extra_context)
108134

109135

updates/git_checker.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,92 @@ def check_for_updates(self, include_prereleases=False):
178178
'latest_version': self.get_current_version(),
179179
'update_available': False,
180180
}
181+
182+
def install_update(self, target_version):
183+
"""Install update by pulling and checking out the specified version"""
184+
try:
185+
current_version = self.get_current_version()
186+
187+
# Ensure we have a clean working directory
188+
result = subprocess.run(
189+
['git', 'status', '--porcelain'],
190+
capture_output=True,
191+
text=True,
192+
cwd=settings.BASE_DIR
193+
)
194+
195+
if result.stdout.strip():
196+
return {
197+
'success': False,
198+
'error': 'Working directory is not clean. Please commit or stash changes before updating.',
199+
'current_version': current_version,
200+
}
201+
202+
# Fetch latest changes from remote
203+
result = subprocess.run(
204+
['git', 'fetch', '--tags'],
205+
capture_output=True,
206+
text=True,
207+
cwd=settings.BASE_DIR
208+
)
209+
210+
if result.returncode != 0:
211+
return {
212+
'success': False,
213+
'error': f'Failed to fetch updates: {result.stderr}',
214+
'current_version': current_version,
215+
}
216+
217+
# Checkout the target version
218+
tag_name = target_version if target_version.startswith('v') else f'v{target_version}'
219+
result = subprocess.run(
220+
['git', 'checkout', tag_name],
221+
capture_output=True,
222+
text=True,
223+
cwd=settings.BASE_DIR
224+
)
225+
226+
if result.returncode != 0:
227+
# Try without 'v' prefix
228+
result = subprocess.run(
229+
['git', 'checkout', target_version],
230+
capture_output=True,
231+
text=True,
232+
cwd=settings.BASE_DIR
233+
)
234+
235+
if result.returncode != 0:
236+
return {
237+
'success': False,
238+
'error': f'Failed to checkout version {target_version}: {result.stderr}',
239+
'current_version': current_version,
240+
}
241+
242+
# Update version file if it exists
243+
version_file = settings.BASE_DIR / 'version.json'
244+
if version_file.exists():
245+
try:
246+
with open(version_file, 'w') as f:
247+
json.dump({'version': target_version}, f, indent=2)
248+
except Exception as e:
249+
logger.warning(f"Could not update version file: {e}")
250+
251+
new_version = self.get_current_version()
252+
253+
return {
254+
'success': True,
255+
'message': f'Successfully updated from {current_version} to {new_version}',
256+
'old_version': current_version,
257+
'new_version': new_version,
258+
}
259+
260+
except Exception as e:
261+
logger.error(f"Error installing update: {e}")
262+
return {
263+
'success': False,
264+
'error': str(e),
265+
'current_version': self.get_current_version(),
266+
}
181267

182268

183269
# Global instance

updates/templates/admin/updates/versioncheck/change_list.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@
66
<div style="background: #f8f9fa; border: 1px solid #dee2e6; padding: 10px; margin: 10px 0; border-radius: 4px;">
77
<strong>📦 Current Installed Version: </strong>
88
<span style="color: #007cba; font-weight: bold; font-size: 1.1em;">{{ current_version }}</span>
9+
{% if latest_version %}
10+
<br><strong>🆙 Update Available: </strong>
11+
<span style="color: #28a745; font-weight: bold; font-size: 1.1em;">{{ latest_version }}</span>
12+
{% endif %}
913
</div>
1014
{% endif %}
1115
{% endblock %}
1216

1317
{% block object-tools-items %}
1418
{{ block.super }}
19+
{% if install_update_url %}
20+
<li>
21+
<a href="{{ install_update_url }}" class="addlink" style="background: #28a745; color: white;"
22+
onclick="return confirm('Are you sure you want to install the update? This will update your CMS to version {{ latest_version }}.');">
23+
🚀 Install Update to {{ latest_version }}
24+
</a>
25+
</li>
26+
{% endif %}
1527
{% if check_updates_url %}
1628
<li>
1729
<a href="{{ check_updates_url }}" class="addlink" style="background: #007cba; color: white;">

0 commit comments

Comments
 (0)