-
Notifications
You must be signed in to change notification settings - Fork 8
add test print button in admin panel #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LookleftPL
wants to merge
5
commits into
develop
Choose a base branch
from
feature/test-print-admin
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8d2f9e8
feature:add test print button using JS fetch and refactor print services
LookleftPL 7063f54
Adding more docstrings
LookleftPL 27d9e39
Splitting the button into four buttons with different printing options.
LookleftPL c2bbd42
editing CHANGELOG.md
LookleftPL 6513515
Renaming tests
LookleftPL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /** | ||
| * Admin Test Print Handler | ||
| * | ||
| * Handles click events for test print buttons in the Django Admin interface. | ||
| * Sends a POST request with a JSON payload containing 'color' and 'duplex' parameters. | ||
| */ | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function() { | ||
| // Select all test print action buttons | ||
| const testPrintButtons = document.querySelectorAll('.admin-test-print-btn'); | ||
|
|
||
| /** | ||
| * Helper function to retrieve a cookie value by name (used for CSRF token). | ||
| * | ||
| * @param {string} name - The name of the cookie. | ||
| * @returns {string|null} The cookie value or null if not found. | ||
| */ | ||
| function getCookie(name) { | ||
| let cookieValue = null; | ||
| if (document.cookie && document.cookie !== '') { | ||
| const cookies = document.cookie.split(';'); | ||
| for (let i = 0; i < cookies.length; i++) { | ||
| const cookie = cookies[i].trim(); | ||
| if (cookie.substring(0, name.length + 1) === (name + '=')) { | ||
| cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return cookieValue; | ||
| } | ||
|
|
||
| testPrintButtons.forEach(button => { | ||
| button.addEventListener('click', function(event) { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
|
|
||
| const url = this.getAttribute('data-url'); | ||
| const color = this.getAttribute('data-color') === 'true'; | ||
| const duplex = this.getAttribute('data-duplex') === 'true'; | ||
|
|
||
| const csrfInput = document.querySelector('[name=csrfmiddlewaretoken]'); | ||
| const csrftoken = csrfInput ? csrfInput.value : getCookie('csrftoken'); | ||
|
|
||
| const payload = { | ||
| color: color, | ||
| duplex: duplex | ||
| }; | ||
|
|
||
| console.log('Sending test print request:', { url, payload }); | ||
|
|
||
| fetch(url, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-CSRFToken': csrftoken | ||
| }, | ||
| body: JSON.stringify(payload) | ||
| }) | ||
| .then(response => { | ||
| console.log('Response status:', response.status); | ||
| return response.json(); | ||
| }) | ||
| .then(data => { | ||
| console.log('Response data:', data); | ||
|
|
||
| if (data.status === 'ok') { | ||
| // Success — display alert and reload page | ||
| alert(`Test print job #${data.job_id} successfully dispatched!`); | ||
| setTimeout(() => { | ||
| window.location.reload(); | ||
| }, 500); | ||
| } else { | ||
| // Error response — display error details | ||
| const errorMessage = data.message || 'Unknown error'; | ||
| const details = data.details ? `\n\nDetails: ${data.details}` : ''; | ||
| alert(`Test print failed: ${errorMessage}${details}`); | ||
| } | ||
| }) | ||
| .catch(error => { | ||
| console.error('Error during test print request:', error); | ||
| alert(`Network error: ${error.message}`); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The term simplex is not used anywhere else in the UI or code. I would also avoid the term duplex - most of the code and the entire web UI use the terms one-sided and two-sided.
I would suggest sticking to one naming convention (both in the UI and in code) and in my opinion one-sided/two-sided is easier to understand for unfamiliar users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The term black and white is incorrect here.
We support two color modes:
monochromein the Internet Printing Protocol)grayscale (monochrome) is different from black and white (IPP:
bi-level) - the former can print a spectrum of colors from white to black, while the latter can only print two colorsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current webapp UI:

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also use this opportunity to adjust these terms in the admin panel: