def save(self, subido_por): foto = self.validated_data['foto'] attachment = Attachment() attachment.subido_por = subido_por attachment.foto.save(foto.name, foto, save=False) attachment.save() return attachment
def handle(self, *args, **options): imaps = settings.IMAPS for imap in imaps: imapper = easyimap.connect(imap['host'], imap['user'], imap['pass'], imap['mailbox']) self.success('Loggueado como {}'.format(imap['user'])) if options['include_seen']: # read every email not present in the db imap_ids = {int(i) for i in imapper.listids()} known_ids = set(Email.objects.values_list('uid', flat=True)) unknown_ids = imap_ids - known_ids mails = (imapper.mail(str(i)) for i in unknown_ids) else: mails = imapper.unseen() # self.success('Se encontraron {} emails'.format(len(list(mails)))) for mail in mails: self.success('Email {}'.format(mail)) attachments = mail.attachments if not attachments: self.success(' ... sin adjuntos') continue email = Email.from_mail_object(mail) self.log(email) for attachment in attachments: # self.success(' -- attachment {}'.format(attachment[0])) if options['only_images'] and not attachment[2].startswith( 'image'): self.warning( f'Ignoring {attachment[0]} ({attachment[2]})') continue instance = Attachment(email=email, mimetype=attachment[2]) try: content = ContentFile(attachment[1]) instance.foto.save(attachment[0], content, save=False) instance.save() self.success(' -- saved {}'.format(instance)) self.log(instance) except IntegrityError: self.warning(f'{attachment[0]} ya está en el sistema')
def contadores(request): e = Eleccion.objects.first() return { 'adjuntos_count': Attachment.sin_asignar().count(), 'mesas_pendientes_count': Mesa.con_carga_pendiente().count(), 'mesas_a_confirmar_count': Mesa.con_carga_a_confirmar().count(), 'primera_eleccion': e.id if e is not None else 1 # las urls esperan un entero. # aunque no exista el objeto }
def handle(self, *args, **options): imapper = easyimap.connect( settings.IMAP_HOST, settings.IMAP_USERNAME, settings.IMAP_PASSWORD, settings.IMAP_MAILBOX ) self.success('Loggueado') if options['include_seen']: # read every email not present in the db imap_ids = {int(i) for i in imapper.listids()} known_ids = set(Email.objects.values_list('uid', flat=True)) unknown_ids = imap_ids - known_ids mails = (imapper.mail(str(i)) for i in unknown_ids) else: mails = imapper.unseen() for mail in mails: attachments = mail.attachments if not attachments: continue email = Email.from_mail_object(mail) self.log(email) for attachment in attachments: if options['only_images'] and not attachment[2].startswith('image'): self.warning(f'Ignoring {attachment[0]} ({attachment[2]})') continue instance = Attachment( email=email, mimetype=attachment[2] ) content = ContentFile(attachment[1]) instance.foto.save(attachment[0], content, save=False) instance.save() self.log(instance)
def cargar_informacion_adjunto(self, adjunto, subido_por, pre_identificacion=None): try: instance = Attachment(mimetype=adjunto.content_type) instance.foto.save(adjunto.name, adjunto, save=False) instance.subido_por = subido_por if pre_identificacion is not None: instance.pre_identificacion = pre_identificacion instance.save() return instance except IntegrityError: self.agregar_resultado_carga( messages.WARNING, f'El archivo {adjunto.name} ya fue subido con anterioridad. <br>' 'Verificá si era el que querías subir y, si lo era, ' 'no tenés que hacer nada.<br> ¡Gracias!') return None