Anonymous avatar Anonymous committed aa7a242

New way of fork to allow changes and possibility to add an edit_code

Comments (0)

Files changed (3)

friendpaste/models.py

         return self
        
     
-    def fork(cls, db, pasteid):
-        """ fork a paste """
+    def fork(cls, db, pasteid, new_code=None):
+        """ simple fork of a paste """
+        if new_code is None:
+            new_code = ''
+
         paste = cls.get_paste(db, pasteid)
         if paste is not None: # create new paste from old
             new_paste = cls(
                 language = paste.language,
                 fork = True,
                 fork_parent = paste.pasteid,
-                forked_atrevision = paste.nb_revision
+                forked_atrevision = paste.nb_revision,
+                edit_code = new_code
             )
             new_paste.store(local.db)
            

friendpaste/views.py

 from friendpaste import settings
 from friendpaste.feeds import RssFeed, RssFeedEntry 
 from friendpaste.http import FPResponse, send_json
-from friendpaste.models import Paste
+from friendpaste.models import Paste, Fork
 from friendpaste.template import render_response, highlighter, pretty_type, shighlight
 from friendpaste.utils import local, local_manager, datetimestr_topython, strptime, make_hash
 
     return render_response('paste/edit.html', form=form, snippet=s)
 
 def fork_snippet(request, id):
-    s = Paste.fork(local.db, id)
+    s = Paste.get_paste(local.db, id)
     if not s:
         raise NotFound
+    form = PasteForm(request.form, prefix='paste', **{
+        'title': s.title,
+        'snippet': s.content,
+        'language': str(s.language)
+    })
+    if request.method == 'POST' and form.validate():
+        code = form.data['code']
+        if code:
+            code = make_hash(code)
 
-    return redirect ('/%s' % s.pasteid)
+        s.forked = True
+        s.store(local.db)
 
+        new_paste = Paste(
+                title=form.data['title'],
+                content=form.data['snippet'],
+                language=form.data['language'],
+                edit_code= code,
+                fork = True,
+                fork_parent = s.pasteid,
+                forked_atrevision = s.nb_revision
+        )
+        new_paste.store(local.db)
+
+        # save fork infos
+        new_fork = Fork(
+                fork_parent = s.pasteid,
+                forked_atrevision = s.nb_revision,
+                fork_id = new_paste.pasteid
+        )
+        new_fork.store(local.db)
+        return redirect('/%s' % new_paste.pasteid)
+
+    return render_response('paste/fork.html', form=form, snippet=s)
 
 def view_snippet(request, id):
     mimetypes = request.accept_mimetypes

templates/paste/fork.html

+{% extends "base.html" %}
+
+{% block title %}Fork #{{ snippet.pasteid }}{% endblock %}
+
+{% block content %}
+<section id="new_snippet">
+<form id="snippet-edit" name="fpaste" action="{{ url_for('paste/fork',
+    id=snippet.pasteid) }}" method="post">
+        <h2>Fork paste #<a href="{{ url_for('paste/view',
+                id=snippet.pasteid) }}">{{ snippet.pasteid }}</a></h2>        
+        {% if form.errors %}
+        <h2 class="errors">Something was wrong!</h2>
+        <ul class="errors">
+            {% if form.snippet.errors %}
+            <li><strong>Paste text:</strong><ul>{% for e in form.snippet.errors %}<li>{{ e }}</li>{% endfor %}</ul></li>
+            {% endif %}
+        </ul>
+        {% endif %}
+        <ol>
+            <li{% if form.snippet.errors %}
+     class="errors"{% endif %}>{{ form.snippet(cols=70,rows=10,class="resizable") }}</li>
+            <li>{{ form.language.label }}{{ form.language }}</li>
+            <li>{{ form.title.label }}{{ form.title }}</li>
+            <li>{{ form.code.label }}{{ form.code(autocomplete="off")
+                }}</li>
+            <li class="ccode"><input type="submit" name="psubmit"
+            value="Submit paste" /></li>
+        </ol>
+        <p class="tip">Removal/Lock code is a password to let you
+        <strong>remove</strong> or 
+        <strong>lock edit</strong> of paste you upload.</p>
+    </form>
+
+</section>
+
+{% endblock %}
+
+{% block script %}
+{% if DEBUG %}
+<script type="text/javascript" src="/static/js/src/resizeable.js"></script>
+<script type="text/javascript" src="/static/js/src/friendpaste.js"></script>
+{% else %}
+<script type="text/javascript" src="/static/js/friendpaste.js?20081115"></script>
+{% endif %}
+
+<script type="text/javascript">
+    if(!base2.detect("webkit"))
+            new Resizeable("#paste_snippet");
+    document.querySelector("#paste_snippet").focus();
+</script>
+{% endblock %}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.