Пример #1
0
def build_one_bookmark(username, url, default_vote=0):
    N = getNote(username, 'bookmarkbook')

    n, created = N.objects.get_or_create(url=url[0])
    if created:
        if url[1] == None:
            n.title = ""
        else:
            n.title = url[1]
        if url[2]:
            a = url[2]
            #Google bookmark file uses 16 digits for the datetime. We only need 10 digits.
            if len(a) == 16:
                a = a[:-6]
            #TODO: catch possible exceptions
            n.init_date = datetime.fromtimestamp(int(a))
        else:
            n.init_date = datetime.now()
        if url[3]:
            a = url[3]
            if len(a) == 16:
                a = a[:-6]
            n.last_modi_date = datetime.fromtimestamp(int(a))
        else:
            n.last_modi_date = datetime.now()

        n.vote = default_vote
        n.save()
        print 'A bookmark is saved:', n
    return n, created
Пример #2
0
def build_one_bookmark(username, url, default_vote=0):
    N = getNote(username, 'bookmarkbook')
    
    n, created = N.objects.get_or_create(url = url[0])
    if created:
        if url[1]==None:
            n.title = ""
        else:    
            n.title   = url[1]
        if url[2]:
            a = url[2]
            #Google bookmark file uses 16 digits for the datetime. We only need 10 digits.
            if len(a)==16:
                a = a[:-6]                        
            #TODO: catch possible exceptions
            n.init_date = datetime.fromtimestamp(int(a))            
        else:
            n.init_date = datetime.now()
        if url[3]:             
            a = url[3]
            if len(a)==16:
                a = a[:-6]              
            n.last_modi_date = datetime.fromtimestamp(int(a))
        else:   
            n.last_modi_date = datetime.now()

        n.vote = default_vote
        n.save()
        print 'A bookmark is saved:', n
    return n, created
Пример #3
0
def sync_last_ndays(username, ndays):
    N = getNote(username, 'notebook') 
    now = date.today()
    ndays_ago = now - datetime.timedelta(days=ndays)    
    note_list = N.objects.filter(init_date__gte=ndays_ago.strftime('%Y-%m-%d'),  init_date__lte=now.strftime('%Y-%m-%d 23:59:59'))
    print 'Sync '+str(len(note_list))+'notes...'
    for n in note_list:        
        n.save()
    print 'Sync finished for this user!'
Пример #4
0
def sync_last_ndays(username, ndays):
    N = getNote(username, 'notebook')
    now = date.today()
    ndays_ago = now - datetime.timedelta(days=ndays)
    note_list = N.objects.filter(
        init_date__gte=ndays_ago.strftime('%Y-%m-%d'),
        init_date__lte=now.strftime('%Y-%m-%d 23:59:59'))
    print 'Sync ' + str(len(note_list)) + 'notes...'
    for n in note_list:
        n.save()
    print 'Sync finished for this user!'
Пример #5
0
def init_social_db_scrap(username):
    user = User.objects.get(username=username)

    N = getNote(username, 'scrapbook')       
    ns = N.objects.all()     
    for n in ns:
        print 'scrap is:', n
        if not n.private and not n.deleted:
            sts = []
            for t in n.tags.all():
                if not t.private:  
                    st, created = Social_Tag.objects.get_or_create(name=t.name)
                    sts.append(st)
            s, created = Social_Scrap.objects.get_or_create(owner=user, url=n.url, owner_note_id=n.id, desc=n.desc, title=n.title,\
init_date=n.init_date, last_modi_date=n.last_modi_date, vote=n.vote) #attachment
            if created:
                for t in sts:
                    s.tags.add(t)
                s.save()       
