def handle(self, *args, **options): """Handle the command""" source_file = options['file'] self.stdout.write(f'Checking if {source_file} exists...') try: # Open csv and loads info to db with open(source_file, 'r') as file: rows = list(csv.reader(file)) count = 0 for idx in range(1, len(rows)): author = Author.objects.filter(name=rows[idx][0]).first() if not author: author = Author(name=rows[idx][0]) author.save() count += 1 self.stdout.write(self.style.SUCCESS( f'Authors table populated with {count} authors!' )) except FileNotFoundError: self.stdout.write(self.style.ERROR('File not found!'))
def test_get_diff_with_callable_related_manager(self): resource = AuthorResource() author = Author(name="Some author") author.save() author2 = Author(name="Some author") self.book.author = author self.book.save() diff = resource.get_diff(author2, author) headers = resource.get_export_headers() self.assertEqual(diff[headers.index("books")], "<span>core.Book.None</span>")
def test_get_diff_with_callable_related_manager(self): resource = AuthorResource() author = Author(name="Some author") author.save() author2 = Author(name="Some author") self.book.author = author self.book.save() diff = resource.get_diff(author2, author) headers = resource.get_export_headers() self.assertEqual(diff[headers.index('books')], '<span>core.Book.None</span>')
def save_status(self, data): """TODO""" status = Status.parse(self.api, json.loads(data)) if not status.geo: # _datafile.write(data+'\n') return if Author.objects.filter(owner__userprofile__twitter_id=status.user.id_str).exists(): # this tweet's author is on stargazer return try: author = Author.objects.filter(source=Author.T_TWITTER, external_id=status.user.id_str).get() except Author.DoesNotExist: author = Author( name=status.user.screen_name, avatar_uri=status.user.profile_image_url, source=Author.T_TWITTER, external_id=status.user.id_str, ) author.save() try: post = Post.objects.filter(source=Post.T_TWITTER, external_id=status.id_str).get() except Post.DoesNotExist: lat = float(status.geo["coordinates"][0]) lng = float(status.geo["coordinates"][1]) try: addr = self._latlng2addr.get(lat, lng) except (LatLng2Addr.ConnectionFailed, LatLng2Addr.GeocodingFailed) as e: addr = "" # twitter api response in UTC created = status.created_at + timedelta(hours=8) post = Post( content=status.text, author=author, latitude=lat, longitude=lng, address=addr, source=Post.T_TWITTER, external_id=status.id_str, external_data=data, created=created, ) post.save() return
def form_valid(self, form): post = form.instance if 0 == self.user.author_set.filter(source=Author.T_LOCAL).count(): author = Author(name=self.user.username, source=Author.T_LOCAL, owner=self.user) author.save() else: author = self.user.author_set.filter(source=Author.T_LOCAL).get() post.author = author self.success_url = '/post/recent/?'+\ 'addr=%(address)s&lat=%(latitude)s&lng=%(longitude)s' return super(CreateView, self).form_valid(form)
def add_authors_to(self, book): authors = [] for form in self.forms: author_data = form.cleaned_data full_name = author_data.get('full_name', '') info = author_data.get('info', '') try: author = Author.objects.get(full_name=full_name) if info: author.info = info author.save() except ObjectDoesNotExist: author = Author(**author_data) author.save() authors.append(author) book.set_authors(authors)
def load_authors(file): print '\nimporting authors' for e in json.load(file): try: author = Author.objects.filter(name=e['name'], source=e['source']).get() except Author.DoesNotExist: author = Author(name=e['name'], avatar_uri=e['avatar_uri'], source=e['source'], external_id=e['external_id']) if e['owner_name']: user = User.objects.filter(username=e['owner_name']).get() author.owner = user author.save() print '.', sys.stdout.flush()
def copy_author_to_submission(user, book): author = Author( first_name=user.first_name, middle_name=user.profile.middle_name, last_name=user.last_name, salutation=user.profile.salutation, institution=user.profile.institution, department=user.profile.department, country=user.profile.country, author_email=user.email, biography=user.profile.biography, orcid=user.profile.orcid, twitter=user.profile.twitter, linkedin=user.profile.linkedin, facebook=user.profile.facebook, ) author.save() book.author.add(author) return author
def form_valid(self, form): post = form.instance user = self.request.user try: author = user.author_set.filter(source=Author.T_LOCAL).get() except Author.DoesNotExist: author = Author(name=user.username, source=Author.T_LOCAL, owner=user) author.save() post.author = author post.save() if self.request.POST['return']: success_url = self.request.POST['return'] + 'lat{}/lng{}/recent/' return HttpResponse(json.dumps({ 'done': True, 'post_id': post.id, 'go_to': success_url.format(post.latitude, post.longitude) }), content_type='application/json')