Esempio n. 1
0
    def reshare(self, entry, args={}):
        sid = args.get('sid', None)
        as_me = int(args.get('as_me', False))
        user = args.get('user', None)

        un = utcnow()
        guid = '%s/entry/%s' % (settings.FEED_TAGURI,
                                un.strftime('%Y-%m-%dT%H:%M:%SZ'))
        if sid:
            s = Service.objects.get(id=sid, api='selfposts')
        else:
            s = Service.objects.filter(api='selfposts').order_by('id')[0]
        e = Entry(service=s, guid=guid)

        e.date_published = un
        e.date_updated = un

        if as_me:
            if user and user.first_name and user.last_name:
                e.author_name = user.first_name + ' ' + user.last_name
            else:
                e.author_name = ''
            e.author_email = ''
            e.author_uri = ''
            if entry.service.api == 'greader':
                e.link = entry.link
            else:
                e.link = settings.BASE_URL + '/'
            if entry.service.api == 'twitter':
                entry.content = entry.content.split(': ', 1)[1]
        else:
            e.author_name = entry.author_name
            e.author_email = entry.author_email
            e.author_uri = entry.author_uri
            e.link = entry.link

        e.geolat = entry.geolat
        e.geolng = entry.geolng
        e.mblob = entry.mblob

        e.title = entry.title
        if entry.service.api == 'greader':
            e.content = '<a href="%s" rel="nofollow">%s</a>' % (e.link,
                                                                e.title)
        elif entry.service.api in ('youtube', 'vimeo'):
            e.content = '<p>%s</p>%s' % (df_title(e.title), entry.content)
        else:
            e.content = urlizetrunc(entry.content, 45)

        try:
            media.transform_to_local(e)
            media.extract_and_register(e)
            e.save()
            return e
        except:
            pass
Esempio n. 2
0
    def reshare(self, entry, args={}):
        sid = args.get('sid', None)
        as_me = int(args.get('as_me', False))
        user = args.get('user', None)

        un = utcnow()
        guid = '%s/entry/%s' % (settings.FEED_TAGURI,
                                un.strftime('%Y-%m-%dT%H:%M:%SZ'))
        if sid:
            s = Service.objects.get(id=sid, api='selfposts')
        else:
            s = Service.objects.filter(api='selfposts').order_by('id')[0]
        e = Entry(service=s, guid=guid)

        e.date_published = un
        e.date_updated = un

        if as_me:
            if user and user.first_name and user.last_name:
                e.author_name = user.first_name + ' ' + user.last_name
            else:
                e.author_name = ''
            e.author_email = ''
            e.author_uri = ''
            if entry.service.api == 'greader':
                e.link = entry.link
            else:
                e.link = settings.BASE_URL + '/'
            if entry.service.api == 'twitter':
                entry.content = entry.content.split(': ', 1)[1]
        else:
            e.author_name = entry.author_name
            e.author_email = entry.author_email
            e.author_uri = entry.author_uri
            e.link = entry.link

        e.geolat = entry.geolat
        e.geolng = entry.geolng
        e.mblob = entry.mblob

        e.title = entry.title
        if entry.service.api == 'greader':
            e.content = '<a href="%s" rel="nofollow">%s</a>' % (
                e.link, e.title)
        elif entry.service.api in ('youtube', 'vimeo'):
            e.content = '<p>%s</p>%s' % (df_title(e.title), entry.content)
        else:
            e.content = urlizetrunc(entry.content, 45)

        try:
            media.transform_to_local(e)
            media.extract_and_register(e)
            e.save()
            return e
        except:
            pass