Пример #6
0
def init_social_db_scrap(username):
    user = User.objects.get(username=username)

    N = getNote(username, 'scrapbook')
    ns = N.objects.all()
    for n in ns:
        print 'scrap is:', n
        if not n.private and not n.deleted:
            sts = []
            for t in n.tags.all():
                if not t.private:
                    st, created = Social_Tag.objects.get_or_create(name=t.name)
                    sts.append(st)
            s, created = Social_Scrap.objects.get_or_create(owner=user, url=n.url, owner_note_id=n.id, desc=n.desc, title=n.title,\
init_date=n.init_date, last_modi_date=n.last_modi_date, vote=n.vote) #attachment
            if created:
                for t in sts:
                    s.tags.add(t)
                s.save()
Пример #7
0
def share(request, username):   
    print 'share in note called'   
    note_ids = request.POST.getlist('note_ids')   
    N = getNote(request.user.username)   
    msgs = [] 
    for note_id in note_ids:         
        note = N.objects.get(id=note_id)  
        message = 'From osl scraps:'+' '+note.title+' '+note.url+'  '
        desc = note.desc
        desc = desc.replace('\r','')
        desc = desc.replace('\n','')#TODO:
        if len(desc) > 100:
            desc = desc[:300] + '...... view more from http://new.notebook.opensourcelearning.org/'+\
                                username+'/scrapbook/scraps/note/'+unicode(note.id)+'/'
        
        message = message+desc
        msg = (message.encode('utf8'), '', '*****@*****.**', ['*****@*****.**'])   
        msgs.append(msg)         
        #share_note(note_id, username)
    send_mass_mail(tuple(msgs), fail_silently=False) 
    return HttpResponse('success', mimetype="text/plain")  
Пример #8
0
def share(request, username):
    print 'share in note called'
    note_ids = request.POST.getlist('note_ids')
    N = getNote(request.user.username)
    msgs = []
    for note_id in note_ids:
        note = N.objects.get(id=note_id)
        message = 'From osl scraps:' + ' ' + note.title + ' ' + note.url + '  '
        desc = note.desc
        desc = desc.replace('\r', '')
        desc = desc.replace('\n', '')  #TODO:
        if len(desc) > 100:
            desc = desc[:300] + '...... view more from http://new.notebook.opensourcelearning.org/'+\
                                username+'/scrapbook/scraps/note/'+unicode(note.id)+'/'

        message = message + desc
        msg = (message.encode('utf8'), '', '*****@*****.**',
               ['*****@*****.**'])
        msgs.append(msg)
        #share_note(note_id, username)
    send_mass_mail(tuple(msgs), fail_silently=False)
    return HttpResponse('success', mimetype="text/plain")
Пример #9
0
def add_bookmark(request):
    username = request.user.username
    N = getNote(username, 'bookmarkbook')
    T = getT(username)
    #W = getW(username)
    #w = W.objects.get(name='bookmarkbook')
    if request.method == 'POST': 
        tags = T.objects.all()
        #the same as in add scrap code, here we don't use form because tags in from is required
        #AddNForm = create_model_form("AddNForm_"+str(username), N, fields={'tags':forms.ModelMultipleChoiceField(queryset=tags)})     
        n = N()  
        post = request.POST.copy()
        url = post.get('url')
        if N.objects.filter(url=url).exists():            
            return render_to_response("include/notes/addNote_result.html",{'message':_('This bookmark aready exists! You can close this window, or it will be closed for you in 2 seconds.')})
        tag_names = post.getlist('item[tags][]')        
        tags = []
        for tag_name in tag_names:
            t, created = T.objects.get_or_create(name=tag_name)
#===============================================================================
#            
#            if created or not w.tags.filter(name=t.name).exists():
#                w.tags.add(t)
#===============================================================================
            #tags.append(t.id)
            tags.append(t.name) 
        
