Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rhul-scripts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Whyte Cameron (2023) ZMAC302
rhul-scripts
Commits
079a5669
Commit
079a5669
authored
3 years ago
by
Hague Matthew UXAC009
Browse files
Options
Downloads
Patches
Plain Diff
Add missing select users script
parent
fdfae26f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
moodle/assignment-select-users.py
+120
-0
120 additions, 0 deletions
moodle/assignment-select-users.py
with
120 additions
and
0 deletions
moodle/assignment-select-users.py
0 → 100644
+
120
−
0
View file @
079a5669
# Script for selecting users on the Moodle assignment submissions page.
#
# E.g. for granting extensions in bulk. Run this script to select all
# the users. Then use the "with selected" drop down to grant the
# extension.
#
# Usage:
# python3 assignment-select-users.py <emails.csv> <assignment-grades-page-url>
#
# emails.csv should contain a column with the header "Email"
#
# Tries to save time by copying your cookies from your Firefox profile.
# Then if you're logged in on your normal firefox, login should be a
# single click with this script
import
browser_cookie3
import
csv
import
sys
from
typing
import
Dict
,
List
,
Optional
,
Set
from
selenium
import
webdriver
from
selenium.webdriver
import
Firefox
from
selenium.webdriver.common.by
import
By
MOODLE_URL
=
"
https://moodle.royalholloway.ac.uk
"
MOODLE_DOMAIN
=
"
moodle.royalholloway.ac.uk
"
MOODLE_SESSION_COOKIE
=
"
MoodleSession
"
WAIT_TIMEOUT
=
10
# s
class
Browser
:
"""
Simple container of browser setup and teardown
Access selenium driver with .driver field
"""
def
__init__
(
self
):
self
.
_profile_dir
=
None
self
.
driver
=
None
def
__enter__
(
self
):
self
.
driver
=
webdriver
.
Firefox
()
self
.
driver
.
implicitly_wait
(
WAIT_TIMEOUT
)
return
self
def
__exit__
(
self
,
*
kwargs
):
if
self
.
driver
is
not
None
:
self
.
driver
.
quit
()
def
login
(
self
):
"""
Login to the browser Moodle session
"""
if
self
.
driver
:
self
.
driver
.
get
(
MOODLE_URL
)
for
cookie
in
self
.
_get_moodle_cookies
():
self
.
driver
.
add_cookie
(
cookie
)
input
(
"
Press enter when logged in.
"
)
else
:
raise
Exception
(
"
Cannot log in, no web driver initialised.
"
)
def
_get_moodle_cookies
(
self
)
->
List
[
Dict
[
str
,
Optional
[
str
]]]:
"""
Gets current moodle cookies from firefox
"""
cookie_jar
=
browser_cookie3
.
firefox
(
domain_name
=
MOODLE_DOMAIN
)
return
[
{
"
name
"
:
c
.
name
,
"
value
"
:
c
.
value
}
for
c
in
cookie_jar
if
c
.
name
==
MOODLE_SESSION_COOKIE
]
def
select_users
(
browser
:
Browser
,
emails
:
Set
[
str
],
assignment_grades_page_url
:
str
):
"""
Select the users on the given page
:param browser: a logged in Moodle browser object
:param emails: list of emails of students to select in lower case
:param assignment_grades_page_url: URL of the grades page where
users can be selected
"""
if
browser
.
driver
is
None
:
return
# Open edit timing section
browser
.
driver
.
get
(
assignment_grades_page_url
+
"
&tilast&tifirst
"
)
email_nodes
=
browser
.
driver
.
find_elements
(
By
.
CSS_SELECTOR
,
"
td.email
"
)
checked_emails
=
set
()
for
email_node
in
email_nodes
:
email
=
email_node
.
text
.
lower
()
if
email
in
emails
:
email_node
.
find_element
(
By
.
XPATH
,
"
parent::*//input[@type=
'
checkbox
'
]
"
).
click
()
checked_emails
.
add
(
email
)
print
(
len
(
checked_emails
),
"
emails selected
"
)
for
email
in
emails
-
checked_emails
:
print
(
"
WARNING: did not select
"
,
email
)
def
get_emails
(
email_file_csv
:
str
):
"""
Returns emails in lower case
"""
with
open
(
email_file_csv
)
as
f
:
return
{
row
[
"
Email
"
].
lower
()
for
row
in
csv
.
DictReader
(
f
)
}
def
main
(
argv
):
"""
Go, go, go!
"""
if
len
(
argv
)
<
2
:
print
(
"
python3 assignment-select-users.py <emails.csv> <assignment-grades-page-url>
"
)
return
email_file_csv
=
argv
[
0
]
assignment_grades_page_url
=
argv
[
1
]
emails
=
get_emails
(
email_file_csv
)
with
Browser
()
as
browser
:
browser
.
login
()
select_users
(
browser
,
emails
,
assignment_grades_page_url
)
input
(
"
Users selected, press enter when done
"
)
main
(
sys
.
argv
[
1
:])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment