Esempio n. 1
0
def email_invite(request, party_id):
    party = get_object_or_404(Party, id=party_id)

    if request.method == "POST":
        form = EmailInviteForm(request.POST)
        if form.is_valid():
            with transaction.commit_on_success():
                party.invite_type = "email"  # 将邀请方式修改为email
                party.save()

                email_message, created = EmailMessage.objects.get_or_create(
                    party=party,
                    defaults={
                        "subject": u"[爱热闹]您收到一个活动邀请",
                        "content": form.cleaned_data["content"],
                        "is_apply_tips": form.cleaned_data["is_apply_tips"],
                    },
                )
                if not created:
                    email_message.subject = u"[爱热闹]您收到一个活动邀请"
                    email_message.content = form.cleaned_data["content"]
                    email_message.is_apply_tips = form.cleaned_data["is_apply_tips"]
                    email_message.save()

                client_email_list = form.cleaned_data["client_email_list"].split(",")
                parties_clients = PartiesClients.objects.select_related("client").filter(party=party)
                clients = Client.objects.filter(creator=request.user)

                for email in client_email_list:
                    client_temp = None
                    for client in clients:
                        if client.email == email:
                            client_temp = client
                            break

                    if not client_temp:
                        client_temp = Client.objects.create(
                            creator=request.user, name=email, email=email, invite_type="email"
                        )

                    party_client_temp = None
                    for party_client in parties_clients:
                        if party_client.client == client_temp:
                            party_client_temp = party_client
                            break

                    if not party_client_temp:
                        party_client = PartiesClients.objects.create(party=party, client=client_temp)

            send_status = u"邮件发送失败"
            with transaction.commit_on_success():
                send_message = Outbox(address=form.cleaned_data["client_email_list"], base_message=email_message)
                send_message.save()
                send_status = u"邮件发送成功"

            request.session["send_status"] = send_status
            return redirect("list_party")
        else:
            client_data = []
            for client in Client.objects.filter(creator=request.user):
                if client.email:
                    client_data.append(client.email)
            return TemplateResponse(
                request,
                "parties/email_invite.html",
                {"form": form, "party": party, "client_data": simplejson.dumps(client_data)},
            )
    else:
        apply_status = request.GET.get("apply", "all")
        if apply_status == "all":
            clients = PartiesClients.objects.filter(party=party_id).exclude(client__invite_type="public")
        else:
            clients = (
                PartiesClients.objects.filter(party=party_id)
                .filter(apply_status=apply_status)
                .exclude(client__invite_type="public")
            )

        if clients:
            client_email_list = []
            for client in clients:
                client_email_list.append(client.client.email)
            client_email_list = ",".join(client_email_list)

            email_message = EmailMessage.objects.get(party=party)

            data = {
                "client_email_list": client_email_list,
                "content": email_message.content,
                "is_apply_tips": email_message.is_apply_tips,
            }
            form = EmailInviteForm(initial=data)
        else:
            # 生成默认内容
            content = party.creator.username + u"邀请你参加:"
            if party.start_time == None and party.address == "":
                content = content + party.description + u",时间、地点,另行通知。"
            elif party.start_time != None and party.address == u"":
                content = (
                    content
                    + datetime.datetime.strftime(party.start_time, "%Y-%m-%d %H:%M")
                    + party.description
                    + u",地点另行通知。"
                )
            elif party.start_time == None and party.address != "":
                content = content + u"在" + party.address + party.description + u",时间待定。"
            else:
                content = (
                    content
                    + datetime.time.strftime(party.start_time, "%Y-%m-%d %H:%M")
                    + u" ,在"
                    + party.address
                    + u"的活动"
                    + party.description
                )

            data = {"client_email_list": "", "content": content, "is_apply_tips": True}
            form = EmailInviteForm(initial=data)
        client_data = []
        for client in Client.objects.filter(creator=request.user):
            if client.email:
                client_data.append(client.email)
        return TemplateResponse(
            request,
            "parties/email_invite.html",
            {"form": form, "party": party, "client_data": simplejson.dumps(client_data)},
        )