#===============================================================================
#        if not tag_names:
#            tags = [T.objects.get(name='untagged').id]
#===============================================================================
#===============================================================================
#        post.setlist('tags', tags)
#        
#        f = AddNForm(post, instance=n)        
#        log.debug("f.errors:"+str(f.errors))
#        f.save()
#===============================================================================

        if not tags or (len(tags) == 1 and tags[0] == u''):
            tags = None
        n.title = post.get('title')
        n.desc = post.get('desc')
        n.url = post.get('url')
        private = post.get('private', False)
        if private in ['true', 'on']:
            n.private = True
        else:
            n.private = False     
        #n.private = post.get('private', False)

        n.vote = post.get('vote')
        n.save()
        n.add_tags(tags, 'bookmarkbook') 
        n.save()        
        return render_to_response("include/notes/addNote_result.html",{'message':_('Bookmark is successfully added! You can close this window, or it will be closed for you in 1 second.')})
       
    else:
        tags = __get_ws_tags(request, username, 'bookmarkbook')  
        from django.forms import TextInput
        #by adding the tags field specifically here, we avoided it using tags of another user (a strange error which repeat even after changing class names and variable names)
        AddNForm = create_model_form("AddNForm_"+str(username), N, fields={},#{'tags':forms.ModelMultipleChoiceField(queryset=tags)},
                                                             options={'exclude':['deleted'],
                                                             'fields':['url','title','tags','desc','vote','private'],
                                                             'widgets':{'title': TextInput(attrs={'size': 60}),
                                                                       }})
        url = request.GET.get('url')
        if N.objects.filter(url=url).exists():
            b = N.objects.get(url=url)
            #addNoteForm = AddNForm(initial={'url': url, 'title':b.title, 'tags': b.tags.all()})
            existing = True         
            return render_to_response('bookmarkbook/notes/addNote.html', {'existing':existing, 'url':url, 'id':b.id, 'title':b.title}, context_instance=RequestContext(request)) 
        
        
        title = request.GET.get('title')          
        #default_tag_id = T.objects.get(name='untagged').id 
        addNoteForm = AddNForm(initial={'url': url, 'title':title#, 'tags': [default_tag_id]
                                        })        
        return render_to_response('bookmarkbook/notes/addNote.html', {'addNoteForm': addNoteForm, 'url':url, 'tags':tags}) #TODO:how to display url using the form's initial
Пример #10
0
def add_scrap(request):
    username = request.user.username
    N = getNote(username, 'scrapbook')
    T = getT(username)
    #W = getW(username)
    #w = W.objects.get(name='scrapbook')  
    if request.method == 'POST': 
        tags = T.objects.all()
        #form require tags to be required. So don't use form now, and use the code from add_note in notebook.notes.views for adding a snippet
        #AddNForm = create_model_form("AddNForm_add_scrap_post_"+str(username), N, fields={'tags':forms.ModelMultipleChoiceField(queryset=tags)})     
        n = N() 
        post = request.POST.copy()
        tag_names = post.getlist('item[tags][]')
        tags = []
        for tag_name in tag_names:
            t, created = T.objects.get_or_create(name=tag_name)
#==============Don't need below any more since add_tags will do this logic=================================================================
#            if created or not w.tags.filter(name=t.name).exists():
#                w.tags.add(t)
#===============================================================================
            #tags.append(t.id)
            tags.append(t.name)    
        

        #if not tag_names:           
