def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs, gdoc_template_id, context=None):
     '''
     copy an existing document in google docs
        :param res_model: the object for which the google doc is created
        :param res_id: the Id of the object for which the google doc is created
        :param name_gdocs: the name of the future ir.attachment that will be created. Based on the google doc template foun.
        :param gdoc_template_id: the id of the google doc document to copy
        :return: the ID of the google document object created
     '''
     #login with the base account google module
     client = self._auth(cr, uid)
     # fetch and copy the original document
     try:
         original_resource = client.get_resource_by_id(gdoc_template_id)
         #copy the document you choose in the configuration
         copy_resource = client.copy_resource(original_resource, 'copy_%s' % original_resource.title.text)
     except:
         raise osv.except_osv(_('Google Docs Error!'), _("Your resource id is not correct. You can find the id in the google docs URL"))
     # create an ir.attachment
     self.create(cr, uid, {
         'res_model': res_model,
         'res_id': res_id,
         'type': 'url',
         'name': name_gdocs,
         'url': copy_resource.get_alternate_link().href
     }, context=context)
     return copy_resource.resource_id.text
 def copy_gdoc(self, cr, uid, res_model, res_id, name_gdocs, gdoc_template_id, context=None):
     '''
     copy an existing document in google docs
        :param res_model: the object for which the google doc is created
        :param res_id: the Id of the object for which the google doc is created
        :param name_gdocs: the name of the future ir.attachment that will be created. Based on the google doc template foun.
        :param gdoc_template_id: the id of the google doc document to copy
        :return: the ID of the google document object created
     '''
     #login with the base account google module
     client = self._auth(cr, uid)
     # fetch and copy the original document
     try:
         doc = client.get_resource_by_id(gdoc_template_id)
         #copy the document you choose in the configuration
         copy_resource = client.copy_resource(doc, name_gdocs)
     except:
         raise osv.except_osv(_('Google Docs Error!'), _("Your resource id is not correct. You can find the id in the google docs URL."))
     # create an ir.attachment
     self.create(cr, uid, {
         'res_model': res_model,
         'res_id': res_id,
         'type': 'url',
         'name': name_gdocs,
         'url': copy_resource.get_alternate_link().href
     }, context=context)
     return copy_resource.resource_id.text
    def handle(self, *args, **options):
        if not GOOGLE_DOCS_ACCOUNT and GOOGLE_DOCS_PASSWORD and GOOGLE_DOCS_RESOURCE_ID:
            raise CommandError('You must set both GOOGLE_DOCS_ACCOUNT, GOOGLE_DOCS_PASSWORD and GOOGLE_DOCS_RESOURCE_ID in your settings file.')
        verbosity = int(options.get('verbosity', 1))
        output_all = options.get('output_all')
        dry_run = options.get('dry_run')
        fields = ('email', 'first_name', 'last_name', 'phone', 'city', 'state', 'zipcode', 'is_a', 'broadcasters', 'date_created', 'share_info')

        profile_list = NonUserProfile.objects.order_by('-date_created')
        if not output_all:
            profile_list = profile_list.filter(share_info=True)

        if len(profile_list):
            if verbosity > 1:
                self.stdout.write('{0} signups to record.'.format(len(profile_list)))
            fp = NamedTemporaryFile(delete=False)
            writer = csv.DictWriter(fp, fields)
            writer.writeheader()

            for signup in profile_list:
                output = {
                    'email': signup.email,
                    'first_name': signup.first_name,
                    'last_name': signup.last_name,
                    'phone': signup.phone,
                    'city': signup.city,
                    'state': signup.state,
                    'zipcode': signup.zipcode,
                    'is_a': signup.is_a,
                    'date_created': signup.date_created.strftime('%m/%d/%Y %H:%M:%S'),
                    'share_info': signup.share_info
                }
                extra_fields_data = signup.extra_fields
                for extra_field in SIGNUP_EXTRA_FIELDS:
                    input_val = None
                    if isinstance(extra_fields_data[extra_field], list):
                        input_val = ', '.join(extra_fields_data[extra_field])
                    else:
                        input_val = extra_fields_data[extra_field]
                    output[extra_field] = input_val
                writer.writerow(output)
            if dry_run:
                self.stdout.write('Row created:\n{0}\n'.format('|'.join([str(output[f]) for f in fields])))
            del(writer)

            if not dry_run:
                client = gdata.docs.client.DocsClient()
                login_token = client.ClientLogin(GOOGLE_DOCS_ACCOUNT, GOOGLE_DOCS_PASSWORD, 'politicaladsleuth')
                fp.close()
                media = gdata.data.MediaSource(file_path=fp.name, content_type='text/csv')
                try:
                    resource = client.get_resource_by_id(GOOGLE_DOCS_RESOURCE_ID)
                    updated_resource = client.update_resource(resource, media=media, update_metadata=False, new_revision=True)
                    self.stdout.write('Data uploaded to "%s"\n'.format(updated_resource.title.text))
                except gdata.client.RequestError as e:
                    self.stderr.write(e.message + '\n')
                    self.stdout.write('****Upload may have succeeded despite an InvalidEntryException error****\n')

            fp.close()
            fp.unlink(fp.name)
        else:
            self.stdout.write('No signups for the given parameters\n')
    def handle(self, *args, **options):
        if not GOOGLE_DOCS_ACCOUNT and GOOGLE_DOCS_PASSWORD and GOOGLE_DOCS_RESOURCE_ID:
            raise CommandError(
                'You must set both GOOGLE_DOCS_ACCOUNT, GOOGLE_DOCS_PASSWORD and GOOGLE_DOCS_RESOURCE_ID in your settings file.'
            )
        verbosity = int(options.get('verbosity', 1))
        output_all = options.get('output_all')
        dry_run = options.get('dry_run')
        fields = ('email', 'first_name', 'last_name', 'phone', 'city', 'state',
                  'zipcode', 'is_a', 'broadcasters', 'date_created',
                  'share_info')

        profile_list = NonUserProfile.objects.order_by('-date_created')
        if not output_all:
            profile_list = profile_list.filter(share_info=True)

        if len(profile_list):
            if verbosity > 1:
                self.stdout.write('{0} signups to record.'.format(
                    len(profile_list)))
            fp = NamedTemporaryFile(delete=False)
            writer = csv.DictWriter(fp, fields)
            writer.writeheader()

            for signup in profile_list:
                output = {
                    'email':
                    signup.email,
                    'first_name':
                    signup.first_name,
                    'last_name':
                    signup.last_name,
                    'phone':
                    signup.phone,
                    'city':
                    signup.city,
                    'state':
                    signup.state,
                    'zipcode':
                    signup.zipcode,
                    'is_a':
                    signup.is_a,
                    'date_created':
                    signup.date_created.strftime('%m/%d/%Y %H:%M:%S'),
                    'share_info':
                    signup.share_info
                }
                extra_fields_data = signup.extra_fields
                for extra_field in SIGNUP_EXTRA_FIELDS:
                    input_val = None
                    if isinstance(extra_fields_data[extra_field], list):
                        input_val = ', '.join(extra_fields_data[extra_field])
                    else:
                        input_val = extra_fields_data[extra_field]
                    output[extra_field] = input_val
                writer.writerow(output)
            if dry_run:
                self.stdout.write('Row created:\n{0}\n'.format('|'.join(
                    [str(output[f]) for f in fields])))
            del (writer)

            if not dry_run:
                client = gdata.docs.client.DocsClient()
                login_token = client.ClientLogin(GOOGLE_DOCS_ACCOUNT,
                                                 GOOGLE_DOCS_PASSWORD,
                                                 'politicaladsleuth')
                fp.close()
                media = gdata.data.MediaSource(file_path=fp.name,
                                               content_type='text/csv')
                try:
                    resource = client.get_resource_by_id(
                        GOOGLE_DOCS_RESOURCE_ID)
                    updated_resource = client.update_resource(
                        resource,
                        media=media,
                        update_metadata=False,
                        new_revision=True)
                    self.stdout.write('Data uploaded to "%s"\n'.format(
                        updated_resource.title.text))
                except gdata.client.RequestError as e:
                    self.stderr.write(e.message + '\n')
                    self.stdout.write(
                        '****Upload may have succeeded despite an InvalidEntryException error****\n'
                    )

            fp.close()
            fp.unlink(fp.name)
        else:
            self.stdout.write('No signups for the given parameters\n')