mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
added lesson update for course
This commit is contained in:
+24
-13
@@ -2,15 +2,15 @@ package com.jetbrains.edu.coursecreator.actions;
|
||||
|
||||
import com.intellij.ide.IdeView;
|
||||
import com.intellij.ide.util.DirectoryChooserUtil;
|
||||
import com.intellij.openapi.actionSystem.*;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||
import com.intellij.openapi.actionSystem.LangDataKeys;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.ProgressManager;
|
||||
import com.intellij.openapi.progress.Task;
|
||||
import com.intellij.openapi.project.DumbAwareAction;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
import com.jetbrains.edu.EduUtils;
|
||||
import com.jetbrains.edu.courseFormat.Course;
|
||||
import com.jetbrains.edu.courseFormat.Lesson;
|
||||
import com.jetbrains.edu.coursecreator.CCProjectService;
|
||||
@@ -19,17 +19,28 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class CCPushLesson extends DumbAwareAction {
|
||||
public CCPushLesson() {
|
||||
super("Push Lesson to Stepic", "Push Lesson to Stepic", null);
|
||||
super("Update Lesson on Stepic", "Update Lesson on Stepic", null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(@NotNull AnActionEvent e) {
|
||||
CCProjectService.setCCActionAvailable(e);
|
||||
final DataContext dataContext = e.getDataContext();
|
||||
final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
|
||||
final Project project = e.getProject();
|
||||
if (virtualFile == null || !virtualFile.isDirectory() || (project != null && virtualFile.equals(project.getBaseDir()))) {
|
||||
EduUtils.enableAction(e, false);
|
||||
e.getPresentation().setEnabledAndVisible(false);
|
||||
final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
|
||||
final Project project = e.getData(CommonDataKeys.PROJECT);
|
||||
if (view == null || project == null) {
|
||||
return;
|
||||
}
|
||||
final Course course = CCProjectService.getInstance(project).getCourse();
|
||||
if (course == null) {
|
||||
return;
|
||||
}
|
||||
PsiDirectory lessonDir = DirectoryChooserUtil.getOrChooseDirectory(view);
|
||||
if (lessonDir == null || !lessonDir.getName().contains("lesson")) {
|
||||
return;
|
||||
}
|
||||
final Lesson lesson = course.getLesson(lessonDir.getName());
|
||||
if (lesson != null && lesson.id > 0) {
|
||||
e.getPresentation().setEnabledAndVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,11 +63,11 @@ public class CCPushLesson extends DumbAwareAction {
|
||||
if (lesson == null) {
|
||||
return;
|
||||
}
|
||||
ProgressManager.getInstance().run(new Task.Modal(project, "Uploading Course", true) {
|
||||
ProgressManager.getInstance().run(new Task.Modal(project, "Uploading Lesson", true) {
|
||||
@Override
|
||||
public void run(@NotNull ProgressIndicator indicator) {
|
||||
indicator.setText("Uploading course to http://stepic.org");
|
||||
EduStepicConnector.postLesson(project, lesson, indicator);
|
||||
indicator.setText("Uploading lesson to http://stepic.org");
|
||||
EduStepicConnector.updateLesson(project, lesson, indicator);
|
||||
}});
|
||||
}
|
||||
|
||||
|
||||
@@ -557,6 +557,42 @@ public class EduStepicConnector {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int updateLesson(Project project, @NotNull final Lesson lesson, ProgressIndicator indicator) {
|
||||
final HttpPut request = new HttpPut(stepicApiUrl + "lessons/" + String.valueOf(lesson.id));
|
||||
if (ourClient == null) {
|
||||
showLoginDialog();
|
||||
}
|
||||
|
||||
setHeaders(request, "application/json");
|
||||
String requestBody = new Gson().toJson(new LessonWrapper(lesson));
|
||||
request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
|
||||
|
||||
try {
|
||||
final CloseableHttpResponse response = ourClient.execute(request);
|
||||
final HttpEntity responseEntity = response.getEntity();
|
||||
final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
|
||||
final StatusLine line = response.getStatusLine();
|
||||
if (line.getStatusCode() != 200) {
|
||||
LOG.error("Failed to push " + responseString);
|
||||
return 0;
|
||||
}
|
||||
final Lesson postedLesson = new Gson().fromJson(responseString, Course.class).getLessons().get(0);
|
||||
for (Integer step : postedLesson.steps) {
|
||||
deleteTask(step);
|
||||
}
|
||||
|
||||
for (Task task : lesson.getTaskList()) {
|
||||
indicator.checkCanceled();
|
||||
postTask(project, task, lesson.id);
|
||||
}
|
||||
return lesson.id;
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e.getMessage());
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int postLesson(Project project, @NotNull final Lesson lesson, ProgressIndicator indicator) {
|
||||
final HttpPost request = new HttpPost(stepicApiUrl + "lessons");
|
||||
if (ourClient == null) {
|
||||
@@ -577,6 +613,7 @@ public class EduStepicConnector {
|
||||
return 0;
|
||||
}
|
||||
final Lesson postedLesson = new Gson().fromJson(responseString, Course.class).getLessons().get(0);
|
||||
lesson.id = postedLesson.id;
|
||||
for (Task task : lesson.getTaskList()) {
|
||||
indicator.checkCanceled();
|
||||
postTask(project, task, postedLesson.id);
|
||||
@@ -589,6 +626,28 @@ public class EduStepicConnector {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static void deleteTask(@NotNull final Integer task) {
|
||||
final HttpDelete request = new HttpDelete(stepicApiUrl + "step-sources/" + task);
|
||||
setHeaders(request, "application/json");
|
||||
ApplicationManager.getApplication().invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
final CloseableHttpResponse response = ourClient.execute(request);
|
||||
final StatusLine line = response.getStatusLine();
|
||||
if (line.getStatusCode() != 204) {
|
||||
final HttpEntity responseEntity = response.getEntity();
|
||||
final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
|
||||
LOG.error("Failed to delete task " + responseString);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void postTask(final Project project, @NotNull final Task task, final int lessonId) {
|
||||
final HttpPost request = new HttpPost(stepicApiUrl + "step-sources");
|
||||
setHeaders(request, "application/json");
|
||||
@@ -735,6 +794,8 @@ public class EduStepicConnector {
|
||||
public LessonWrapper(Lesson lesson) {
|
||||
this.lesson = new Lesson();
|
||||
this.lesson.setName(lesson.getName());
|
||||
this.lesson.id = lesson.id;
|
||||
this.lesson.steps = new ArrayList<Integer>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user