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
3ff442be
Commit
3ff442be
authored
3 years ago
by
Hague Matthew UXAC009
Browse files
Options
Downloads
Patches
Plain Diff
Update quiz-change-times to new cookie handling
parent
cfa653e6
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/quiz-change-times.py
+20
-22
20 additions, 22 deletions
moodle/quiz-change-times.py
with
20 additions
and
22 deletions
moodle/quiz-change-times.py
+
20
−
22
View file @
3ff442be
...
...
@@ -14,30 +14,24 @@
# * --increment=dd:hh:mm: increment the open/close time by days hours mins
# * --set="yyyy-mm-dd hh:mm": set all open/close dates to given date
#
# Tries to save time by copying your cookies from your Firefox
# cookies.sqlite. Edit the USER_COOKIES variable to point to your
# cookies to enable this. Then if you're logged in on your normal
# firefox, login should be a single click with this script
# 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
getopt
import
os
import
shutil
import
re
import
sys
import
tempfile
from
datetime
import
datetime
,
timedelta
from
typing
import
Any
,
List
,
Optional
from
typing
import
Any
,
Dict
,
List
,
Optional
from
selenium
import
webdriver
from
selenium.webdriver.support
import
expected_conditions
as
ec
from
selenium.webdriver.support.ui
import
Select
from
selenium.webdriver.support.ui
import
WebDriverWait
USER_COOKIES
\
=
"
/home/matt/.mozilla/firefox/g4q3fupz.default-release/cookies.sqlite
"
MOODLE_URL
=
"
https://moodle.royalholloway.ac.uk
"
QUIZ_EDIT_URL
\
=
"
https://moodle.royalholloway.ac.uk/course/modedit.php?update={}&return=1
"
...
...
@@ -46,6 +40,9 @@ QUIZ_ID_GRP = 1
WAIT_TIMEOUT
=
60
# seconds
MOODLE_DOMAIN
=
"
moodle.royalholloway.ac.uk
"
MOODLE_SESSION_COOKIE
=
"
MoodleSession
"
class
Browser
:
"""
Simple container of browser setup and teardown
...
...
@@ -56,32 +53,33 @@ class Browser:
self
.
driver
=
None
def
__enter__
(
self
):
self
.
_profile_dir
=
tempfile
.
TemporaryDirectory
()
if
os
.
path
.
isfile
(
USER_COOKIES
):
shutil
.
copy
(
USER_COOKIES
,
self
.
_profile_dir
.
name
)
self
.
driver
=
webdriver
.
Firefox
(
firefox_profile
=
webdriver
.
FirefoxProfile
(
self
.
_profile_dir
.
name
)
)
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
()
if
self
.
_profile_dir
is
not
None
:
self
.
_profile_dir
.
cleanup
()
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
get_quiz_edit_url
(
quiz_url
:
str
)
->
str
:
"""
Convert a quiz URL into the page URL for editing the quiz
"""
if
match
:
=
QUIZ_ID_RE
.
match
(
quiz_url
):
...
...
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