Newer
Older
# -*- coding: utf-8 -*-
from __future__ import division
__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from django.shortcuts import ( # noqa
render, get_object_or_404, redirect)
from django.contrib import messages
import django.forms as forms
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from course.models import (
Course,
Participation, participation_role, participation_status)
from course.content import (get_course_repo, get_course_desc)
from course.views import (
get_role_and_participation, get_active_commit_sha
)
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
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
from django.db import transaction
# {{{ new course setup
class CourseCreationForm(forms.ModelForm):
class Meta:
model = Course
fields = [
'identifier', 'git_source', 'ssh_private_key',
'enrollment_approval_required',
'enrollment_required_email_suffix',
'course_robot_email_address']
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_class = "form-horizontal"
self.helper.label_class = "col-lg-2"
self.helper.field_class = "col-lg-8"
self.helper.add_input(
Submit("submit", "Validate and create",
css_class="col-lg-offset-2"))
super(CourseCreationForm, self).__init__(*args, **kwargs)
@login_required
@transaction.atomic
def set_up_new_course(request):
if not request.user.is_staff:
raise PermissionDenied("only staff may create courses")
if request.method == "POST":
form = CourseCreationForm(request.POST)
if form.is_valid():
new_course = form.save(commit=False)
from course.content import get_course_repo_path
repo_path = get_course_repo_path(new_course)
try:
import os
os.makedirs(repo_path)
from dulwich.repo import Repo
repo = Repo.init(repo_path)
from dulwich.client import get_transport_and_path
client, remote_path = get_transport_and_path(
new_course.git_source.encode())
remote_refs = client.fetch(remote_path, repo)
new_sha = repo["HEAD"] = remote_refs["HEAD"]
from course.validation import validate_course_content
validate_course_content(repo, new_sha)
new_course.active_git_commit_sha = new_sha
new_course.save()
# {{{ set up a participation for the course creator
part = Participation()
part.user = request.user
part.course = new_course
part.role = participation_role.instructor
part.status = participation_status.active
part.save()
# }}}
except Exception as e:
import shutil
shutil.rmtree(repo_path)
messages.add_message(request, messages.ERROR,
"Course creation failed: %s: %s" % (
type(e).__name__, str(e)))
else:
return redirect(
"course.views.course_page",
new_course.identifier)
else:
return render(request, "generic-form.html", {
"form_description": "Set up new course",
"form": form
})
# }}}
# {{{ fetch
class GitFetchForm(forms.Form):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_class = "form-horizontal"
self.helper.label_class = "col-lg-2"
self.helper.field_class = "col-lg-8"
super(GitFetchForm, self).__init__(*args, **kwargs)
self.helper.add_input(Submit("fetch", "Fetch"))
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
def fetch_course_updates(request, course_identifier):
import sys
course = get_object_or_404(Course, identifier=course_identifier)
role, participation = get_role_and_participation(request, course)
if role != participation_role.instructor:
raise PermissionDenied("must be instructor to fetch revisisons")
commit_sha = get_active_commit_sha(course, participation)
repo = get_course_repo(course)
course_desc = get_course_desc(repo, commit_sha)
form = GitFetchForm(request.POST, request.FILES)
if request.method == "POST":
if form.is_valid():
was_successful = True
log_lines = []
try:
repo = get_course_repo(course)
if not course.git_source:
raise RuntimeError("no git source URL specified")
if course.ssh_private_key:
repo.auth(pkey=course.ssh_private_key.encode("ascii"))
log_lines.append("Pre-fetch head is at '%s'" % repo.head())
from dulwich.client import get_transport_and_path
client, remote_path = get_transport_and_path(
course.git_source.encode())
remote_refs = client.fetch(remote_path, repo)
repo["HEAD"] = remote_refs["HEAD"]
log_lines.append("Post-fetch head is at '%s'" % repo.head())
except Exception:
was_successful = False
from traceback import format_exception
log = "\n".join(log_lines) + "".join(
format_exception(*sys.exc_info()))
else:
log = "\n".join(log_lines)
return render(request, 'course/course-bulk-result.html', {
"process_description": "Fetch course updates via git",
"log": log,
"status": "Pull successful."
if was_successful
else "Pull failed. See above for error.",
"was_successful": was_successful,
"course": course,
"course_desc": course_desc,
})
else:
form = GitFetchForm()
else:
form = GitFetchForm()
return render(request, "course/generic-course-form.html", {
"participation": participation,
"form": form,
"form_description": "Fetch New Course Revisions",
"course": course,
"course_desc": course_desc,
})
# }}}
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
class GitUpdateForm(forms.Form):
new_sha = forms.CharField(required=True, initial=50)
def __init__(self, previewing, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_class = "form-horizontal"
self.helper.label_class = "col-lg-2"
self.helper.field_class = "col-lg-8"
if previewing:
self.helper.add_input(
Submit("end_preview", "End preview",
css_class="col-lg-offset-2"))
else:
self.helper.add_input(
Submit("preview", "Validate and preview",
css_class="col-lg-offset-2"))
self.helper.add_input(
Submit("update", "Validate and update"))
super(GitUpdateForm, self).__init__(*args, **kwargs)
def update_course(request, course_identifier):
course = get_object_or_404(Course, identifier=course_identifier)
role, participation = get_role_and_participation(request, course)
if role != participation_role.instructor:
raise PermissionDenied("must be instructor to update course")
commit_sha = get_active_commit_sha(course, participation)
repo = get_course_repo(course)
course_desc = get_course_desc(repo, commit_sha)
previewing = bool(participation is not None
and participation.preview_git_commit_sha)
response_form = None
if request.method == "POST":
form = GitUpdateForm(previewing, request.POST, request.FILES)
if "end_preview" in form.data:
messages.add_message(request, messages.INFO,
"Preview ended.")
participation.preview_git_commit_sha = None
participation.save()
previewing = False
elif form.is_valid():
new_sha = form.cleaned_data["new_sha"].encode("utf-8")
from course.validation import validate_course_content, ValidationError
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
try:
validate_course_content(repo, new_sha)
except ValidationError as e:
messages.add_message(request, messages.ERROR,
"Course content did not validate successfully. (%s) "
"Update not applied."
% str(e))
validated = False
else:
messages.add_message(request, messages.INFO,
"Course content validated successfully.")
validated = True
if validated and "update" in form.data:
messages.add_message(request, messages.INFO,
"Update applied.")
course.active_git_commit_sha = new_sha
course.save()
response_form = form
elif validated and "preview" in form.data:
messages.add_message(request, messages.INFO,
"Preview activated.")
participation.preview_git_commit_sha = new_sha
participation.save()
previewing = True
if response_form is None:
form = GitUpdateForm(previewing,
{"new_sha": repo.head()})
text_lines = [
"<b>Current git HEAD:</b> %s (%s)" % (
repo.head(),
repo[repo.head()].message),
"<b>Public active git SHA:</b> %s (%s)" % (
course.active_git_commit_sha,
repo[course.active_git_commit_sha.encode()].message),
]
if participation is not None and participation.preview_git_commit_sha:
text_lines.append(
"<b>Current preview git SHA:</b> %s (%s)" % (
participation.preview_git_commit_sha,
repo[participation.preview_git_commit_sha.encode()].message,
))
else:
text_lines.append("<b>Current preview git SHA:</b> None")
return render(request, "course/generic-course-form.html", {
"participation": participation,
"form": form,
"form_text": "".join(
"<p>%s</p>" % line
for line in text_lines
),
"form_description": "Update Course Revision",
"course": course,
"course_desc": course_desc,
})
# }}}
# vim: foldmethod=marker