コード例 #1
0
 def import_polls(self):
     self.stdout.write(self.style.MIGRATE_LABEL("Importing Polls"))
     progress = ProgressBar(widgets=[Percentage(), Bar()],
                            maxval=len(self.polls)).start()
     defect = 0
     for i, post in enumerate(self.polls):
         post.adjust_paths(attachments=self.attachments,
                           prefix=self.attachment_path)
         post.fix_paragraphs()
         post.fix_more()
         attachment = None
         if post.attachment_id:
             attachment = Attachment.objects.get(pk=post.attachment_id)
         objPoll = Question(
             id=post.id,
             title=post.title,
             question_text=post.question_text,
             author=User.objects.get(username=post.creator),
             content=post.body,
             url=post.url,
             attachment=attachment,
             publish=True,
             pub_date=post.post_date,
             # votes=post.votes
         )
         objPoll.save()
         for choice in post.choices:
             objChoice = Choice(
                 question=objPoll,
                 choice_text=choice.get('label'),
                 attachment=Attachment.objects.get(
                     pk=choice.get('thumbnail_id')),
                 visible=choice.get('visible'),
                 # votes=choice.get('votes')
             )
             objChoice.save()
         for answer in post.answers:
             try:
                 choice = Choice.objects.get(choice_text=answer.get('text'))
                 objAnswer = Information(
                     choice=choice,
                     ip_address=answer.get('ip'),
                     useragent=answer.get('useragent'),
                     created_at=datetime.datetime.fromtimestamp(
                         answer.get('time'), tz=pytz.utc))
                 objPoll.votes = objPoll.votes + 1
                 choice.votes = choice.votes + 1
                 choice.save()
                 objAnswer.save()
             except:
                 defect = defect + 1
                 pass
         objPoll.save()
         tags = []
         for tag in post.tags:
             tag = Tag.objects.get(slug=tag)
             objPoll.tags.add(tag)
             objPoll.save()
         progress.update(i)
     progress.finish()
     self.stdout.write(self.style.ERROR("DEFECT answers: " + str(defect)))