Esempio n. 2
0
def email_invite(request, party_id):
    party = get_object_or_404(Party, id = party_id, creator = request.user)
    #取得最近20个活动,用来从中获取好友
    recent_parties = Party.objects.filter(invite_type = 'email').filter(creator = request.user).exclude(id = party.id).order_by('-created_time')
    
    if request.method == 'POST':
        form = EmailInviteForm(request.POST)
        if form.is_valid():
            with transaction.commit_on_success():
                party.invite_type = 'email' #将邀请方式修改为email
                party.save()
                
                email_message, created = EmailMessage.objects.get_or_create(party = party,
                    defaults = {'subject': u'[爱热闹]您收到一个活动邀请', 'content': form.cleaned_data['content'], 'is_apply_tips': form.cleaned_data['is_apply_tips']})
                if not created:
                    email_message.subject = u'[爱热闹]您收到一个活动邀请'
                    email_message.content = form.cleaned_data['content']
                    email_message.is_apply_tips = form.cleaned_data['is_apply_tips']
                    email_message.save()
                
                client_email_list = form.cleaned_data['client_email_list'].split(',')
                parties_clients = PartiesClients.objects.select_related('client').filter(party = party)
                clients = Client.objects.filter(creator = request.user)
                
                for email in client_email_list:
                    client_temp = None
                    for client in clients:
                        if client.email == email:
                            client_temp = client
                            break
                    
                    if not client_temp:
                        client_temp = Client.objects.create(
                            creator = request.user,
                            #name = email,
                            email = email,
                            invite_type = 'email'
                        )
                    
                    party_client_temp = None
                    for party_client in parties_clients:
                        if party_client.client == client_temp:
                            party_client_temp = party_client
                            break
                    
                    if not party_client_temp:
                        party_client = PartiesClients.objects.create(
                            party = party,
                            client = client_temp
                        )
            
            send_status = 'email_fail'
            with transaction.commit_on_success():
                send_message = Outbox(address = form.cleaned_data['client_email_list'], base_message = email_message)
                send_message.save()
                send_status = 'email_success'
             
            request.session['send_status'] = send_status    
            return redirect('list_party')
        else:
            client_data = []
            for client in Client.objects.filter(creator = request.user):
                if client.email:
                    client_data.append(client.email)
            noanswer_client = []
            apply_client = []
            reject_client = []
            parties_clients = PartiesClients.objects.select_related('client').filter(party = party)
            for  party_client in parties_clients :
                if party_client.apply_status == 'noanswer':
                    noanswer_client.append(party_client.client.email)
                if party_client.apply_status == 'apply':
                    apply_client.append(party_client.client.email)
                if party_client.apply_status == 'reject':
                    reject_client.append(party_client.client.email)
            
            noanswer_client = ','.join(noanswer_client)
            apply_client = ','.join(apply_client)
            reject_client = ','.join(reject_client)
            quickadd_client = {'noanswer_client':noanswer_client,
                               'apply_client':apply_client,
                               'reject_client':reject_client
                               }
                      
            return TemplateResponse(request, 'parties/email_invite.html', {'form': form, 'party': party, 'client_data':simplejson.dumps(client_data), 'quickadd_client':quickadd_client, 'recent_parties':recent_parties})
    else:
        apply_status = request.GET.get('apply', 'all')
        if apply_status == 'all':
            clients = PartiesClients.objects.filter(party = party_id)
        else:
            clients = PartiesClients.objects.filter(party = party_id).filter(apply_status = apply_status)

        #生成默认内容
        userprofile = request.user.get_profile()
        creator = userprofile.true_name if userprofile.true_name else request.user.username  
        content = _create_default_content(creator, party.start_date, party.start_time , party.address, party.description)
       
        if clients:
            client_email_list = []
            for client in clients:
                client_email_list.append(client.client.email)
            client_email_list = ','.join(client_email_list)
            
            email_message = EmailMessage.objects.get(party = party)
            
            data = {
                'client_email_list': client_email_list,
                'content': content,
                'is_apply_tips' : email_message.is_apply_tips
            }
            form = EmailInviteForm(initial = data)
        else:
            data = {
                'client_email_list': '',
                'content': content,
                'is_apply_tips' : True
            }
            form = EmailInviteForm(initial = data)
        client_data = []
        for client in Client.objects.filter(creator = request.user):
            if client.email:
                client_data.append(client.email)
        noanswer_client = []
        apply_client = []
        reject_client = []
        parties_clients = PartiesClients.objects.select_related('client').filter(party = party)
        for  party_client in parties_clients :
            if party_client.apply_status == 'noanswer':
                noanswer_client.append(party_client.client.email)
            if party_client.apply_status == 'apply':
                apply_client.append(party_client.client.email)
            if party_client.apply_status == 'reject':
                reject_client.append(party_client.client.email)
        
        noanswer_client = ','.join(noanswer_client)
        apply_client = ','.join(apply_client)
        reject_client = ','.join(reject_client)
        quickadd_client = {'noanswer_client':noanswer_client,
                           'apply_client':apply_client,
                           'reject_client':reject_client
                           }
        return TemplateResponse(request, 'parties/email_invite.html', {'form': form, 'party': party, 'client_data':simplejson.dumps(client_data), 'quickadd_client':quickadd_client, 'recent_parties':recent_parties})