Skip to content

Commit

Permalink
Adjustments for 3.12.0-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Sep 20, 2024
1 parent fe8641e commit 390f094
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 15 deletions.
2 changes: 1 addition & 1 deletion fiduswriter/gitrepo_export/helpers/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
async def proxy(path, user, query_string, body, method):
if not any(regex.match(path) for regex in ALLOWED_PATHS):
raise Exception("Path not permitted.")
social_token = SocialToken.objects.get(
social_token = await SocialToken.objects.aget(
account__user=user, account__provider="github"
)
headers = get_headers(social_token.token)
Expand Down
2 changes: 1 addition & 1 deletion fiduswriter/gitrepo_export/helpers/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


async def proxy(path, user, query_string, body, method):
social_token = SocialToken.objects.get(
social_token = await SocialToken.objects.aget(
account__user=user, account__provider="gitlab"
)
headers = get_headers(social_token.token)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.15 on 2024-09-20 11:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("gitrepo_export", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="bookrepository",
name="export_docx",
field=models.BooleanField(default=False),
preserve_default=False,
),
migrations.AddField(
model_name="bookrepository",
name="export_odt",
field=models.BooleanField(default=False),
preserve_default=False,
),
]
2 changes: 2 additions & 0 deletions fiduswriter/gitrepo_export/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class BookRepository(models.Model):
export_html = models.BooleanField()
export_unified_html = models.BooleanField()
export_latex = models.BooleanField()
export_odt = models.BooleanField()
export_docx = models.BooleanField()

class Meta(object):
verbose_name_plural = "Book repositories"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,20 @@ export class GitrepoExporterBooksOverview {
const exportLatex = document.querySelector(
"#book-settings-repository-latex"
).checked
const exportDocx = document.querySelector(
"#book-settings-repository-docx"
).checked
const exportOdt = document.querySelector(
"#book-settings-repository-odt"
).checked
if (
!exportEpub &&
!exportUnpackedEpub &&
!exportHtml &&
!exportUnifiedHtml &&
!exportLatex
!exportLatex &&
!exportDocx &&
!exportOdt
) {
// No export formats selected. Reset repository.
repoId = 0
Expand All @@ -274,7 +282,10 @@ export class GitrepoExporterBooksOverview {
this.bookRepos[book.id].export_html !== exportHtml ||
this.bookRepos[book.id].export_unified_html !==
exportUnifiedHtml ||
this.bookRepos[book.id].export_latex !== exportLatex))
this.bookRepos[book.id].export_latex !== exportLatex ||
this.bookRepos[book.id].export_odt !== exportOdt ||
this.bookRepos[book.id].export_docx !== exportDocx
))
) {
const postData = {
book_id: book.id,
Expand All @@ -289,6 +300,8 @@ export class GitrepoExporterBooksOverview {
postData["export_html"] = exportHtml
postData["export_unified_html"] = exportUnifiedHtml
postData["export_latex"] = exportLatex
postData["export_odt"] = exportOdt
postData["export_docx"] = exportDocx
}
return post(
"/api/gitrepo_export/update_book_repo/",
Expand All @@ -306,7 +319,9 @@ export class GitrepoExporterBooksOverview {
export_unpacked_epub: exportUnpackedEpub,
export_html: exportHtml,
export_unified_html: exportUnifiedHtml,
export_latex: exportLatex
export_latex: exportLatex,
export_odt: exportOdt,
export_docx: exportDocx
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
SingleFileHTMLBookExporter
} from "../../books/exporter/html"
import {LatexBookExporter} from "../../books/exporter/latex"
import {ODTBookExporter} from "../../books/exporter/odt"
import {DOCXBookExporter} from "../../books/exporter/docx"
import {commitFile, commitZipContents} from "./tools"

export class EpubBookGithubExporter extends EpubBookExporter {
Expand Down Expand Up @@ -91,3 +93,33 @@ export class LatexBookGithubExporter extends LatexBookExporter {
)
}
}

export class DOCXBookGithubExporter extends DOCXBookExporter {
constructor(schema, csl, book, user, docList, updated, repo) {
super(schema, csl, book, user, docList, updated)
this.repo = repo
}

download(blob) {
return () =>
commitFile(this.repo, blob, "book.docx").then(response => [
response
])
}

}

export class ODTBookGithubExporter extends ODTBookExporter {
constructor(schema, csl, book, user, docList, updated, repo) {
super(schema, csl, book, user, docList, updated)
this.repo = repo
}

download(blob) {
return () =>
commitFile(this.repo, blob, "book.odt").then(response => [
response
])
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
UnpackedEpubBookGithubExporter,
HTMLBookGithubExporter,
LatexBookGithubExporter,
SingleFileHTMLBookGithubExporter
SingleFileHTMLBookGithubExporter,
DOCXBookGithubExporter,
ODTBookGithubExporter
} from "./book_exporters"
import {promiseChain, commitTree} from "./tools"

Expand Down Expand Up @@ -133,6 +135,32 @@ export class GithubBookProcessor {
)
commitInitiators.push(latexExporter.init())
}

if (this.bookRepo.export_docx) {
const docxExporter = new DOCXBookGithubExporter(
this.booksOverview.schema,
this.booksOverview.app.csl,
this.book,
this.booksOverview.user,
this.booksOverview.documentList,
new Date(this.book.updated * 1000),
this.userRepo
)
commitInitiators.push(docxExporter.init())
}

if (this.bookRepo.export_odt) {
const odtExporter = new ODTBookGithubExporter(
this.booksOverview.schema,
this.booksOverview.app.csl,
this.book,
this.booksOverview.user,
this.booksOverview.documentList,
new Date(this.book.updated * 1000),
this.userRepo
)
commitInitiators.push(odtExporter.init())
}
return Promise.all(commitInitiators).then(commitFunctions =>
promiseChain(commitFunctions.flat()).then(responses => {
const responseCodes = responses.flat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
HTMLBookExporter,
SingleFileHTMLBookExporter
} from "../../books/exporter/html"
import {ODTBookExporter} from "../../books/exporter/odt"
import {DOCXBookExporter} from "../../books/exporter/docx"
import {LatexBookExporter} from "../../books/exporter/latex"
import {zipToBlobs} from "./tools"

Expand Down Expand Up @@ -77,3 +79,31 @@ export class LatexBookGitlabExporter extends LatexBookExporter {
return zipToBlobs(this.textFiles, this.httpFiles, [], "latex/")
}
}

export class DOCXBookGitlabExporter extends DOCXBookExporter {
constructor(schema, csl, book, user, docList, updated, repo) {
super(schema, csl, book, user, docList, updated)
this.repo = repo
}

download(blob) {
return Promise.resolve({
"book.docx": blob
})
}

}

export class ODTBookGitlabExporter extends ODTBookExporter {
constructor(schema, csl, book, user, docList, updated, repo) {
super(schema, csl, book, user, docList, updated)
this.repo = repo
}

download(blob) {
return Promise.resolve({
"book.odt": blob
})
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
UnpackedEpubBookGitlabExporter,
HTMLBookGitlabExporter,
LatexBookGitlabExporter,
SingleFileHTMLBookGitlabExporter
SingleFileHTMLBookGitlabExporter,
DOCXBookGitlabExporter,
ODTBookGitlabExporter
} from "./book_exporters"
import {commitFiles} from "./tools"

Expand Down Expand Up @@ -133,6 +135,31 @@ export class GitlabBookProcessor {
)
fileGetters.push(latexExporter.init())
}
if (this.bookRepo.export_docx) {
const docxExporter = new DOCXBookGitlabExporter(
this.booksOverview.schema,
this.booksOverview.app.csl,
this.book,
this.booksOverview.user,
this.booksOverview.documentList,
new Date(this.book.updated * 1000),
this.userRepo
)
fileGetters.push(docxExporter.init())
}

if (this.bookRepo.export_odt) {
const odtExporter = new ODTBookGitlabExporter(
this.booksOverview.schema,
this.booksOverview.app.csl,
this.book,
this.booksOverview.user,
this.booksOverview.documentList,
new Date(this.book.updated * 1000),
this.userRepo
)
fileGetters.push(odtExporter.init())
}
return Promise.all(fileGetters)
.then(files =>
commitFiles(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,30 @@ export const repoSelectorTemplate = ({
<td>
<input type="checkbox" id="book-settings-repository-latex" ${
bookRepo && bookRepo.export_latex ? "checked" : ""
}>
</td>
</tr>
<tr>
<th>
<h4 class="fw-tablerow-title">${gettext("Export ODT")}</h4>
</th>
<td>
<input type="checkbox" id="book-settings-repository-odt" ${
bookRepo && bookRepo.export_odt && book.odt_template ? "checked" : ""
} ${
book.odt_template ? "" : "disabled"
}>
</td>
</tr>
<tr>
<th>
<h4 class="fw-tablerow-title">${gettext("Export DOCX")}</h4>
</th>
<td>
<input type="checkbox" id="book-settings-repository-docx" ${
bookRepo && bookRepo.export_docx && book.docx_template ? "checked" : ""
} ${
book.docx_template ? "" : "disabled"
}>
</td>
</tr>`
Expand Down
18 changes: 10 additions & 8 deletions fiduswriter/gitrepo_export/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def update_book_repo(request):
export_html=request.POST["export_html"] == "true",
export_unified_html=request.POST["export_unified_html"] == "true",
export_latex=request.POST["export_latex"] == "true",
export_docx=request.POST["export_docx"] == "true",
export_odt=request.POST["export_odt"] == "true",
)
status = 201
return HttpResponse(status=status)
Expand All @@ -87,20 +89,20 @@ def update_book_repo(request):
@async_to_sync
async def get_git_repos(request, reload=False):
social_tokens = {
"github": SocialToken.objects.filter(
"github": await SocialToken.objects.filter(
account__user=request.user, account__provider="github"
).first(),
"gitlab": SocialToken.objects.filter(
).afirst(),
"gitlab": await SocialToken.objects.filter(
account__user=request.user, account__provider="gitlab"
).first(),
).afirst(),
}

if not social_tokens["github"] and not social_tokens["gitlab"]:
return HttpResponseForbidden()
repo_info = models.RepoInfo.objects.filter(user=request.user).first()
repo_info = await models.RepoInfo.objects.filter(user=request.user).afirst()
if repo_info:
if reload:
repo_info.delete()
await repo_info.adelete()
else:
return JsonResponse({"repos": repo_info.content}, status=200)
repos = []
Expand All @@ -119,11 +121,11 @@ async def get_git_repos(request, reload=False):
return []
except Exception as e:
return HttpResponse("Error: %s" % e, status=500)
repo_info, created = models.RepoInfo.objects.get_or_create(
repo_info, created = await models.RepoInfo.objects.aget_or_create(
user=request.user
)
repo_info.content = repos
repo_info.save()
await repo_info.asave()
return JsonResponse({"repos": repos}, status=200)


Expand Down

0 comments on commit 390f094

Please sign in to comment.