Пример #1
0
    def init_with_context(self, context):
        site_name = get_admin_site_name(context)
        # append a link list module for "quick links"

        self.children.append(
            ModelAdd(None, model=Story, text='Create new Story'))
        self.children.append(
            OwnInstancesList(_('My Stories'), model=Story, key='authors'))
        self.children.append(
            modules.AppList(title='Tumblelog', models=('tumblelog.*', )))
        self.children.append(
            modules.LinkList('bookmarklet',
                             children=[{
                                 'title':
                                 'luminous flux bookmarklet',
                                 'url':
                                 generate_bookmarklink(context['request'])
                             }]))
        self.children.append(
            modules.AppList(title='Static Content',
                            models=('django.contrib.flatpages.*', )))
        self.children.append(
            CreateForInstance(model=ChangeSuggestion,
                              instances=Story.objects.all(),
                              key='story'))
        self.children.append(
            OwnInstancesList(_('My Suggestions'),
                             model=ChangeSuggestion,
                             key='user'))
def bookmarklet_window(request):
    forms = form_for_type

    user = get_profile_model().get_by_api_key(request.GET['api_key']).user
    templatevars = {'site': get_current_site(request), 'bookmarklink': generate_bookmarklink(request, user), 'proper': request.method=='POST',
                    'typeimages': hasattr(settings,'TUMBLELOG_TYPEIMAGES') and settings.TUMBLELOG_TYPEIMAGES, 'apiuser': user,
                    }
    if request.method == 'POST':
        oembed = None

        url = request.POST['url']
        proper = request.POST.get('proper')
        images = [(request.POST[x],request.POST[x+'_w'],request.POST[x+'_h'],) for x in request.POST.keys() if x.startswith('img_') and not x[-1] in ('w','h')]
        images.sort(key=itemgetter(2))
        images = json.loads(base64.b64decode(request.POST['images'])) if request.POST.get('images',None) else images
        quote = html2text(request.POST.get('selection',''))

        if not proper:
            oembed = embedly_support.get_info_if_active(url)

        if oembed:
            mode = oembed['type']
        else:
            mode = 'quote' if quote else 'picture' if images else 'link'

        mode = request.POST['submit'] if request.POST.get('submit', None) else mode

        templatevars['url'] = url
        templatevars['images'] = images
        templatevars['quote'] = quote
        templatevars['api_key'] = user.get_profile().api_key()
        initial = {'url': url,'author':user,'body':quote}
        def generate_meta(name, parentmeta):
            newmeta = copy.copy(parentmeta)
            newmeta.exclude = (copy.copy(parentmeta.exclude) or []) + ['author','post_type','data']
            return newmeta
        formchilds = []
        for name, form in forms.iteritems():
            newcls = type(form.__name__, (form,), {'Meta': generate_meta(name, form._meta)})
            formchilds.append((name,newcls,))

        forms = {}
        for x,y in formchilds:
            init = copy.copy(initial)
            data = None if not proper and oembed and mode == x else request.POST if proper and mode == x else None
            if oembed and mode==x:
                init.update(oembed.data)
            prefix = x

            forms[x] = y(
                    data,
                    prefix=prefix,
                    initial=dict(init.items() + {'post_type': x}.items()),
                    instance = Post(author=user))
            if not proper and oembed and mode==x:
                try:
                    forms[x].is_valid()
                except ValueError,x:
                    print x
        if PARENT_MODEL:
            for name, form in forms.iteritems():
                form.fields['parent'].queryset = PARENT_MODEL_GET().objects.for_user(user)
        if request.POST.get('submit', None):
            mode = request.POST['submit']
            form = forms[mode]
            if form.instance:
                form.instance.post_type = mode
            try:
                form.full_clean()
                if form.is_valid():
                    form.save()
                    templatevars['success'] = True
            except ValueError, e:
                pass # if value errors occur here, they should have been caught by form validation.
Пример #3
0
    def init_with_context(self, context):
        site_name = get_admin_site_name(context)
        # append a link list module for "quick links"

        self.children.append(ModelAdd(None, model=Story, text='Create new Story'))
        self.children.append(OwnInstancesList(_('My Stories'), model=Story, key='authors'))
        self.children.append(modules.AppList(title='Tumblelog', models=('tumblelog.*',)))
        self.children.append(modules.LinkList('bookmarklet', children=[{'title': 'luminous flux bookmarklet', 'url': generate_bookmarklink(context['request'])}]))
        self.children.append(modules.AppList(title='Static Content', models=('django.contrib.flatpages.*',)))
        self.children.append(CreateForInstance(model=ChangeSuggestion, instances=Story.objects.all(), key='story'))
        self.children.append(OwnInstancesList(_('My Suggestions'), model=ChangeSuggestion, key='user'))