liquid_feedback_frontend

annotate app/main/draft/diff.lua @ 1235:7f818548b7b8

Added push grace period for verification phase to lf4rcs
author bsw
date Fri Dec 04 14:25:43 2015 +0100 (2015-12-04)
parents 02772bc49467
children 32cc544d5a5b
rev   line source
bsw@2 1 local old_draft_id = param.get("old_draft_id", atom.integer)
bsw@2 2 local new_draft_id = param.get("new_draft_id", atom.integer)
bsw@1045 3 local initiative_id = param.get("initiative_id", atom.number)
bsw@2 4
bsw@1045 5 if not old_draft_id
bsw@1045 6 or not new_draft_id
bsw@1045 7 or old_draft_id == new_draft_id
bsw@1045 8 then
bsw@1045 9 slot.reset_all()
bsw@1045 10 slot.select("error", function()
bsw@1045 11 ui.tag{ content = _"Please choose two different versions of the draft to compare" }
bsw@1045 12 end )
bsw@1045 13 request.redirect{
bsw@1045 14 module = "draft", view = "list", params = {
bsw@1045 15 initiative_id = initiative_id
bsw@1045 16 }
bsw@1045 17 }
bsw@10 18 return
bsw@10 19 end
bsw@10 20
bsw@2 21 if old_draft_id > new_draft_id then
jbe@1226 22 old_draft_id, new_draft_id = new_draft_id, old_draft_id
bsw@2 23 end
bsw@2 24
bsw@2 25 local old_draft = Draft:by_id(old_draft_id)
bsw@2 26 local new_draft = Draft:by_id(new_draft_id)
bsw@2 27
bsw@1045 28 local initiative = new_draft.initiative
bsw@1045 29
bsw@1045 30 if app.session.member then
bsw@1045 31 initiative:load_everything_for_member_id(app.session.member_id)
bsw@1045 32 initiative.issue:load_everything_for_member_id(app.session.member_id)
bsw@1045 33 end
bsw@1045 34
bsw@1045 35
bsw@1045 36 execute.view{ module = "issue", view = "_sidebar_state", params = {
bsw@1045 37 initiative = initiative
bsw@1045 38 } }
bsw@1045 39
bsw@1045 40 execute.view {
bsw@1045 41 module = "issue", view = "_sidebar_issue",
bsw@1045 42 params = {
bsw@1045 43 issue = initiative.issue,
bsw@1045 44 highlight_initiative_id = initiative.id
bsw@1045 45 }
poelzi@160 46 }
poelzi@159 47
bsw@1045 48 execute.view {
bsw@1045 49 module = "issue", view = "_sidebar_whatcanido",
bsw@1045 50 params = { initiative = initiative }
bsw@1045 51 }
poelzi@159 52
bsw@1045 53 execute.view {
bsw@1045 54 module = "issue", view = "_sidebar_members", params = {
bsw@1045 55 issue = initiative.issue, initiative = initiative
bsw@1045 56 }
bsw@1045 57 }
bsw@1045 58
bsw@1045 59
bsw@1045 60
bsw@1045 61 execute.view {
bsw@1045 62 module = "issue", view = "_head", params = {
bsw@1045 63 issue = initiative.issue
bsw@1045 64 }
bsw@1045 65 }
bsw@1045 66
bsw@1045 67
poelzi@159 68
bsw@95 69 local old_draft_content = string.gsub(string.gsub(old_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
bsw@95 70 local new_draft_content = string.gsub(string.gsub(new_draft.content, "\n", " ###ENTER###\n"), " ", "\n")
bsw@95 71
jbe@1226 72 local key = multirand.string(24, "0123456789abcdefghijklmnopqrstuvwxyz")
bsw@2 73
bsw@1230 74 local old_draft_filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "diff-" .. key .. "-old.tmp")
bsw@1230 75 local new_draft_filename = encode.file_path(WEBMCP_BASE_PATH, 'tmp', "diff-" .. key .. "-new.tmp")
bsw@2 76
bsw@2 77 local old_draft_file = assert(io.open(old_draft_filename, "w"))
bsw@95 78 old_draft_file:write(old_draft_content)
bsw@2 79 old_draft_file:write("\n")
bsw@2 80 old_draft_file:close()
bsw@2 81
bsw@2 82 local new_draft_file = assert(io.open(new_draft_filename, "w"))
bsw@95 83 new_draft_file:write(new_draft_content)
bsw@2 84 new_draft_file:write("\n")
bsw@2 85 new_draft_file:close()
bsw@2 86
bsw@728 87 local output, err, status = extos.pfilter(nil, "sh", "-c", "diff -U 1000000000 '" .. old_draft_filename .. "' '" .. new_draft_filename .. "' | grep -v ^--- | grep -v ^+++ | grep -v ^@")
bsw@2 88
bsw@2 89 os.remove(old_draft_filename)
bsw@2 90 os.remove(new_draft_filename)
bsw@2 91
bsw@95 92 local last_state = "first_run"
bsw@95 93
bsw@95 94 local function process_line(line)
bsw@95 95 local state_char = string.sub(line, 1, 1)
bsw@95 96 local state
bsw@95 97 if state_char == "+" then
bsw@95 98 state = "added"
bsw@95 99 elseif state_char == "-" then
bsw@95 100 state = "removed"
bsw@95 101 elseif state_char == " " then
bsw@95 102 state = "unchanged"
bsw@95 103 end
bsw@95 104 local state_changed = false
bsw@95 105 if state ~= last_state then
bsw@95 106 if last_state ~= "first_run" then
bsw@95 107 slot.put("</span> ")
bsw@95 108 end
bsw@95 109 last_state = state
bsw@95 110 state_changed = true
bsw@95 111 slot.put("<span class=\"diff_" .. tostring(state) .. "\">")
bsw@95 112 end
bsw@95 113
bsw@95 114 line = string.sub(line, 2, #line)
bsw@95 115 if line ~= "###ENTER###" then
bsw@95 116 if not state_changed then
bsw@95 117 slot.put(" ")
bsw@95 118 end
bsw@953 119 slot.put(encode.html(line))
bsw@95 120 else
bsw@95 121 slot.put("<br />")
bsw@95 122 end
bsw@95 123 end
bsw@95 124
bsw@1045 125 ui.section( function()
bsw@1045 126 ui.sectionHead( function()
bsw@1045 127 ui.link{
bsw@1045 128 module = "initiative", view = "show", id = initiative.id,
bsw@1045 129 content = function ()
bsw@1045 130 ui.heading {
bsw@1045 131 level = 1,
bsw@1045 132 content = initiative.display_name
bsw@1045 133 }
bsw@1045 134 end
bsw@1045 135 }
bsw@1045 136 ui.heading{ level = 2, content = _("Comparision of revisions #{id1} and #{id2}", {
bsw@1045 137 id1 = old_draft.id,
bsw@1045 138 id2 = new_draft.id
bsw@1045 139 } ) }
bsw@1045 140 end )
bsw@1045 141
bsw@1045 142 if app.session.member_id and not new_draft.initiative.revoked then
bsw@1045 143 local supporter = app.session.member:get_reference_selector("supporters")
bsw@1045 144 :add_where{ "initiative_id = ?", new_draft.initiative_id }
bsw@1045 145 :optional_object_mode()
bsw@1045 146 :exec()
bsw@1045 147 if supporter and supporter.draft_id ~= new_draft.id then
bsw@1045 148 ui.sectionRow("draft_updated_info", function()
bsw@1045 149 ui.container{
bsw@1045 150 attr = { class = "info" },
bsw@1045 151 content = _"The draft of this initiative has been updated!"
bsw@1045 152 }
bsw@1045 153 slot.put(" ")
bsw@1045 154 ui.link{
bsw@1045 155 text = _"refresh my support",
bsw@1045 156 module = "initiative",
bsw@1045 157 action = "add_support",
bsw@1045 158 id = new_draft.initiative.id,
bsw@1045 159 params = { draft_id = new_draft.id },
bsw@1045 160 routing = {
bsw@1045 161 default = {
bsw@1045 162 mode = "redirect",
bsw@1045 163 module = "initiative",
bsw@1045 164 view = "show",
bsw@1045 165 id = new_draft.initiative.id
bsw@1045 166 }
bsw@1045 167 }
bsw@1045 168 }
bsw@1045 169
bsw@1045 170 slot.put(" &middot; ")
bsw@1045 171
bsw@1045 172 ui.link{
bsw@1045 173 text = _"remove my support",
bsw@1045 174 module = "initiative",
bsw@1045 175 action = "remove_support",
bsw@1045 176 id = new_draft.initiative.id,
bsw@1045 177 routing = {
bsw@1045 178 default = {
bsw@1045 179 mode = "redirect",
bsw@1045 180 module = "initiative",
bsw@1045 181 view = "show",
bsw@1045 182 id = new_draft.initiative.id
bsw@1045 183 }
bsw@1045 184 }
bsw@1045 185 }
bsw@1045 186
bsw@1045 187 end )
bsw@2 188 end
bsw@1045 189 end
bsw@1045 190
bsw@1045 191 ui.sectionRow( function()
bsw@2 192
bsw@1045 193 if not status then
bsw@1045 194 ui.field.text{ value = _"The drafts do not differ" }
bsw@1045 195 else
bsw@1045 196 ui.container{
bsw@1045 197 tag = "div",
bsw@1045 198 attr = { class = "diff" },
bsw@1045 199 content = function()
bsw@1045 200 output = output:gsub("[^\n\r]+", function(line)
bsw@1045 201 process_line(line)
bsw@1045 202 end)
bsw@1045 203 end
bsw@1045 204 }
bsw@1045 205 end
bsw@1045 206
bsw@1045 207 end )
jbe@1226 208 end )

Impressum / About Us