Esempio n. 3
0
def api(request, **args):
    cmd = args.get('cmd', '')
    entry = request.POST.get('entry', None)

    authed = request.user.is_authenticated() and request.user.is_staff
    friend = request.user.is_authenticated() and not request.user.is_staff
    if not authed and cmd != 'getcontent':
        return HttpResponseForbidden()

    if cmd == 'hide' and entry:
        Entry.objects.filter(id=int(entry)).update(active=False)

    elif cmd == 'unhide' and entry:
        Entry.objects.filter(id=int(entry)).update(active=True)

    elif cmd == 'gsc':  # get selfposts classes
        _srvs = Service.objects.filter (api='selfposts')\
            .order_by('cls').values('id', 'cls')
        srvs = {}
        for item in _srvs:
            if item['cls'] not in srvs:
                srvs[item['cls']] = item
        srvs = list(srvs.values())

        d = []
        for s in srvs:
            d.append({'id': s['id'], 'cls': s['cls']})
        return HttpResponse(json.dumps(d), content_type='application/json')

    elif cmd == 'share':
        images = []
        for i in range(0, 5):
            img = request.POST.get('image' + str(i), None)
            if img:
                images.append(img)
        source = request.POST.get('from', '')
        entry = selfposts.API(False).share({
            'content':
            request.POST.get('content', ''),
            'sid':
            request.POST.get('sid', None),
            'draft':
            request.POST.get('draft', False),
            'friends_only':
            request.POST.get('friends_only', False),
            'link':
            request.POST.get('link', None),
            'images':
            images,
            'files':
            request.FILES,
            'source':
            source,
            'user':
            request.user
        })
        if entry:
            if not entry.draft:
                pshb.publish()
            if source == 'bookmarklet':
                d = {
                    'close_msg':
                    _("You've successfully shared this web page at your stream."
                      )
                }
                return HttpResponse(json.dumps(d),
                                    content_type='application/json')
            else:
                entry.friends_only = False
                if request.is_ajax():
                    return render_to_response('stream-pure.html', {
                        'entries': (entry, ),
                        'authed': authed
                    })
                else:
                    return HttpResponseRedirect(settings.BASE_URL + '/')

    elif cmd == 'reshare' and entry:
        try:
            entry = Entry.objects.get(id=int(entry))
            if entry:
                entry = selfposts.API(False).reshare(
                    entry, {
                        'as_me': request.POST.get('as_me', False),
                        'user': request.user
                    })
                if entry:
                    pshb.publish()
                    return render_to_response('stream-pure.html', {
                        'entries': (entry, ),
                        'authed': authed
                    })
        except Exception:
            pass

    elif cmd == 'favorite':
        try:
            entry = Entry.objects.get(id=int(entry))
            if entry:
                try:
                    fav = Favorite.objects.get(user=request.user, entry=entry)
                except Favorite.DoesNotExist:
                    fav = Favorite(user=request.user, entry=entry)
                    fav.save()
                    media.transform_to_local(entry)
                    media.extract_and_register(entry)
                    entry.save()
        except Exception:
            pass

    elif cmd == 'unfavorite':
        try:
            if entry:
                entry = Entry.objects.get(id=int(entry))
                if entry:
                    Favorite.objects.get(user=request.user,
                                         entry=entry).delete()
        except Exception:
            pass

    elif cmd == 'getcontent':
        try:
            if entry:
                if not authed:
                    entry = Entry.objects.get(id=int(entry),
                                              service__public=True)
                else:
                    entry = Entry.objects.get(id=int(entry))
                if entry:
                    if request.POST.get('raw', False) and authed:
                        return HttpResponse(entry.content)

                    if authed or friend:
                        entry.friends_only = False
                    content = fix_ampersands(gls_content('', entry))
                    return HttpResponse(content)
        except Exception:
            pass

    elif cmd == 'putcontent':
        try:
            if entry and authed:
                content = request.POST.get('content', '')
                if content:
                    Entry.objects.filter(id=int(entry)).update(content=content)
                entry = Entry.objects.get(id=int(entry))
                if entry:
                    content = fix_ampersands(gls_content('', entry))
                    return HttpResponse(content)
        except Exception:
            pass

    return HttpResponse()
