def publisher_dashboard(request): from atrinsic.base.models import AqWidget, Organization, PublisherVertical, UserAqWidget, W9Status from forms import DashboardSettingsForm, w9UploadForm from atrinsic.util.AceApi import create_company this_page = 'publisher-dashboard' all_advertisers = Organization.objects.filter(publisher_relationships__status=RELATIONSHIP_ACCEPTED, publisher_relationships__publisher=request.organization) aids = [] aids = [j.id for j in all_advertisers] x = UserAqWidget.objects.select_related("AqWidget").filter(page=this_page,organization=request.organization).order_by('sort_order') widgets = UserAqWidget.prep(x,request,aids) z = AqWidget.objects.filter(widget_type__in=[1,3], Active=1) widget_list = AqWidget.prep(z) inbox = request.organization.received_messages.filter(is_active=True).order_by('-date_sent') # Check W9 Status form, if record doesnt exist, or set to Not Received # pass bool to page to display warning Lightbox showW9Warning = False #if request.organization.country != None and request.organization.country.lower().find("us") > -1: try: wNine = W9Status.objects.get(organization=request.organization) except: wNine = W9Status.objects.create(organization=request.organization, status = W9_STATUS_NOT_RECEIVED, datereceived=datetime.datetime.now()) if wNine.status != W9_STATUS_NOT_RECEIVED: showW9Warning = False uploadForm = "" else: showW9Warning = True uploadForm = "" if request.organization.ace_id == None: create_company(request.organization) hashed_ACEID = (int(request.organization.ace_id) + 148773) * 12 return AQ_render_to_response(request, 'publisher/dashboard.html', { 'verticals' : PublisherVertical.objects.filter(is_adult=request.organization.is_adult).order_by('order'), 'widgets':widgets, 'widget_list':widget_list, 'current_page':this_page, 'msgcount' : inbox, 'settings':True, 'sdate':request.GET.get('start_date',''), 'edate':request.GET.get('end_date',''), 'showW9Warning':showW9Warning, 'wNineForm':w9UploadForm(), 'w9Link': settings.W9_PATH, 'hashed_ACEID' : hashed_ACEID, #'w9Up' : W9UploadForm() }, context_instance=RequestContext(request))
def ajax_new_chart(request, widget, new_style): from atrinsic.base.models import UserAqWidget, AqWidget if request.GET.get('preview', None) == None: user_widget = UserAqWidget.objects.get(pk=widget) my_widget = user_widget.widget else: my_widget = AqWidget.objects.get(pk=widget) user_widget = UserAqWidget(widget=my_widget) if new_style == None: new_style = my_widget.widget_style return widget_layout(request, my_widget, user_widget, new_style)
def live_dashboard(request): this_page = 'live-dashboard' from atrinsic.base.models import OrganizationContacts, AqWidget, UserAqWidget if request.method == "POST": pywik_obj = PyWik(auth='8ea3806a0efcbc383600e6209ed557fc') #create the site first. status, code, site_response = pywik_obj('SitesManager_addSite', siteName=request.POST['sites'], urls=request.POST['urls']) print "site_response %s" % site_response if status == True and code == 200: if site_response.has_key('value'): idSite = site_response['value'] else: return AQ_render_to_response( request, 'live/js_tracker.html', { 'js_tracker': site_response['message'], 'has_piwik': 0, 'error': True, }, context_instance=RequestContext(request)) instant_password = str(datetime.datetime.now()) import md5 hash = md5.new(instant_password) print "instant_password %s" % instant_password print "hash %s" % hash.hexdigest() #then create the user, could have done this in reverse order, it really doenst matter. #used a regular expression to only keep alphanumeric characters. user_login = re.sub('[^a-zA-Z0-9_]', '', request.organization.company_name)[:20] oc = OrganizationContacts.objects.select_related( "organization", "contact").get(organization=request.organization) status, code, user_response = pywik_obj('UsersManager_addUser', userLogin=user_login, password=instant_password, email=oc.email) print "user_response %s" % user_response #now that we have a user and a site, we associate them status, code, access_response = pywik_obj('UsersManager_setUserAccess', userLogin=user_login, access="view", idSites=idSite) print "access_response %s" % access_response if status == True and code == 200: if not site_response.has_key('value'): return AQ_render_to_response( request, 'live/js_tracker.html', { 'js_tracker': user_response['message'], 'has_piwik': 0, 'error': True, }, context_instance=RequestContext(request)) #now that he has access we need the auth code for the api calls he will do from now on. #first need the password hashed before you pass it for safety status, code, token_response = pywik_obj('UsersManager_getTokenAuth', userLogin=user_login, md5Password=hash.hexdigest()) print "token_response %s" % token_response #finaly we need to get the JS tag the client needs for tracking. status, code, js_response = pywik_obj('SitesManager_getJavascriptTag', idSite=idSite) print "js_response %s" % js_response request.organization.pywik_token_auth_key = token_response['value'] request.organization.pywik_siteId = idSite request.organization.save() return AQ_render_to_response(request, 'live/js_tracker.html', { 'js_tracker': js_response['value'], 'has_piwik': 0, }, context_instance=RequestContext(request)) else: if request.organization.pywik_token_auth_key != None: x = UserAqWidget.objects.select_related("AqWidget").filter( page=this_page, organization=request.organization).order_by('sort_order') widgets = UserAqWidget.prep(x, request, None) z = AqWidget.objects.filter(widget_type=2) widget_list = AqWidget.prep(z) return AQ_render_to_response( request, 'live/dashboard.html', { 'widgets': widgets, 'widget_list': widget_list, 'current_page': this_page, 'has_piwik': 1, }, context_instance=RequestContext(request)) else: return AQ_render_to_response( request, 'live/signup.html', { 'has_piwik': 0, }, context_instance=RequestContext(request))
def live_dashboard(request): this_page = 'live-dashboard' from atrinsic.base.models import OrganizationContacts,AqWidget,UserAqWidget if request.method == "POST": pywik_obj = PyWik(auth='8ea3806a0efcbc383600e6209ed557fc') #create the site first. status, code, site_response = pywik_obj('SitesManager_addSite', siteName = request.POST['sites'], urls = request.POST['urls']) print "site_response %s" % site_response if status == True and code == 200: if site_response.has_key('value'): idSite = site_response['value'] else: return AQ_render_to_response(request, 'live/js_tracker.html', { 'js_tracker':site_response['message'], 'has_piwik':0, 'error':True, }, context_instance=RequestContext(request)) instant_password=str(datetime.datetime.now()) import md5 hash = md5.new(instant_password) print "instant_password %s" % instant_password print "hash %s" % hash.hexdigest() #then create the user, could have done this in reverse order, it really doenst matter. #used a regular expression to only keep alphanumeric characters. user_login = re.sub('[^a-zA-Z0-9_]','',request.organization.company_name)[:20] oc = OrganizationContacts.objects.select_related("organization", "contact").get(organization=request.organization) status, code, user_response = pywik_obj('UsersManager_addUser',userLogin=user_login,password=instant_password,email=oc.email) print "user_response %s" % user_response #now that we have a user and a site, we associate them status, code, access_response = pywik_obj('UsersManager_setUserAccess',userLogin=user_login,access="view",idSites=idSite) print "access_response %s" % access_response if status == True and code == 200: if not site_response.has_key('value'): return AQ_render_to_response(request, 'live/js_tracker.html', { 'js_tracker':user_response['message'], 'has_piwik':0, 'error':True, }, context_instance=RequestContext(request)) #now that he has access we need the auth code for the api calls he will do from now on. #first need the password hashed before you pass it for safety status, code, token_response = pywik_obj('UsersManager_getTokenAuth',userLogin=user_login,md5Password=hash.hexdigest()) print "token_response %s" % token_response #finaly we need to get the JS tag the client needs for tracking. status, code, js_response = pywik_obj('SitesManager_getJavascriptTag',idSite=idSite) print "js_response %s" % js_response request.organization.pywik_token_auth_key = token_response['value'] request.organization.pywik_siteId = idSite request.organization.save() return AQ_render_to_response(request, 'live/js_tracker.html', { 'js_tracker':js_response['value'], 'has_piwik':0, }, context_instance=RequestContext(request)) else: if request.organization.pywik_token_auth_key != None: x = UserAqWidget.objects.select_related("AqWidget").filter(page=this_page,organization=request.organization).order_by('sort_order') widgets = UserAqWidget.prep(x,request,None) z = AqWidget.objects.filter(widget_type=2) widget_list = AqWidget.prep(z) return AQ_render_to_response(request, 'live/dashboard.html', { 'widgets':widgets, 'widget_list':widget_list, 'current_page':this_page, 'has_piwik':1, }, context_instance=RequestContext(request)) else: return AQ_render_to_response(request, 'live/signup.html', { 'has_piwik':0, }, context_instance=RequestContext(request))
def publisher_reports(request): from forms import ReportFormPublisher from atrinsic.base.models import Organization, AqWidget, UserAqWidget from reports import * inits = {} inits = { 'start_date': request.GET.get('start_date', None), 'end_date': request.GET.get('end_date', None), 'group_by': request.GET.get('group_by', 0), 'run_reporting_by': request.GET.get('run_reporting_by', 0), 'report_type': request.GET.get('report_type', 0), 'specific_advertiser': request.GET.get('specific_advertiser'), 'advertiser_category': request.GET.getlist('advertiser_category'), } this_page = 'publisher-reports' widget_report = None report = None refilter = 0 if request.GET: form = ReportFormPublisher(request.organization, inits) if form.is_valid(): refilter = 1 date_start, date_end = time_period(form) if date_start == None: date_start = datetime.datetime.now() if date_end == None: date_end = datetime.datetime.now() all_advertisers = Organization.objects.filter( publisher_relationships__status=RELATIONSHIP_ACCEPTED, publisher_relationships__publisher=request.organization) aids = [] if form.cleaned_data["run_reporting_by"] == '0': # all advertisers aids = [j.id for j in all_advertisers] elif form.cleaned_data[ "run_reporting_by"] == '1': # specific advertiser abc = form.cleaned_data["specific_advertiser"] aids.append(int(form.cleaned_data["specific_advertiser"][2:])) elif form.cleaned_data[ "run_reporting_by"] == '2': # advertiser by category aids = [] for j in all_advertisers: for site in j.website_set.all(): if site.vertical in form.cleaned_data[ "advertiser_category"]: aids.append(j.id) z, created = UserAqWidget.objects.get_or_create( page=this_page, zone=4, organization=request.organization, widget=AqWidget.objects.get( pk=form.cleaned_data["report_type"])) z.custom_date_range = date_start.strftime( '%m/%d/%Y') + "," + date_end.strftime('%m/%d/%Y') z.save() widget = UserAqWidget.prep([z], request, aids) widget_report = widget[0] else: report = None form = ReportFormPublisher(request.organization) #if request.GET.get("target",None): #target = int(request.GET.get("target")) if request.GET.get("target", None) or int( request.GET['report_type']) == REPORTTYPE_ORDER_DETAIL: if int(request.GET['report_type']) == REPORTTYPE_ORDER_DETAIL: target = REPORTFORMAT_CSV else: target = int(request.GET.get("target")) if int(form.cleaned_data["report_type"]) == REPORTTYPE_SALES: report = DateReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_SALES, advertiser_set=aids, ) if form.cleaned_data[ "report_type"] == REPORTTYPE_SALES_BY_ADVERTISER: report = OrgDateReport( date_start, date_end, request.organization, spec=REPORTTYPE_SALES_BY_ADVERTISER, advertiser_set=aids, ) if form.cleaned_data["report_type"] == REPORTTYPE_REVENUE: report = RevenueReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_REVENUE, advertiser_set=aids, ) if form.cleaned_data[ "report_type"] == REPORTTYPE_REVENUE_BY_ADVERTISER: report = OrgRevenueReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_REVENUE_BY_ADVERTISER, advertiser_set=aids, ) if form.cleaned_data["report_type"] == REPORTTYPE_CREATIVE: report = CreativeReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_CREATIVE, advertiser_set=aids, ) if form.cleaned_data[ "report_type"] == REPORTTYPE_CREATIVE_BY_PROMO: report = PromoReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_CREATIVE_BY_PROMO, advertiser_set=aids, ) if form.cleaned_data["report_type"] == REPORTTYPE_ORDER_DETAIL: report = OrderReport( date_start, date_end, request.organization, spec=REPORTTYPE_ORDER_DETAIL, advertiser_set=aids, ) if form.cleaned_data["report_type"] == REPORTTYPE_ACCOUNTING: report = OrgDateReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_ACCOUNTING, advertiser_set=aids, ) """ if form.cleaned_data["report_type"] == REPORTTYPE_ORDER_DETAIL: report = OrderReport(date_start,date_end,request.organization,form.cleaned_data["group_by"],spec=REPORTTYPE_ORDER_DETAIL, publisher_set=pids, ) """ if form.cleaned_data["report_type"] == REPORTTYPE_ACCOUNTING: report = OrgDateReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_ACCOUNTING, publisher_set=pids, ) if report: if target == REPORTFORMAT_EXCEL: from atrinsic.util.xls import write_rows import tempfile file_id, file_path = tempfile.mkstemp() res = [[]] for row in report.RenderHeader(): res[0].append(row[0]) for row in report.RenderContents(): res.append(row) last_row = [] res.append([]) for row in report.RenderFooter(): last_row.append(row[1]) res.append(last_row) #return HttpResponse('test') write_rows(file_path, res) res = open(file_path).read() response = HttpResponse( res, mimetype="application/vnd.ms-excel") response[ 'Content-Disposition'] = 'attachment; filename=report.xls' return response """ The foloowing is the old way of doing it. """ #response = HttpResponse(render_to_string("misc/reports/download.xls",{"report":report}),mimetype="application/vnd.ms-excel") #response['Content-Disposition'] = 'attachment; filename=report.xls' return response elif target == REPORTFORMAT_CSV: response = HttpResponse(render_to_string( "misc/reports/download.csv", {"report": report}), mimetype="text/csv") response[ 'Content-Disposition'] = 'attachment; filename=report.csv' return response elif target == REPORTFORMAT_TSV: response = HttpResponse( render_to_string("misc/reports/download.txt", {"report": report}), mimetype="application/octet-stream") response[ 'Content-Disposition'] = 'attachment; filename=report.txt' return response else: form = ReportFormPublisher(request.organization) return AQ_render_to_response( request, "publisher/reports/index.html", { "form": form, 'refilter': refilter, "widget": widget_report, "reporting": 1, "url": 'publisher/reports/download' #request.META['REQUEST_URI'].replace('publisher/reports','publisher/reports/download'), }, context_instance=RequestContext(request))
def publisher_dashboard(request): from atrinsic.base.models import AqWidget, Organization, PublisherVertical, UserAqWidget, W9Status from forms import DashboardSettingsForm, w9UploadForm from atrinsic.util.AceApi import create_company this_page = 'publisher-dashboard' all_advertisers = Organization.objects.filter( publisher_relationships__status=RELATIONSHIP_ACCEPTED, publisher_relationships__publisher=request.organization) aids = [] aids = [j.id for j in all_advertisers] x = UserAqWidget.objects.select_related("AqWidget").filter( page=this_page, organization=request.organization).order_by('sort_order') widgets = UserAqWidget.prep(x, request, aids) z = AqWidget.objects.filter(widget_type__in=[1, 3], Active=1) widget_list = AqWidget.prep(z) inbox = request.organization.received_messages.filter( is_active=True).order_by('-date_sent') # Check W9 Status form, if record doesnt exist, or set to Not Received # pass bool to page to display warning Lightbox showW9Warning = False #if request.organization.country != None and request.organization.country.lower().find("us") > -1: try: wNine = W9Status.objects.get(organization=request.organization) except: wNine = W9Status.objects.create(organization=request.organization, status=W9_STATUS_NOT_RECEIVED, datereceived=datetime.datetime.now()) if wNine.status != W9_STATUS_NOT_RECEIVED: showW9Warning = False uploadForm = "" else: showW9Warning = True uploadForm = "" if request.organization.ace_id == None: create_company(request.organization) hashed_ACEID = (int(request.organization.ace_id) + 148773) * 12 return AQ_render_to_response( request, 'publisher/dashboard.html', { 'verticals': PublisherVertical.objects.filter( is_adult=request.organization.is_adult).order_by('order'), 'widgets': widgets, 'widget_list': widget_list, 'current_page': this_page, 'msgcount': inbox, 'settings': True, 'sdate': request.GET.get('start_date', ''), 'edate': request.GET.get('end_date', ''), 'showW9Warning': showW9Warning, 'wNineForm': w9UploadForm(), 'w9Link': settings.W9_PATH, 'hashed_ACEID': hashed_ACEID, #'w9Up' : W9UploadForm() }, context_instance=RequestContext(request))
def publisher_reports(request): from forms import ReportFormPublisher from atrinsic.base.models import Organization, AqWidget, UserAqWidget from reports import * inits = {} inits = { "start_date": request.GET.get("start_date", None), "end_date": request.GET.get("end_date", None), "group_by": request.GET.get("group_by", 0), "run_reporting_by": request.GET.get("run_reporting_by", 0), "report_type": request.GET.get("report_type", 0), "specific_advertiser": request.GET.get("specific_advertiser"), "advertiser_category": request.GET.getlist("advertiser_category"), } this_page = "publisher-reports" widget_report = None report = None refilter = 0 if request.GET: form = ReportFormPublisher(request.organization, inits) if form.is_valid(): refilter = 1 date_start, date_end = time_period(form) if date_start == None: date_start = datetime.datetime.now() if date_end == None: date_end = datetime.datetime.now() all_advertisers = Organization.objects.filter( publisher_relationships__status=RELATIONSHIP_ACCEPTED, publisher_relationships__publisher=request.organization, ) aids = [] if form.cleaned_data["run_reporting_by"] == "0": # all advertisers aids = [j.id for j in all_advertisers] elif form.cleaned_data["run_reporting_by"] == "1": # specific advertiser abc = form.cleaned_data["specific_advertiser"] aids.append(int(form.cleaned_data["specific_advertiser"][2:])) elif form.cleaned_data["run_reporting_by"] == "2": # advertiser by category aids = [] for j in all_advertisers: for site in j.website_set.all(): if site.vertical in form.cleaned_data["advertiser_category"]: aids.append(j.id) z, created = UserAqWidget.objects.get_or_create( page=this_page, zone=4, organization=request.organization, widget=AqWidget.objects.get(pk=form.cleaned_data["report_type"]), ) z.custom_date_range = date_start.strftime("%m/%d/%Y") + "," + date_end.strftime("%m/%d/%Y") z.save() widget = UserAqWidget.prep([z], request, aids) widget_report = widget[0] else: report = None form = ReportFormPublisher(request.organization) # if request.GET.get("target",None): # target = int(request.GET.get("target")) if request.GET.get("target", None) or int(request.GET["report_type"]) == REPORTTYPE_ORDER_DETAIL: if int(request.GET["report_type"]) == REPORTTYPE_ORDER_DETAIL: target = REPORTFORMAT_CSV else: target = int(request.GET.get("target")) if int(form.cleaned_data["report_type"]) == REPORTTYPE_SALES: report = DateReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_SALES, advertiser_set=aids, ) if form.cleaned_data["report_type"] == REPORTTYPE_SALES_BY_ADVERTISER: report = OrgDateReport( date_start, date_end, request.organization, spec=REPORTTYPE_SALES_BY_ADVERTISER, advertiser_set=aids ) if form.cleaned_data["report_type"] == REPORTTYPE_REVENUE: report = RevenueReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_REVENUE, advertiser_set=aids, ) if form.cleaned_data["report_type"] == REPORTTYPE_REVENUE_BY_ADVERTISER: report = OrgRevenueReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_REVENUE_BY_ADVERTISER, advertiser_set=aids, ) if form.cleaned_data["report_type"] == REPORTTYPE_CREATIVE: report = CreativeReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_CREATIVE, advertiser_set=aids, ) if form.cleaned_data["report_type"] == REPORTTYPE_CREATIVE_BY_PROMO: report = PromoReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_CREATIVE_BY_PROMO, advertiser_set=aids, ) if form.cleaned_data["report_type"] == REPORTTYPE_ORDER_DETAIL: report = OrderReport( date_start, date_end, request.organization, spec=REPORTTYPE_ORDER_DETAIL, advertiser_set=aids ) if form.cleaned_data["report_type"] == REPORTTYPE_ACCOUNTING: report = OrgDateReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_ACCOUNTING, advertiser_set=aids, ) """ if form.cleaned_data["report_type"] == REPORTTYPE_ORDER_DETAIL: report = OrderReport(date_start,date_end,request.organization,form.cleaned_data["group_by"],spec=REPORTTYPE_ORDER_DETAIL, publisher_set=pids, ) """ if form.cleaned_data["report_type"] == REPORTTYPE_ACCOUNTING: report = OrgDateReport( date_start, date_end, request.organization, form.cleaned_data["group_by"], spec=REPORTTYPE_ACCOUNTING, publisher_set=pids, ) if report: if target == REPORTFORMAT_EXCEL: from atrinsic.util.xls import write_rows import tempfile file_id, file_path = tempfile.mkstemp() res = [[]] for row in report.RenderHeader(): res[0].append(row[0]) for row in report.RenderContents(): res.append(row) last_row = [] res.append([]) for row in report.RenderFooter(): last_row.append(row[1]) res.append(last_row) # return HttpResponse('test') write_rows(file_path, res) res = open(file_path).read() response = HttpResponse(res, mimetype="application/vnd.ms-excel") response["Content-Disposition"] = "attachment; filename=report.xls" return response """ The foloowing is the old way of doing it. """ # response = HttpResponse(render_to_string("misc/reports/download.xls",{"report":report}),mimetype="application/vnd.ms-excel") # response['Content-Disposition'] = 'attachment; filename=report.xls' return response elif target == REPORTFORMAT_CSV: response = HttpResponse( render_to_string("misc/reports/download.csv", {"report": report}), mimetype="text/csv" ) response["Content-Disposition"] = "attachment; filename=report.csv" return response elif target == REPORTFORMAT_TSV: response = HttpResponse( render_to_string("misc/reports/download.txt", {"report": report}), mimetype="application/octet-stream", ) response["Content-Disposition"] = "attachment; filename=report.txt" return response else: form = ReportFormPublisher(request.organization) return AQ_render_to_response( request, "publisher/reports/index.html", { "form": form, "refilter": refilter, "widget": widget_report, "reporting": 1, "url": "publisher/reports/download", # request.META['REQUEST_URI'].replace('publisher/reports','publisher/reports/download'), }, context_instance=RequestContext(request), )
def advertiser_reports(request): from atrinsic.base.models import Organization,PublisherGroup,UserAqWidget,AqWidget, Report_Adv_Pub from forms import ReportForm inits = {} inits = { 'start_date':request.GET.get('start_date',None), 'end_date':request.GET.get('end_date',None), 'group_by':request.GET.get('group_by',0), 'run_reporting_by':request.GET.get('run_reporting_by',0), 'report_type':request.GET.get('report_type',0), 'specific_advertiser':request.GET.get('specific_advertiser',None), 'advertiser_category':request.GET.get('advertiser_category',None), 'run_reporting_by_publisher':request.GET.getlist('run_reporting_by_publisher'), 'run_reporting_by_vertical':request.GET.getlist('run_reporting_by_vertical'), 'run_reporting_by_group':request.GET.getlist('run_reporting_by_group'), } this_page = 'advertiser-reports' form = ReportForm(request.organization,inits) refilter=0 widget_report = None if request.GET: if form.is_valid(): refilter=1 date_start,date_end = time_period(form) if date_start == None: date_start = datetime.datetime.now() if date_end == None: date_end = datetime.datetime.now() all_publishers = Organization.objects.filter(advertiser_relationships__status=RELATIONSHIP_ACCEPTED, advertiser_relationships__advertiser=request.organization) pids = [] if form.cleaned_data["run_reporting_by"] == '0': # all publishers pids = [j.id for j in all_publishers] elif form.cleaned_data["run_reporting_by"] == '3': # specific publisher if inits.has_key("run_reporting_by_publisher"): pids.extend([int(x[2:]) for x in inits["run_reporting_by_publisher"]]) else: pids = [] for j in all_publishers: pids.append(j.id) elif form.cleaned_data["run_reporting_by"] == '2': # publisher by category pids = [] for j in all_publishers: for site in j.website_set.all(): try: if site.vertical in form.cleaned_data["run_reporting_by_vertical"]: pids.append(j.id) except: print ' - web site with bad vertical: %s -- %s' % (site,site.id) elif form.cleaned_data["run_reporting_by"] == '1': # publisher by group for g_id in form.cleaned_data["run_reporting_by_group"]: group = PublisherGroup.objects.get(id=int(g_id[2:])) pids.extend([g.id for g in group.publishers.all()]) z,created = UserAqWidget.objects.get_or_create(page=this_page,zone=4,organization=request.organization, widget=AqWidget.objects.get(pk=form.cleaned_data["report_type"])) z.custom_date_range = date_start.strftime('%m/%d/%Y')+","+date_end.strftime('%m/%d/%Y') z.save() widget = UserAqWidget.prep([z],request,pids) widget_report = widget[0] else: report = None form = ReportForm(request.organization) if request.GET.get("target",None) or int(request.GET['report_type']) == REPORTTYPE_ORDER_DETAIL: if int(request.GET['report_type']) == REPORTTYPE_ORDER_DETAIL: target = REPORTFORMAT_CSV else: target = int(request.GET.get("target")) from AqWidgets import QuickReports if int(form.cleaned_data["report_type"]) == REPORTTYPE_SALES: report = DateReport(date_start,date_end,request.organization,group_by=form.cleaned_data["group_by"], spec=REPORTTYPE_SALES, publisher_set=pids) if form.cleaned_data["report_type"] == REPORTTYPE_SALES_BY_PUBLISHER: report = OrgDateReport(date_start,date_end,request.organization,group_by=form.cleaned_data["group_by"], spec=REPORTTYPE_SALES_BY_PUBLISHER, publisher_set=pids) if form.cleaned_data["report_type"] == REPORTTYPE_REVENUE: report = RevenueReport(date_start,date_end,request.organization,group_by=form.cleaned_data["group_by"], spec=REPORTTYPE_REVENUE, publisher_set=pids) if form.cleaned_data["report_type"] == REPORTTYPE_REVENUE_BY_PUBLISHER: report = OrgRevenueReport(date_start,date_end,request.organization,group_by=form.cleaned_data["group_by"], spec=REPORTTYPE_REVENUE_BY_PUBLISHER, publisher_set=pids) if form.cleaned_data["report_type"] == REPORTTYPE_CREATIVE: report = CreativeReport(date_start,date_end,request.organization,group_by=form.cleaned_data["group_by"], spec=REPORTTYPE_CREATIVE, publisher_set=pids) if form.cleaned_data["report_type"] == REPORTTYPE_CREATIVE_BY_PROMO: report = PromoReport(date_start,date_end,request.organization,group_by=form.cleaned_data["group_by"], spec=REPORTTYPE_CREATIVE_BY_PROMO, publisher_set=pids) if form.cleaned_data["report_type"] == REPORTTYPE_ORDER_DETAIL: report = OrderReport(date_start,date_end,request.organization,group_by=form.cleaned_data["group_by"], spec=REPORTTYPE_ORDER_DETAIL, publisher_set=pids) if form.cleaned_data["report_type"] == REPORTTYPE_ACCOUNTING: report = AccountingReport(date_start,date_end,request.organization,group_by=form.cleaned_data["group_by"], spec=REPORTTYPE_ACCOUNTING, publisher_set=pids) if target == REPORTFORMAT_EXCEL: from atrinsic.util.xls import write_rows import tempfile file_id,file_path = tempfile.mkstemp() res = [[]] for row in report.RenderHeader(): res[0].append(row[0]) for row in report.RenderContents(): res.append(row) last_row = [] res.append([]) for row in report.RenderFooter(): last_row.append(row[1]) res.append(last_row) #return HttpResponse(str(res)) write_rows(file_path,res) res = open(file_path).read() #return HttpResponse(str(res)) response = HttpResponse(res,mimetype="application/vnd.ms-excel") response['Content-Disposition'] = 'attachment; filename=download.xls' return response """ Old way of doing it""" response = render_to_response("misc/reports/dataxls.html", {'report': report,}) filename = "misc/reports/download.xls" response['Content-Disposition'] = 'attachment; filename='+filename response['Content-Type'] = 'application/vnd.ms-excel; charset=utf-8' return response elif target == REPORTFORMAT_CSV: response = HttpResponse(render_to_string("misc/reports/download.csv",{"report":report}),mimetype="text/csv") response['Content-Disposition'] = 'attachment; filename=report.csv' return response elif target == REPORTFORMAT_TSV: response = HttpResponse(render_to_string("misc/reports/download.txt",{"report":report}),mimetype="application/octet-stream") response['Content-Disposition'] = 'attachment; filename=report.txt' return response return AQ_render_to_response(request, "advertiser/reports/index.html",{ "form":form, 'refilter':refilter, "widget":widget_report, "reporting":1, "url": 'advertiser/reports/download'#request.META['REQUEST_URI'].replace('advertiser/reports','advertiser/reports/download'), },context_instance=RequestContext(request))