Permalink
Browse files

Timeline

  • Loading branch information...
danieleguido committed Mar 7, 2013
1 parent 9b237eb commit 5f3a382c4e8a1a0119b4078b6ddf3ff8efa49df7
View
@@ -6,3 +6,5 @@ local_settings.py
*.db
.DS_Store
+
+static/admin
View
@@ -9,8 +9,8 @@
from django.template.defaultfilters import slugify
from glue import Epoxy, API_EXCEPTION_FORMERRORS, API_EXCEPTION_INTEGRITY, API_EXCEPTION_DOESNOTEXIST, API_EXCEPTION_OSERROR
-from glue.models import Page, Pin, Tag
-from glue.forms import AddPageForm, AddPinForm, EditPinForm, UploadPinForm
+from glue.models import Page, Pin, Tag, Serie, Frame
+from glue.forms import AddPageForm, AddPinForm, EditPinForm, UploadPinForm, AddSerieForm
logger = logging.getLogger(__name__)
@@ -62,6 +62,58 @@ def page( request, page_id ):
def page_by_slug( request, page_slug, page_language ):
return Epoxy( request ).single( Page, {'slug':page_slug,'language':page_language} ).json()
+
+@login_required
+def series( request ):
+ response = Epoxy( request )
+
+ if response.method =='POST':
+ form = AddSerieForm( request.REQUEST )
+ if not form.is_valid():
+ return response.throw_error( error=form.errors, code=API_EXCEPTION_FORMERRORS).json()
+
+
+ for language,l in settings.LANGUAGES:
+ try:
+ serie = Serie( title=form.cleaned_data[ 'title_%s' % language ], language=language, slug=form.cleaned_data[ 'slug' ], type=form.cleaned_data[ 'type' ] )
+ serie.save()
+ serie.authors.add( request.user )
+
+ except IntegrityError, e:
+ return response.throw_error( error="%s" % e, code=API_EXCEPTION_INTEGRITY).json()
+
+ response.add( 'object', serie, jsonify=True )
+ return response.queryset( Serie.objects.filter( authors=request.user ) ).json()
+
+@login_required
+def serie( request, serie_id ):
+ return Epoxy( request ).single( Serie, {'id':serie_id} ).json()
+
+@login_required
+def serie_frames( request, serie_id ):
+ response = Epoxy( request )
+
+ try:
+ serie = Serie.objects.get( id=serie_id )
+ except Serie.DoesNotExist, e:
+ return response.throw_error( error="%s" % e, code=API_EXCEPTION_DOESNOTEXIST ).json()
+
+
+ response.add( 'object', serie.json(load_frames=True))
+ return response.json()
+
+@login_required
+def frames( request, serie_id ):
+ response = Epoxy( request )
+ # add a dictonary of frame : sort
+ return response.queryset( Frame.objects.filter( serie__authors=request.user ) ).json()
+
+
+
+@login_required
+def frame( request, frame_id ):
+ return Epoxy( request ).single( Frame, {'id':frame_id,'serie__authors':request.user } ).json()
+
@login_required
def pins( request ):
response = Epoxy( request )
View
@@ -2,7 +2,7 @@
from django.forms import ModelForm
from django.utils.translation import ugettext as _
from django.conf import settings
-from glue.models import Tag
+from glue.models import Tag, Serie
class LoginForm(forms.Form):
username = forms.CharField( label=_('login'), max_length=64 )
@@ -20,6 +20,18 @@ def __init__(self, *args, **kwargs):
slug = forms.SlugField( required=True )
+class AddSerieForm(forms.Form):
+ def __init__(self, *args, **kwargs):
+
+ super(AddSerieForm, self).__init__(*args, **kwargs)
+
+ for l,lang in settings.LANGUAGES:
+ self.fields['title_%s' % l] = forms.CharField( label=_("%s title" % lang), required=True )
+
+ slug = forms.SlugField( required=True )
+
+ type = forms.ChoiceField( label=_("type"), required=True, choices=Serie.TYPE_CHOICES )
+
class AddPinForm(forms.Form):
View
@@ -172,6 +172,18 @@ class Frame( models.Model ):
abstract = models.TextField( default="", blank=True, null=True ) # a description of the passage
+ def json( self ):
+ return{
+ 'id': self.id,
+ 'serie':self.serie.id,
+ 'duration': self.duration,
+ 'sort': self.sort,
+ 'abstract': self.abstract,
+ 'pin': self.pin.json(),
+ 'role': self.role,
+ 'type': self.type
+ }
+
class Meta:
ordering = ( 'sort', )
@@ -203,3 +215,16 @@ class Serie( PageAbstract ):
watchers = models.ManyToManyField( User, blank=True, null=True, related_name="serie_watched" ) # User.serie_watched
tags = models.ManyToManyField( Tag, blank=True, null=True ) # add tags !
+
+ def json( self, load_frames=False ):
+ # when load frames is True return the list. otherwise null
+ return{
+ 'id': self.id,
+ 'slug':self.slug,
+ 'title': self.title,
+ 'abstract': self.abstract,
+ 'content': self.content,
+ 'language': self.language,
+ 'type': self.type,
+ 'frames':[ f.json() for f in self.frames.all() ] if load_frames else None
+ }
View
@@ -23,4 +23,11 @@
url(r'^pin/(?P<pin_id>\d+)/clean/$', 'glue.api.pin_clean', name='glue_api_pin_clean'),
url(r'^pin/upload/$', 'glue.api.pin_upload', name='glue_api_pin_upload'),
+
+ url(r'^serie/$', 'glue.api.series', name='glue_api_series'), # get list, post single serie
+ url(r'^serie/(?P<serie_id>\d+)/$', 'glue.api.serie', name='glue_api_serie'), # get a signle serie WITHOUT its frame collection. POST to edit serie Data
+ url(r'^serie/(?P<serie_id>\d+)/frames/$', 'glue.api.serie_frames', name='glue_api_serie_frames'), # get a signle serie along wiht its frame collection. POST to add frame...
+
+ url(r'^frame/$', 'glue.api.frames', name='glue_api_frames'), # get list of frames per author (useless)
+ url(r'^frame/(?P<frame_id>\d+)/$', 'glue.api.frame', name='glue_api_frame'), # GET: full information
)
View
@@ -1,3 +1,8 @@
+/**
+ oo
+ Serie handler
+*/
+
var oo = oo || {}; oo.rice = {};
/*
@@ -8,9 +13,26 @@ var oo = oo || {}; oo.rice = {};
*/
oo.magic = oo.magic || {};
+oo.magic.serie = {}
+oo.magic.serie.add = function(){}
+/*
+ Api
+ ===
+
+*/
+oo.api.serie = {};
+oo.api.serie.add = function( params ){ oo.log( '[oo.api.serie.add]', params );
+ $.ajax( $.extend( oo.api.settings.post,{ url: oo.urls.serie.add,
+ data: params,
+ success:function(result){ oo.log( "[oo.api.pin.add] result:", result );
+ oo.api.process( result, oo.magic.serie.add, "id_add_serie" );
+ }
+ }));
+};
+
/*
@@ -24,32 +46,30 @@ oo.rice.dragstart = function( event ){
$("#actual-serie").height( 40 );
}
-oo.rice.append = function( event ){
- if (typeof oo.rice.serie == "undefined" ){
- // default serie
- oo.rice.serie = new oo.rice.Serie();
- }
- oo.trigger( oo.rice.events.append, {what:'me'});
+oo.rice.add_frame = function( event ){
+ var frame = $(this);
+ oo.trigger( oo.rice.events.add_frame, new oo.rice.Frame({ id: frame.attr('data-id'), mimetype: frame.attr('data-mimetype'), slug: frame.attr('data-slug') }) );
}
oo.rice.init = function(){oo.log("[oo.rice.init]");
// load last serie from cookie
oo.rice.serie = new oo.rice.Serie({ autoload:true });
- $(document).on('click','.drag-to-serie',oo.rice.append );
-
+ // start global listeners
+ $(document).on('click','.drag-to-serie', oo.rice.add_frame );
}
oo.rice.events = {
- 'append':'oo.rice.events.add',
+ 'add_frame':'oo.rice.events.add_frame',
'change':'oo.rice.events.change',
'clean':'oo.rice.events.clean',
'init':'oo.rice.events.init',
'replace':'oo.rice.events.replace',
'remove':'oo.rice.events.remove',
- 'reset':'oo.rice.events.reset'
+ 'reset':'oo.rice.events.reset',
+ 'inscribe':'oo.rice.events.inscribe'
};
/*
@@ -61,24 +81,152 @@ oo.rice.events = {
*/
oo.rice.Serie = function( options ){
+ var serie = this
this.settings = $.extend({
- autoload: false
+ autoload: false,
+ target_selector: '#actual-serie',
+ source_selector: '.draggable',
+ frames:[],
+ slug:'serie-slug'
+
}, options );
- this.append = function( event, data ){
- oo.log( event, data );
+ /*
+ Expose publicly serie frames
+ */
+ this.get_frames = function(){
+ return this.settings.frames;
+ }
+
+ /*
+ get current hash (md5) git like
+ */
+ this.get_hash = function(){
+ return md5( JSON.stringify( this.settings ) )
+ }
+
+
+ this.html = function(){
+
+ }
+
+
+ this.sync = function(){
+
+
+ }
+
+ /*
+ @param frames -
+ */
+ this.set_frames = function( frames ){
+ oo.log( "[serie] set_frames", this.settings );
+ // compare version.
+
+ this.settings.frames = []
+
+ for( f in frames ){
+ this.settings.frames.push( oo.rice.Frame( frames[f] ) );
+ }
+ }
+
+ this.add_frame = function( event, frame ){
+ // add and send
+ oo.log( "[serie] set_frames", frame );
+ serie.settings.frames.push( frame );
+ serie.sync();
};
this.init = function(){
- // if options.autoload:
+ oo.log( "[serie] init", this.settings );
+
+ // intitialize history
+ this.history = new oo.rice.History()
+
+ // initialize sortable
+ $( this.settings.target_selector ).sortable({
+ revert: true,
+ update: function(event, ui) {
+ if ($(ui.item).hasClass('draggable')) {
+ $(ui.item).css({color:'red'});
+ }
+ }
+ });
+
+ $( this.settings.source_selector ).draggable({
+ connectToSortable: this.settings.target_selector,
+ helper: 'clone',
+ revert: 'invalid'
+ });
+
+ $('ul, li').disableSelection();
- oo.log( "[oo.rice.Serie] Serie object created", this.settings );
// enable listeners
+ oo.on( oo.rice.events.add_frame, this.add_frame );
+
- oo.on( oo.rice.events.append, this.append );
+
}
+ this.init();
+}
+
+
+/*
+
+
+ Frame Class
+ ===========
+
+*/
+oo.rice.Frame = function( options ){
+
+ this.settings = $.extend({
+ id:0,
+ mimetype: 'text/plain',
+ slug: '', // frame object as it comes from api/frame
+ }, options );
+
+ this.props = function(){
+ return this.settings
+ }
+
+ this.html = function(){
+ return $("<li/>",{ 'data-slug': this.settings.slug, 'class':'frame ' + this.settings.mimetype }).text(
+ "#" + this.settings.id
+ );
+ }
+};
+
+
+oo.rice.History = function( options ){
+
+ this.buffer = [];
+ this.cursor = 0;
+
+ this.settings = $.extend({
+ onInscribeEvent: oo.rice.events.inscribe
+ }, options );
+
+ this.inscribe = function( eventType, data ){
+ oo.log( "[new oo.rice.History] inscribe", eventType, data);
+ this.buffer.push({
+ hash: md5( data ),
+ type: eventType,
+ data:data
+ });
+ this.cursor = this.buffer.length - 1;
+ }
+
+ this.back = function(){
+
+ }
+
+ this.init = function(){
+ oo.log( "[new oo.rice.History] init");
+ oo.on( oo.rice.events.inscribe, this.inscribe );
+ }
this.init();
}

Large diffs are not rendered by default.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 5f3a382

Please sign in to comment.