Commits

Adam Gomaa committed c330ad3

Add support for timestamped images, too.

Comments (0)

Files changed (4)

 
 
-class Note(object):
+
+def Note(fname, directory):
+    from os.path import splitext
+    ext = splitext(fname)[1]
+    return {".txt": TextNote,
+            ".png": ImageNote}[ext](fname, directory)
+
+
+
+
+class BaseNote(object):
     "A file in my notes directory"
 
     def __init__(self, fname, directory):
         self.fname = fname
         self.directory = directory
 
+    def timestamp(self):
+        from os.path import splitext
+        return splitext(self.fname)[0]
+
+    def fd(self, mode='r'):
+        from os.path import join
+        return open(join(self.directory, self.fname), mode=mode)
+
+
+
+
+class TextNote(BaseNote):
     def model_data(self):
         "The data for this Note that should be supplied to the Backbone model"
         return {
             "html": self.html(),
             }
 
-    def timestamp(self):
-        from os.path import splitext
-        return splitext(self.fname)[0]
-
     def text(self):
         return self.fd().read()
 
     def save_text(self, text):
         self.fd('w').write(text)
 
+
+class ImageNote(BaseNote):
+    def model_data(self):
+        "The data for this Note that should be supplied to the Backbone model"
+        return {
+            "id": self.fname,
+            "timestamp": self.timestamp(),
+            }
+
+    def GET(self):
+        from django.http import HttpResponse
+        return HttpResponse(self.fd().read(), content_type="image/png")
+

notedir/notes.html

         <textarea style="width: 100%;" name="text"><%= text %></textarea>
       </div>
     </div>
+    <div id="note-image-template" style="display: none;">
+      <div class="note" data-note-timestamp="<%- timestamp %>" >
+        <h3><%- timestamp %></h3>
+        <img style="width: 100%;">
+      </div>
+    </div>
     <div id="notes-view">
       <div class="notes-pagination"></div>
       <div class="notes-container"></div>
     }
 });
 
-var NoteCol = PaginatedCollection.extend({model: Note, baseUrl: '/notes/'});
+    var NoteCol = PaginatedCollection.extend({model: Note, baseUrl: '/notes/', perPage: 10});
 
 PaginatedView = Backbone.View.extend({
   initialize: function() {
     }
 });
 
+
     var EditableNoteView = Backbone.View.extend({
         template: get_template("#note-edit-template"),
         events: {
         }
     });
 
+var ImageNoteView = Backbone.View.extend({
+    tagName: "div",
+    template: get_template("#note-image-template"),
+    render: function(){
+        $(this.el).html(this.template(this.template_context()));
+        this.$("img").attr("src", this.model.url());
+        return this;
+    },
+    template_context: function(){
+        return this.model.attributes;
+    }
+});
+
+
+
 var AppView = PaginatedView.extend({
     el: $("#notes-view"),
     initialize: function(options){
         this.collection.fetch();
     },
     addOne: function(note){
-        var view = new NoteView({model: note});
+        if(note.get("text")){
+            var view = new NoteView({model: note});
+        }else{
+            var view = new ImageNoteView({model: note});
+        }
         this.$(".notes-container").append(view.render().el);
     },
     addAll: function(){
         return fs_file("notes.css", content_type="text/css")
     if "col" in request.GET:
         return notes_data()
+    elif path.startswith("note/"):
+        return _Note(path[len("note/"):]).GET()
 
     return fs_file("notes.html")