Exemplo n.º 1
0
 def create(self, validated_data):
     bookmark = Bookmark(
         title=validated_data['title'],
         siteName=validated_data['siteName'],
         url=validated_data['url'],
         type=self.data.get('type',
                            Bookmark._meta.get_field('type').get_default()),
         user=self.context['request'].user,
     )
     bookmark.save()
     return bookmark
Exemplo n.º 2
0
def bookmarklet_create(request):
    """ Create a new bookmark within the collection ID provided. --- Change it to not provided for now"""
    #collection = get_object_or_404(Collection, pk=collection_id)
    if request.user.is_authenticated():

        if request.method == 'POST': # If the form has been submitted...
            form = CreateBookmarklet(request.POST) # A form bound to the POST data
            if form.is_valid(): # All validation rules pass
                name = form.cleaned_data['name']
                url = form.cleaned_data['url']
                is_private = form.cleaned_data['is_private']
                read_later = form.cleaned_data['read_later']
                rating = form.cleaned_data['rating']
                notes = form.cleaned_data['notes']
                collection = form.cleaned_data['collection']
                userprofile = request.user.userprofile
                
                raw_html = ''
                if read_later:
                    get_url = urlopen(url) # Opens a connection to the bookmark's url
                    raw_html = get_url.read() # Reads that connection into raw_html to be saved.
                    get_url.close() # Closes the connection.

                new_bookmark = Bookmark(name=name, is_private=is_private, url=url, read_later=read_later, collection=collection, rating=rating, notes=notes, raw_html=raw_html, user=userprofile)
                new_bookmark.save()
                
                return HttpResponseRedirect(reverse('core:bookmark_detail', args=[new_bookmark.id]) ) # Redirect after POST
            else: 
                """If NOT form.is_valid()"""
                errors = "please fix errors"
                #form = CreateBookmarklet(user=request.user, initial={'url': url, 'name': title}) # An unbound form
                #form.fields['url'].widget = forms.HiddenInput()
                #form = CreateBookmarklet(request.user)
                return render(request,'core/bookmarklet_create.html',{'form':form, 'errors': errors})
        else:
            data = request.GET.copy()
            url = data['url']
            title = data['title']
            form = CreateBookmarklet(user=request.user, initial={'url': url, 'name': title}) # An unbound form
            form.fields['url'].widget = forms.HiddenInput()
            #form = CreateBookmarklet() #An unbound form
            return render(request,'core/bookmarklet_create.html',{'form':form})
    else:
        form = AuthenticationForm()
        return render(request, 'registration/login.html', {'form':form})
Exemplo n.º 3
0
def bookmark_create(request):
    """ Create a new bookmark within the collection ID provided. --- Change it to not provided for now"""
    #collection = get_object_or_404(Collection, pk=collection_id)
    if request.user.is_authenticated():
        if request.method == 'POST': # If the form has been submitted...
            form = CreateBookmark(request.POST) # A form bound to the POST data
            if form.is_valid(): # All validation rules pass
                name = form.cleaned_data['name']
                url = form.cleaned_data['url']
                is_private = form.cleaned_data['is_private']
                read_later = form.cleaned_data['read_later']
                rating = form.cleaned_data['rating']
                notes = form.cleaned_data['notes']
                collection = form.cleaned_data['collection']
                userprofile = request.user.userprofile
            
                get_url = urlopen(url) # Opens a connection to the bookmark's url
                raw_html = get_url.read() # Reads that connection into raw_html to be saved.
                get_url.close() # Closes the connection.

                new_bookmark = Bookmark(name=name, is_private=is_private, url=url, read_later=read_later,
                    collection=collection, rating=rating, notes=notes, raw_html=raw_html, user=userprofile)
                new_bookmark.save()

                return HttpResponseRedirect(reverse('core:bookmark_detail', args=[new_bookmark.id])) # Redirect after POST
        else:
            form = CreateBookmark(request.user) # An unbound form
            return render(request,'core/bookmark_create.html',{'form':form})

        """return render(request, 'core/bookmark_create.html', {
            'form': form
            })            """
        
    else:
        form = AuthenticationForm()
        return render(request, 'registration/login.html', {'form':form})