Esempio n. 4
0
def api(request, **args):
    cmd = args.get('cmd', '')
    entry = request.POST.get('entry', None)

    authed = request.user.is_authenticated() and request.user.is_staff
    friend = request.user.is_authenticated() and not request.user.is_staff
    if not authed and cmd != 'getcontent':
        return HttpResponseForbidden()

    if cmd == 'hide' and entry:
        Entry.objects.filter(id=int(entry)).update(active=False)

    elif cmd == 'unhide' and entry:
        Entry.objects.filter(id=int(entry)).update(active=True)

    elif cmd == 'gsc':  # get selfposts classes
        _srvs = Service.objects.filter (api='selfposts')\
            .order_by('cls').values('id', 'cls')
        srvs = {}
        for item in _srvs:
            if item['cls'] not in srvs:
                srvs[item['cls']] = item
        srvs = list(srvs.values())

        d = []
        for s in srvs:
            d.append({'id': s['id'], 'cls': s['cls']})
        return HttpResponse(json.dumps(d), content_type='application/json')

    elif cmd == 'share':
        images = []
        for i in range(0, 5):
            img = request.POST.get('image' + str(i), None)
            if img:
                images.append(img)
        source = request.POST.get('from', '')
        entry = selfposts.API(False).share(
            {'content': request.POST.get('content', ''),
             'sid': request.POST.get('sid', None),
             'draft': request.POST.get('draft', False),
             'friends_only': request.POST.get('friends_only', False),
             'link': request.POST.get('link', None),
             'images': images,
             'files': request.FILES,
             'source': source,
             'user': request.user})
        if entry:
            if not entry.draft:
                pshb.publish()
            if source == 'bookmarklet':
                d = {'close_msg': _(
                    "You've successfully shared this web page at your stream.")}
                return HttpResponse(json.dumps(d),
                                    content_type='application/json')
            else:
                entry.friends_only = False
                if request.is_ajax():
                    return render_to_response('stream-pure.html',
                                              {'entries': (entry,),
                                               'authed': authed})
                else:
                    return HttpResponseRedirect(settings.BASE_URL + '/')

    elif cmd == 'reshare' and entry:
        try:
            entry = Entry.objects.get(id=int(entry))
            if entry:
                entry = selfposts.API(False).reshare(
                    entry, {'as_me': request.POST.get('as_me', False),
                            'user': request.user})
                if entry:
                    pshb.publish()
                    return render_to_response('stream-pure.html',
                                              {'entries': (entry,),
                                               'authed': authed})
        except Exception:
            pass

    elif cmd == 'favorite':
        try:
            entry = Entry.objects.get(id=int(entry))
            if entry:
                try:
                    fav = Favorite.objects.get(user=request.user, entry=entry)
                except Favorite.DoesNotExist:
                    fav = Favorite(user=request.user, entry=entry)
                    fav.save()
                    media.transform_to_local(entry)
                    media.extract_and_register(entry)
                    entry.save()
        except Exception:
            pass

    elif cmd == 'unfavorite':
        try:
            if entry:
                entry = Entry.objects.get(id=int(entry))
                if entry:
                    Favorite.objects.get(user=request.user,
                                         entry=entry).delete()
        except Exception:
            pass

    elif cmd == 'getcontent':
        try:
            if entry:
                if not authed:
                    entry = Entry.objects.get(id=int(entry),
                                              service__public=True)
                else:
                    entry = Entry.objects.get(id=int(entry))
                if entry:
                    if request.POST.get('raw', False) and authed:
                        return HttpResponse(entry.content)

                    if authed or friend:
                        entry.friends_only = False
                    content = fix_ampersands(gls_content('', entry))
                    return HttpResponse(content)
        except Exception:
            pass

    elif cmd == 'putcontent':
        try:
            if entry and authed:
                content = request.POST.get('content', '')
                if content:
                    Entry.objects.filter(id=int(entry)).update(
                        content=content)
                entry = Entry.objects.get(id=int(entry))
                if entry:
                    content = fix_ampersands(gls_content('', entry))
                    return HttpResponse(content)
        except Exception:
            pass

    return HttpResponse()