Beispiel #1
0
def channels_add(request):
    if request.method == 'POST':
        form = ChannelForm(request.POST, error_class=SpanErrorList)
        if form.is_valid():
            chan_form = form.save(commit=False)
            chan_form.created_by = request.user
            chan_form.save()
            messages.success(request, "Your channel '%s' was added." % form.cleaned_data['channel_name'])
            return HttpResponseRedirect(reverse('channels_list'))
    else:
        form = ChannelForm()
    
    form.fields['site'].queryset = ApiAccount.objects.filter(user=request.user)
        
    return render_to_response('channels_add.html', {'form':form},
                                                    context_instance=RequestContext(request)) 
Beispiel #2
0
def channels_add(request):
    if request.method == 'POST':
        form = ChannelForm(request.POST, error_class=SpanErrorList)
        if form.is_valid():
            chan_form = form.save(commit=False)
            chan_form.created_by = request.user
            chan_form.save()
            messages.success(
                request, "Your channel '%s' was added." %
                form.cleaned_data['channel_name'])
            return HttpResponseRedirect(reverse('channels_list'))
    else:
        form = ChannelForm()

    form.fields['site'].queryset = ApiAccount.objects.filter(user=request.user)

    return render_to_response('channels_add.html', {'form': form},
                              context_instance=RequestContext(request))
Beispiel #3
0
def channels_edit(request, channel_id):
    channel = get_object_or_404(UserCreatedChannel, pk=channel_id)
    if not channel.created_by == request.user:
        raise Http404 
        
    if request.method == 'POST':
        form = ChannelForm(request.POST, error_class=SpanErrorList, instance=channel)
        if form.is_valid():
            chan_form = form.save(commit=False)
            chan_form.created_by = request.user
            chan_form.save() 
            messages.success(request, "Your channel '%s' was saved." % form.cleaned_data['channel_name'])
            return HttpResponseRedirect(reverse('channels_list'))
    else:
        form = ChannelForm(instance=channel)
    
    form.fields['site'].queryset = ApiAccount.objects.filter(user=request.user)
        
    return render_to_response('channels_edit.html', {'form':form},
                                                    context_instance=RequestContext(request)) 
Beispiel #4
0
def channels_edit(request, channel_id):
    channel = get_object_or_404(UserCreatedChannel, pk=channel_id)
    if not channel.created_by == request.user:
        raise Http404

    if request.method == 'POST':
        form = ChannelForm(request.POST,
                           error_class=SpanErrorList,
                           instance=channel)
        if form.is_valid():
            chan_form = form.save(commit=False)
            chan_form.created_by = request.user
            chan_form.save()
            messages.success(
                request, "Your channel '%s' was saved." %
                form.cleaned_data['channel_name'])
            return HttpResponseRedirect(reverse('channels_list'))
    else:
        form = ChannelForm(instance=channel)

    form.fields['site'].queryset = ApiAccount.objects.filter(user=request.user)

    return render_to_response('channels_edit.html', {'form': form},
                              context_instance=RequestContext(request))
Beispiel #5
0
def create_shorten_links(request,
                         template_name='dashboard/create_shorten_links.html'):
    if request.method == 'GET':
        form = ChannelForm(client=request.client)
        return render(request, template_name, {'form': form})
    elif request.method == 'POST':
        form = ChannelForm(data=request.POST, client=request.client)
        if form.is_valid():
            channel = form.save(commit=False)

            if form.cleaned_data['platform'] != 'WEC':
                channel.network = 'FB'
                aid_prefix = 'FBCS'
                aid_2 = channel.project.aid_pattern[4:6]
                project = Project.objects.get(name=channel.project)
                if form.cleaned_data['platform'] == 'FBC':
                    url_str = project.non_cn_url_chi
                    channel.language = 'CHI'
                    aid_offset = 1000
                else:
                    url_str = project.non_cn_url_eng
                    channel.language = 'ENG'
                    aid_offset = 0

                aid_3 = Channel.objects.filter(
                    project=channel.project,
                    network='FB',
                    language=channel.language).count() + 8001 + aid_offset
                channel.aid = str(aid_prefix) + str(aid_2) + str(aid_3)
                query_dict = {
                    'fbcsid': channel.aid,
                    'lang': channel.language.lower()
                }
                query_string = urllib.urlencode(query_dict)
                long_url = '%s?%s' % (url_str, query_string)

                shortener = Shortener(
                    'Google', api_key=settings.DASHBOARD_GOOGLE_API_KEY)
                for i in xrange(3):
                    try:
                        channel.short_url = shortener.short(long_url)
                    except Timeout as e:
                        last_err = e
                    else:
                        break
                else:
                    raise Exception(
                        'Failed to generate the Google shorten link, %s' %
                        (str(last_err)))

            elif form.cleaned_data['platform'] == 'WEC':
                aid_1 = 'WECS'
                aid_2 = channel.project.aid_pattern[4:6]
                channel.network = 'WE'
                channel.language = 'SC'
                url_str = Project.objects.get(name=channel.project).cn_url
                aid_3 = Channel.objects.filter(project=channel.project,
                                               network='WE').count() + 9001
                channel.aid = str(aid_1) + str(aid_2) + str(aid_3)
                long_url = '%s?fbcsid=%s' % (url_str, channel.aid)
                service = settings.DASHBOARD_WEIBO_API_KEY
                for i in xrange(3):
                    try:
                        channel.short_url = json.loads(
                            getLink(service,
                                    long_url))[0].get('url_short', 'None')
                    except Timeout as e:
                        last_err = e
                    else:
                        break
                else:
                    raise Exception(
                        'Failed to generate the Tencent shorten link, %s' %
                        (str(last_err)))
            channel.save()

            return render(request,
                          'dashboard/create_shorten_links_finish.html', {
                              'channel': channel,
                          })
        else:
            return render(request, template_name, {
                'form': form,
            })