#                tags = [T.objects.get(name='untagged').id]
             
    
  

        if not tags or (len(tags) == 1 and tags[0] == u''):
            tags = None
        #f = AddNForm(post, instance=n)
        #log.debug("f.errors:"+str(f.errors))
        #TODO:handle errors such as url broken
        #n = f.save(commit=False)
        
        n.title = post.get('title')
        n.desc = post.get('desc')
        n.url = post.get('url')
        private = post.get('private', False)
        if private in ['true', 'on']:
            n.private = True
        else:
            n.private = False     
        n.vote = post.get('vote')
        n.save()
        n.add_tags(tags, 'scrapbook')   
 
        n.save() #called this specifically to save the url to the social db as well
        return render_to_response("include/notes/addNote_result.html",\
                                  {'message':_('Scrap is successfully added! You can close this window, or it will be closed for you in 1 second.')})
       
    else:        
        tags = __get_ws_tags(request, username, 'scrapbook')        
        from django.forms import TextInput
        #by adding the tags field specifically here, we avoided it using tags of another user (a strange error which repeat even after changing class names and variable names)
        AddNForm_scrap = create_model_form("AddNForm_add_scrap_get_"+str(username), N, fields={#'tags':forms.ModelMultipleChoiceField(queryset=tags)
                                                                                               }, options={'exclude':['deleted'],
                                                             'fields':['url','title','tags','desc','vote','private'],
                                                             'widgets':{'title': TextInput(attrs={'size': 80}),
                                                                       }})
        
        url = request.GET.get('url')
        title = request.GET.get('title')
        desc = request.GET.get('desc')
       
        #default_tag_id = T.objects.get(name='untagged').id  
        addNoteForm = AddNForm_scrap(initial={'url': url, 'title':title, 'desc':desc#, 'tags': [default_tag_id]
                                              })
        #no need of the custimized form in the scrapbook template
        return render_to_response('scrapbook/notes/addNote.html', {'addNoteForm': addNoteForm, 'desc':desc, 'url':url, 'tags':tags})
Пример #11
0
def add_bookmark(request):
    username = request.user.username
    N = getNote(username, 'bookmarkbook')
    T = getT(username)
    #W = getW(username)
    #w = W.objects.get(name='bookmarkbook')
    if request.method == 'POST':
        tags = T.objects.all()
        #the same as in add scrap code, here we don't use form because tags in from is required
        #AddNForm = create_model_form("AddNForm_"+str(username), N, fields={'tags':forms.ModelMultipleChoiceField(queryset=tags)})
        n = N()
        post = request.POST.copy()
        url = post.get('url')
        if N.objects.filter(url=url).exists():
            return render_to_response(
                "include/notes/addNote_result.html", {
                    'message':
                    _('This bookmark aready exists! You can close this window, or it will be closed for you in 2 seconds.'
                      )
                })
        tag_names = post.getlist('item[tags][]')
        tags = []
        for tag_name in tag_names:
            t, created = T.objects.get_or_create(name=tag_name)
            #===============================================================================
            #
            #            if created or not w.tags.filter(name=t.name).exists():
            #                w.tags.add(t)
            #===============================================================================
            #tags.append(t.id)
            tags.append(t.name)


#===============================================================================
#        if not tag_names:
#            tags = [T.objects.get(name='untagged').id]
#===============================================================================
#===============================================================================
#        post.setlist('tags', tags)
#
#        f = AddNForm(post, instance=n)
#        log.debug("f.errors:"+str(f.errors))
#        f.save()
#===============================================================================

        if not tags or (len(tags) == 1 and tags[0] == u''):
            tags = None
        n.title = post.get('title')
        n.desc = post.get('desc')
        n.url = post.get('url')
        private = post.get('private', False)
        if private in ['true', 'on']:
            n.private = True
        else:
            n.private = False
        #n.private = post.get('private', False)

        n.vote = post.get('vote')
        n.save()
        n.add_tags(tags, 'bookmarkbook')
        n.save()
        return render_to_response(
            "include/notes/addNote_result.html", {
                'message':
                _('Bookmark is successfully added! You can close this window, or it will be closed for you in 1 second.'
                  )
            })

    else:
        tags = __get_ws_tags(request, username, 'bookmarkbook')
        from django.forms import TextInput
        #by adding the tags field specifically here, we avoided it using tags of another user (a strange error which repeat even after changing class names and variable names)
        AddNForm = create_model_form(
            "AddNForm_" + str(username),
            N,
            fields={},  #{'tags':forms.ModelMultipleChoiceField(queryset=tags)},
            options={
                'exclude': ['deleted'],
                'fields': ['url', 'title', 'tags', 'desc', 'vote', 'private'],
                'widgets': {
                    'title': TextInput(attrs={'size': 60}),
                }
            })
        url = request.GET.get('url')
        if N.objects.filter(url=url).exists():
            b = N.objects.get(url=url)
            #addNoteForm = AddNForm(initial={'url': url, 'title':b.title, 'tags': b.tags.all()})
            existing = True
            return render_to_response('bookmarkbook/notes/addNote.html', {
                'existing': existing,
                'url': url,
                'id': b.id,
                'title': b.title
            },
                                      context_instance=RequestContext(request))

        title = request.GET.get('title')
        #default_tag_id = T.objects.get(name='untagged').id
        addNoteForm = AddNForm(initial={
            'url': url,
            'title': title  #, 'tags': [default_tag_id]
        })
        return render_to_response('bookmarkbook/notes/addNote.html', {
            'addNoteForm': addNoteForm,
            'url': url,
            'tags': tags
        })  #TODO:how to display url using the form's initial
