def fill_user_initial_data(vosae_user, language_code): """ This task creates inital data when a new :class:`~core.models.VosaeUser` is created """ from organizer.models import VosaeCalendar, CalendarList, CalendarAclRule, VosaeEvent, EventDateTime with respect_language(language_code): # Creates a personal Calendar and an associated CalendarList calendar = VosaeCalendar(tenant=vosae_user.tenant, summary=_('My calendar')) calendar.acl.rules.append(CalendarAclRule(principal=vosae_user, role=u'OWNER')) calendar.save() CalendarList( tenant=vosae_user.tenant, vosae_user=vosae_user, calendar=calendar, selected=True ).save() # Creates an Event VosaeEvent( tenant=vosae_user.tenant, calendar=calendar, summary=_('I joined %(tenant)s on Vosae') % {'tenant': vosae_user.tenant}, description=_('This is the most amazing day of my life, I joined %(tenant)s on Vosae') % {'tenant': vosae_user.tenant.name}, creator=vosae_user, organizer=vosae_user, start=EventDateTime(date=date.today()), end=EventDateTime(date=date.today()) ).save()
def export_documents(export): from contacts.imex import get_exportable_documents as contacts_documents from invoicing.imex import get_exportable_documents as invoicing_documents with respect_language(export.language): archive_name = _('Vosae export.zip') zipped = ContentFile('', archive_name) f = zipfile.ZipFile(zipped, mode='w', compression=zipfile.ZIP_DEFLATED) for documents, path_func, doc_func in contacts_documents(export): for document in documents: f.writestr(path_func(document), doc_func(document)) for documents, path_func, doc_func in invoicing_documents(export): for document in documents: f.writestr(path_func(document), doc_func(document)) f.close() zipped.content_type = "application/zip" export.zipfile = VosaeFile( tenant=export.tenant, uploaded_file=zipped, issuer=export.issuer ) export.zipfile.save() export.update(set__zipfile=export.zipfile) context = { 'tenant': export.tenant, 'file': export.zipfile, 'site': {'name': settings.SITE_NAME, 'url': settings.SITE_URL} } # Email to issuer subject = _('Your Vosae export is available') message = render_to_string('data_liberation/emails/export_finished.txt', context) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [export.issuer.email])
def fill_tenant_initial_data(tenant, language_code): """ This task creates inital data when a new :class:`~core.models.Tenant` is created """ from core.models import VosaeUser from contacts.models import Contact, Organization, Email, Address from invoicing.models import Item, Tax, Currency with respect_language(language_code): initial_vosae_user = VosaeUser.objects.get(tenant=tenant) # Creates default taxes, based on registration country for tax_percent, tax_name in tenant.registration_info.DEFAULT_TAXES: Tax(tenant=tenant, name=tax_name, rate=tax_percent).save() # Creates a shared organization Organization( tenant=tenant, creator=initial_vosae_user, corporate_name=u'Vosae', emails=[ Email(type=u'WORK', email=u'*****@*****.**') ], addresses=[ Address(type=u'WORK', street_address=u'35 Rue Lesdiguières', postal_code=u'38000', city=u'Grenoble', country=u'France') ], note=_('We\'d like to make it right'), private=False ).save() # Creates a shared contact Contact( tenant=tenant, creator=initial_vosae_user, name=_('Support'), firstname=u'Vosae', role=_('The best support'), emails=[ Email(type=u'WORK', email=u'*****@*****.**') ], organization=Organization.objects.get(tenant=tenant, corporate_name=u'Vosae'), private=False ).save() # Creates an item Item( tenant=tenant, reference=_('TEST-ITEM'), description=_('Test item'), unit_price=Decimal('1000'), currency=Currency.objects.get(symbol='EUR'), tax=Tax.objects.filter(tenant=tenant).first(), type=u'SERVICE' ).save()
def export_documents(export): from contacts.imex import get_exportable_documents as contacts_documents from invoicing.imex import get_exportable_documents as invoicing_documents with respect_language(export.language): archive_name = _('Vosae export.zip') zipped = ContentFile('', archive_name) f = zipfile.ZipFile(zipped, mode='w', compression=zipfile.ZIP_DEFLATED) for documents, path_func, doc_func in contacts_documents(export): for document in documents: f.writestr(path_func(document), doc_func(document)) for documents, path_func, doc_func in invoicing_documents(export): for document in documents: f.writestr(path_func(document), doc_func(document)) f.close() zipped.content_type = "application/zip" export.zipfile = VosaeFile(tenant=export.tenant, uploaded_file=zipped, issuer=export.issuer) export.zipfile.save() export.update(set__zipfile=export.zipfile) context = { 'tenant': export.tenant, 'file': export.zipfile, 'site': { 'name': settings.SITE_NAME, 'url': settings.SITE_URL } } # Email to issuer plaintext_context = Context( autoescape=False) # HTML escaping not appropriate in plaintext subject = subject = _("Your Vosae export is available") text_body = render_to_string( 'data_liberation/emails/export_finished.txt', context, plaintext_context) html_body = render_to_string( "data_liberation/emails/export_finished.html", context) message = EmailMultiAlternatives( subject=subject, from_email=settings.DEFAULT_FROM_EMAIL, to=[export.issuer.email], body=text_body) message.attach_alternative(html_body, "text/html") message.send()
def get_pdf(self, issuer=None, language=None): """Return the cached PDF or generate and cache it.""" from core.models import VosaeFile try: pdf_language = language or self.tenant.report_settings.language except: pdf_language = 'en' if not self.current_revision.pdf.get(pdf_language, None): with respect_language(pdf_language): buf = self.gen_pdf() pdf = ContentFile(buf.getvalue(), self.filename) pdf.content_type = "application/pdf" self.current_revision.pdf[pdf_language] = VosaeFile( tenant=self.tenant, uploaded_file=pdf, issuer=issuer ) self.current_revision.pdf[pdf_language].save() self.update(set__current_revision__pdf=self.current_revision.pdf) return self.current_revision.pdf[pdf_language]
def export_documents(export): from contacts.imex import get_exportable_documents as contacts_documents from invoicing.imex import get_exportable_documents as invoicing_documents with respect_language(export.language): archive_name = _('Vosae export.zip') zipped = ContentFile('', archive_name) f = zipfile.ZipFile(zipped, mode='w', compression=zipfile.ZIP_DEFLATED) for documents, path_func, doc_func in contacts_documents(export): for document in documents: f.writestr(path_func(document), doc_func(document)) for documents, path_func, doc_func in invoicing_documents(export): for document in documents: f.writestr(path_func(document), doc_func(document)) f.close() zipped.content_type = "application/zip" export.zipfile = VosaeFile( tenant=export.tenant, uploaded_file=zipped, issuer=export.issuer ) export.zipfile.save() export.update(set__zipfile=export.zipfile) context = { 'tenant': export.tenant, 'file': export.zipfile, 'site': {'name': settings.SITE_NAME, 'url': settings.SITE_URL} } # Email to issuer plaintext_context = Context(autoescape=False) # HTML escaping not appropriate in plaintext subject = subject = _("Your Vosae export is available") text_body = render_to_string('data_liberation/emails/export_finished.txt', context, plaintext_context) html_body = render_to_string("data_liberation/emails/export_finished.html", context) message = EmailMessage(subject=subject, from_email=settings.DEFAULT_FROM_EMAIL, to=[export.issuer.email], body=text_body) message.attach_alternative(html_body, "text/html") message.send()
def emit_reminders(vosae_event_id): from organizer.models import CalendarList, VosaeEvent from notification import models as notifications try: vosae_event = VosaeEvent.objects.get(id=vosae_event_id) except: # Event retrieval failed (maybe deleted in the meantime) return None calendar_lists = CalendarList.objects.filter(calendar=vosae_event.calendar) reminders = {'EMAIL': {}, 'POPUP': set([])} # Event reminders for method in set([ reminder.method for reminder in vosae_event.reminders.overrides if reminder.minutes == vosae_event.next_reminder.threshold ]): if method == 'EMAIL': vosae_users = [ calendar_list.vosae_user for calendar_list in calendar_lists ] for vosae_user in vosae_users: user_lang = vosae_user.get_language() if not user_lang in reminders['EMAIL']: reminders['EMAIL'][user_lang] = set([]) reminders['EMAIL'][user_lang].update([vosae_user.email]) elif method == 'POPUP': reminders['POPUP'].update([ unicode(calendar_list.vosae_user.id) for calendar_list in calendar_lists ]) # Default reminders if vosae_event.reminders.use_default: for calendar_list in calendar_lists: for method in set([ reminder.method for reminder in calendar_list.reminders if reminder.minutes == vosae_event.next_reminder.threshold ]): if method == 'EMAIL': user_lang = calendar_list.vosae_user.get_language() if not user_lang in reminders['EMAIL']: reminders['EMAIL'][user_lang] = set([]) reminders['EMAIL'][user_lang].update( [calendar_list.vosae_user.email]) elif method == 'POPUP': reminders['POPUP'].update( [unicode(calendar_list.vosae_user.id)]) # Prefill context timezone = vosae_event.get_start_timezone() start = vosae_event._get_next_occurrence( after=vosae_event.next_reminder.at).astimezone(timezone) context = { 'vosae_event': vosae_event, 'start': start.replace(tzinfo=None), 'timezone': start.tzname(), 'event_url': u'{0}/{1}/organizer/event/{2}'.format(settings.WEB_ENDPOINT, vosae_event.tenant.slug, unicode(vosae_event.id)) } # Schedule next reminder vosae_event._set_next_reminder() vosae_event.update(set__next_reminder=vosae_event.next_reminder) vosae_event.check_immediate_reminders_emit() # Saves notifications for recipient in reminders['POPUP']: notifications.EventReminder(tenant=vosae_event.tenant, recipient=DBRef('vosae_user', ObjectId(recipient)), vosae_event=vosae_event, occurs_at=start, summary=vosae_event.summary).save() # Send mails connection = get_connection() for language, emails in reminders['EMAIL'].items(): with respect_language(language): plaintext_context = Context( autoescape=False) # HTML escaping not appropriate in plaintext subject = render_to_string( 'organizer/emails/organizer_reminder_subject.txt', context, plaintext_context) subject = ''.join(subject.splitlines()) text_body = render_to_string( 'organizer/emails/organizer_reminder_content.txt', context, plaintext_context) html_body = render_to_string( 'organizer/emails/organizer_reminder_content.html', context) message = EmailMultiAlternatives( subject, text_body, cc=emails, connection=connection ) # Mandrill doesn't support BCC so we have to use CC without preserving recipients message.attach_alternative(html_body, "text/html") message.preserve_recipients = False # Useful for Mandrill message.send()
def emit_reminders(vosae_event_id): from organizer.models import CalendarList, VosaeEvent from notification import models as notifications try: vosae_event = VosaeEvent.objects.get(id=vosae_event_id) except: # Event retrieval failed (maybe deleted in the meantime) return None calendar_lists = CalendarList.objects.filter(calendar=vosae_event.calendar) reminders = { 'EMAIL': {}, 'POPUP': set([]) } # Event reminders for method in set([reminder.method for reminder in vosae_event.reminders.overrides if reminder.minutes == vosae_event.next_reminder.threshold]): if method == 'EMAIL': vosae_users = [calendar_list.vosae_user for calendar_list in calendar_lists] for vosae_user in vosae_users: user_lang = vosae_user.get_language() if not user_lang in reminders['EMAIL']: reminders['EMAIL'][user_lang] = set([]) reminders['EMAIL'][user_lang].update([vosae_user.email]) elif method == 'POPUP': reminders['POPUP'].update([unicode(calendar_list.vosae_user.id) for calendar_list in calendar_lists]) # Default reminders if vosae_event.reminders.use_default: for calendar_list in calendar_lists: for method in set([reminder.method for reminder in calendar_list.reminders if reminder.minutes == vosae_event.next_reminder.threshold]): if method == 'EMAIL': user_lang = calendar_list.vosae_user.get_language() if not user_lang in reminders['EMAIL']: reminders['EMAIL'][user_lang] = set([]) reminders['EMAIL'][user_lang].update([calendar_list.vosae_user.email]) elif method == 'POPUP': reminders['POPUP'].update([unicode(calendar_list.vosae_user.id)]) # Prefill context timezone = vosae_event.get_start_timezone() start = vosae_event._get_next_occurrence(after=vosae_event.next_reminder.at).astimezone(timezone) context = { 'vosae_event': vosae_event, 'start': start.replace(tzinfo=None), 'timezone': start.tzname(), 'event_url': u'{0}/{1}/organizer/event/{2}'.format(settings.WEB_ENDPOINT, vosae_event.tenant.slug, unicode(vosae_event.id)) } # Schedule next reminder vosae_event._set_next_reminder() vosae_event.update(set__next_reminder=vosae_event.next_reminder) vosae_event.check_immediate_reminders_emit() # Saves notifications for recipient in reminders['POPUP']: notifications.EventReminder( tenant=vosae_event.tenant, recipient=DBRef('vosae_user', ObjectId(recipient)), vosae_event=vosae_event, occurs_at=start, summary=vosae_event.summary ).save() # Send mails connection = get_connection() for language, emails in reminders['EMAIL'].items(): with respect_language(language): plaintext_context = Context(autoescape=False) # HTML escaping not appropriate in plaintext subject = render_to_string('organizer/emails/organizer_reminder_subject.txt', context, plaintext_context) subject = ''.join(subject.splitlines()) text_body = render_to_string('organizer/emails/organizer_reminder_content.txt', context, plaintext_context) html_body = render_to_string('organizer/emails/organizer_reminder_content.html', context) message = EmailMultiAlternatives(subject, text_body, cc=emails, connection=connection) # Mandrill doesn't support BCC so we have to use CC without preserving recipients message.attach_alternative(html_body, "text/html") message.preserve_recipients = False # Useful for Mandrill message.send()