fixed EDU-229 Upload to repository action

added upload to stepic.org action
This commit is contained in:
Ekaterina Tuzova
2015-07-16 14:58:05 +03:00
parent 33d5dee30b
commit 29ccbc8988
4 changed files with 172 additions and 19 deletions
@@ -88,6 +88,11 @@
<add-to-group group-id="ProjectViewPopupMenu" anchor="before" relative-to-action="CutCopyPasteGroup"/>
<add-to-group group-id="FileMenu" relative-to-action="FileMainSettingsGroup" anchor="before"/>
</action>
<action id="PushCourse" class="com.jetbrains.edu.coursecreator.actions.CCPushCourse">
<add-to-group group-id="MainToolBar" anchor="last" />
<add-to-group group-id="ProjectViewPopupMenu" anchor="before" relative-to-action="CutCopyPasteGroup"/>
<add-to-group group-id="FileMenu" relative-to-action="FileMainSettingsGroup" anchor="before"/>
</action>
</actions>
</idea-plugin>
@@ -0,0 +1,38 @@
package com.jetbrains.edu.coursecreator.actions;
import com.intellij.ide.IdeView;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.jetbrains.edu.courseFormat.Course;
import com.jetbrains.edu.coursecreator.CCProjectService;
import com.jetbrains.edu.stepic.EduStepicConnector;
import org.jetbrains.annotations.NotNull;
public class CCPushCourse extends DumbAwareAction {
public CCPushCourse() {
super("Upload course to Stepic", "Upload course to Stepic", null);
}
@Override
public void update(@NotNull AnActionEvent e) {
CCProjectService.setCCActionAvailable(e);
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
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;
}
EduStepicConnector.postCourse(project, course);
}
}
@@ -22,6 +22,7 @@ public class CourseInfo {
private String myType = "pycharm Python";
@SerializedName("instructors")
List<Instructor> myInstructors = new ArrayList<Instructor>();
int id;
public static CourseInfo INVALID_COURSE = new CourseInfo();
@@ -212,11 +212,11 @@ public class EduStepicConnector {
}
public static List<Lesson> getLessons(int sectionId) throws IOException {
final SectionWrapper sectionWrapper = getFromStepic("sections/" + String.valueOf(sectionId), SectionWrapper.class);
List<Integer> unitIds = sectionWrapper.sections.get(0).units;
final SectionContainer sectionContainer = getFromStepic("sections/" + String.valueOf(sectionId), SectionContainer.class);
List<Integer> unitIds = sectionContainer.sections.get(0).units;
final List<Lesson> lessons = new ArrayList<Lesson>();
for (Integer unitId : unitIds) {
UnitWrapper unit = getFromStepic("units/" + String.valueOf(unitId), UnitWrapper.class);
UnitContainer unit = getFromStepic("units/" + String.valueOf(unitId), UnitContainer.class);
int lessonID = unit.units.get(0).lesson;
LessonContainer lesson = getFromStepic("lessons/" + String.valueOf(lessonID), LessonContainer.class);
Lesson realLesson = lesson.lessons.get(0);
@@ -260,8 +260,93 @@ public class EduStepicConnector {
dialog.show();
}
public static void postCourse(Project project, @NotNull final Course course) {
final HttpPost request = new HttpPost(stepicApiUrl + "courses");
if (ourClient == null) {
showLoginDialog();
}
public static void postLesson(Project project, @NotNull final Lesson lesson) {
setHeaders(request, "application/json");
String requestBody = new Gson().toJson(new CourseWrapper(course));
request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
try {
final CloseableHttpResponse response = ourClient.execute(request);
final String responseString = EntityUtils.toString(response.getEntity());
final StatusLine line = response.getStatusLine();
if (line.getStatusCode() != 201) {
LOG.error("Failed to push " + responseString);
return;
}
final CourseInfo postedCourse = new Gson().fromJson(responseString, CoursesContainer.class).courses.get(0);
final int sectionId = postModule(postedCourse);
int position = 1;
for (Lesson lesson : course.getLessons()) {
final int lessonId = postLesson(project, lesson);
postUnit(lessonId, position, sectionId);
position += 1;
}
}
catch (IOException e) {
LOG.error(e.getMessage());
}
}
private static void postUnit(int lessonId, int position, int sectionId) {
final HttpPost request = new HttpPost(stepicApiUrl + "units");
setHeaders(request, "application/json");
final UnitWrapper unitWrapper = new UnitWrapper();
unitWrapper.unit = new Unit();
unitWrapper.unit.lesson = lessonId;
unitWrapper.unit.position = position;
unitWrapper.unit.section = sectionId;
String requestBody = new Gson().toJson(unitWrapper);
request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
try {
final CloseableHttpResponse response = ourClient.execute(request);
final String responseString = EntityUtils.toString(response.getEntity());
final StatusLine line = response.getStatusLine();
if (line.getStatusCode() != 201) {
LOG.error("Failed to push " + responseString);
}
}
catch (IOException e) {
LOG.error(e.getMessage());
}
}
private static int postModule(CourseInfo courseInfo) {
final HttpPost request = new HttpPost(stepicApiUrl + "sections");
setHeaders(request, "application/json");
final Section section = new Section();
section.course = courseInfo.id;
section.title = String.valueOf(courseInfo.getName());
section.position = 1;
final SectionWrapper sectionContainer = new SectionWrapper();
sectionContainer.section = section;
String requestBody = new Gson().toJson(sectionContainer);
request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
try {
final CloseableHttpResponse response = ourClient.execute(request);
final String responseString = EntityUtils.toString(response.getEntity());
final StatusLine line = response.getStatusLine();
if (line.getStatusCode() != 201) {
LOG.error("Failed to push " + responseString);
}
final Section postedSection = new Gson().fromJson(responseString, SectionContainer.class).sections.get(0);
return postedSection.id;
}
catch (IOException e) {
LOG.error(e.getMessage());
}
return -1;
}
public static int postLesson(Project project, @NotNull final Lesson lesson) {
final HttpPost request = new HttpPost(stepicApiUrl + "lessons");
if (ourClient == null) {
showLoginDialog();
@@ -277,16 +362,18 @@ public class EduStepicConnector {
final StatusLine line = response.getStatusLine();
if (line.getStatusCode() != 201) {
LOG.error("Failed to push " + responseString);
return;
return 0;
}
final Lesson postedLesson = new Gson().fromJson(responseString, Course.class).getLessons().get(0);
for (Task task : lesson.getTaskList()) {
postTask(project, task, postedLesson.id);
}
return postedLesson.id;
}
catch (IOException e) {
LOG.error(e.getMessage());
}
return -1;
}
public static void postTask(Project project, @NotNull final Task task, int id) {
@@ -381,6 +468,16 @@ public class EduStepicConnector {
}
}
static class CourseWrapper {
CourseInfo course;
public CourseWrapper(Course course) {
this.course = new CourseInfo();
this.course.setName(course.getName());
this.course.setDescription(course.getDescription());
}
}
static class LessonWrapper {
Lesson lesson;
@@ -416,27 +513,39 @@ public class EduStepicConnector {
}
}
static class SectionWrapper {
static class Section {
List<Integer> units;
}
static class Section {
List<Integer> units;
int course;
String title;
int position;
int id;
}
static class SectionWrapper {
Section section;
}
static class SectionContainer {
List<Section> sections;
List<Lesson> lessons;
static class Unit {
int id;
int lesson;
}
List<Unit> units;
}
static class Unit {
int id;
int section;
int lesson;
int position;
}
static class UnitContainer {
List<Unit> units;
}
static class UnitWrapper {
static class Unit {
int lesson;
}
List<Unit> units;
static class UnitWrapper{
Unit unit;
}
}