Пример #12
0
def add_scrap(request):
    username = request.user.username
    N = getNote(username, 'scrapbook')
    T = getT(username)
    #W = getW(username)
    #w = W.objects.get(name='scrapbook')
    if request.method == 'POST':
        tags = T.objects.all()
        #form require tags to be required. So don't use form now, and use the code from add_note in notebook.notes.views for adding a snippet
        #AddNForm = create_model_form("AddNForm_add_scrap_post_"+str(username), N, fields={'tags':forms.ModelMultipleChoiceField(queryset=tags)})
        n = N()
        post = request.POST.copy()
        tag_names = post.getlist('item[tags][]')
        tags = []
        for tag_name in tag_names:
            t, created = T.objects.get_or_create(name=tag_name)
            #==============Don't need below any more since add_tags will do this logic=================================================================
            #            if created or not w.tags.filter(name=t.name).exists():
            #                w.tags.add(t)
            #===============================================================================
            #tags.append(t.id)
            tags.append(t.name)

        #if not tag_names:


#                tags = [T.objects.get(name='untagged').id]

        if not tags or (len(tags) == 1 and tags[0] == u''):
            tags = None
        #f = AddNForm(post, instance=n)
        #log.debug("f.errors:"+str(f.errors))
        #TODO:handle errors such as url broken
        #n = f.save(commit=False)

        n.title = post.get('title')
        n.desc = post.get('desc')
        n.url = post.get('url')
        private = post.get('private', False)
        if private in ['true', 'on']:
            n.private = True
        else:
            n.private = False
        n.vote = post.get('vote')
        n.save()
        n.add_tags(tags, 'scrapbook')

        n.save(
        )  #called this specifically to save the url to the social db as well
        return render_to_response("include/notes/addNote_result.html",\
                                  {'message':_('Scrap is successfully added! You can close this window, or it will be closed for you in 1 second.')})

    else:
        tags = __get_ws_tags(request, username, 'scrapbook')
        from django.forms import TextInput
        #by adding the tags field specifically here, we avoided it using tags of another user (a strange error which repeat even after changing class names and variable names)
        AddNForm_scrap = create_model_form(
            "AddNForm_add_scrap_get_" + str(username),
            N,
            fields={  #'tags':forms.ModelMultipleChoiceField(queryset=tags)
            },
            options={
                'exclude': ['deleted'],
                'fields': ['url', 'title', 'tags', 'desc', 'vote', 'private'],
                'widgets': {
                    'title': TextInput(attrs={'size': 80}),
                }
            })

        url = request.GET.get('url')
        title = request.GET.get('title')
        desc = request.GET.get('desc')

        #default_tag_id = T.objects.get(name='untagged').id
        addNoteForm = AddNForm_scrap(initial={
            'url': url,
            'title': title,
            'desc': desc  #, 'tags': [default_tag_id]
        })
        #no need of the custimized form in the scrapbook template
        return render_to_response('scrapbook/notes/addNote.html', {
            'addNoteForm': addNoteForm,
            'desc': desc,
            'url': url,
            'tags': tags
        })