def save_and_return_story_id(self, user): url = self.cleaned_data["url"] story_title = self.cleaned_data["story_title"] try: story = Story.objects.get(title=story_title, created_by=user) article = Article(url=url, story=story, created_by=user) except Story.DoesNotExist: story = Story(title=story_title, created_by=user) story.save() article = Article(url=url, story=story, created_by=user) #Get title and picture using the Open Graph meta tags in the html #file of the article response = requests.get(article.url) html_soup = BeautifulSoup(response.text, "html.parser") title = html_soup.find("meta", property="og:title")["content"] image_url = html_soup.find("meta", property="og:image")["content"] article.title = title article.image_url = image_url article.save() return story.id
def create_multiple_stories(self, num_stories=100): """creates range of stories and authors and privacy levels modulo 3 = public 3+1 = logged in 3+2 = private """ self.users = [] self.stories = [] for story_num in range(num_stories): if story_num % 3 == 0: access = Story.PUBLIC elif story_num % 3 == 1: access = Story.LOGGED_IN else: access = Story.PRIVATE self.users.append( self.User.objects.create_user( username=f"djangotestuser{story_num}", password=f"{story_num}2345abcde", ) ) # users[-1] = last in list self.users[-1].save() self.stories.append( Story( title=f"My Story{story_num}", content="This is a story all about how...", author=self.users[-1], access=access, ) ) self.stories[-1].save()
def new_story(request, twilio_request, response=twiml.Response()): try: author = Author.objects.get(sms_number=twilio_request.from_) except Author.DoesNotExist: first_name = "Anonymous" last_anon = User.objects.filter(first_name=first_name).count() last_name = "SMS#%d" % (last_anon + 1, ) username = "******".format(first_name, last_name).lower() user, new_user = User.objects.get_or_create(first_name=first_name, last_name=last_name, username=username) author = Author(user=user, sms_number=twilio_request.from_) author.save() story = Story(author=author, content=twilio_request.body) story.save() # save storyId for continuation request.set_cookie('storyId', story.id) response.message( "Thank you for sharing with StoriesOfSolidarity.org" + " Now put your story on the map! Reply with your zipcode for accurate posting." ) return response
def save_story(self, story): s = Story( headline=story['title']['#cdata-section'], byline=story['author'], pubdate=parser.parse(story['pubDate']), description=story['description']['#cdata-section'], full_text=story['body']['#cdata-section'], small_image_url=story['smallimage'], large_image_url=story['largeimage'], video_url=story['video'] ) s.save() entities = [] if story['metadata'] is not None: for k, v in story['metadata'].items(): entity_type, entity_type_created = EntityType.objects.get_or_create(entity_type=k) if isinstance(v, dict): entity_obj, entity_created = Entity.objects.get_or_create( entity_name=v['@value'], entity_type=entity_type ) entities.append(entity_obj) else: for e in v: entity, entity_created = Entity.objects.get_or_create( entity_name=e['@value'], entity_type=entity_type ) entities.append(entity) s.entities.add(*entities)
def post(self, request): # Get user and unique identifier user = request.user # Create new story new_story = Story(author=user) new_story.save() # Serialize the story serializer = StorySerializer(new_story) # Return the story return Response(serializer.data, status=status.HTTP_201_CREATED)
def add_story(request, emp_id): if request.method == 'POST': try: story_data = json.loads(request.body) story = Story(project_name=story_data['name'], project_desc=story_data['desc'], skills_required=[ Skill.objects.get(tag=s) for s in story_data['skills'] ], deadline=datetime.strptime(story_data['deadline'])) story.save() return JsonResponse({}, status=200) except: return JsonResponse({}, status=400) return JsonResponse({}, status=400)
def create_common_components(self, num_entrants): """Sets up a range of users, stories and entries""" self.users = [] self.stories = [] self.entries = [] for user_num in range(num_entrants): self.users.append( self.User.objects.create_user( username=f"djangotestuser{user_num}", password=f"{user_num}2345abcde", ) ) self.users[-1].save() for user in self.users: self.stories.append(Story(author=user, access=Story.PRIVATE)) self.stories[-1].save()
def create_story( self, access=UNIVERSAL_PRIVATE, author=None, title=None, wordcount=600 ): """Creates many types of stories""" fake = Faker() if not author: author = self.user if not title: title = fake.sentence() content = fake.sentence(nb_words=wordcount, variable_nb_words=False) self.story = Story( title=title, content=content, author=author, access=access, ) self.story.save()
def create_stories(self): import datetime from django.template.defaultfilters import slugify from django.contrib.sites.models import Site from django_ext import markov from stories.models import Story from categories.models import Category from staff.models import StaffMember chain = markov.MarkovChain() staff = StaffMember.objects.all()[:] categories = Category.objects.all()[:] # Load the dictionary into the markov chain sentences = os.path.abspath( os.path.join(fixtures_path, 'demo_story_data.txt')) for line in open(sentences): words = line.strip().split() chain.add(words) # Let's generate 10 random stories for i in range(10): story_data = {'headline': " ".join(chain.random_output(15))} story_data['slug'] = slugify(story_data['headline'])[:30] # 25% chance of a subhead if random.randint(1, 4) == 1: story_data['subhead'] = " ".join(chain.random_output(20)) story_data['teaser'] = " ".join(chain.random_output()) story_data['publish_date'] = datetime.datetime.now().date() story_data['publish_time'] = datetime.datetime.now().time() story_data['site'] = Site.objects.get_current() story_data['primary_category'] = random.choice(categories) # Create a story with 4-10 paragraphs with 1-4 sentences each body = [] for j in range(0, random.randint(4, 10)): p = [] for i in range(0, random.randint(1, 4)): p.append(" ".join(chain.random_output())) body.append("<p>%s</p>" % " ".join(p)) story_data['body'] = "\n".join(body) story = Story(**story_data) story.save() story.authors.add(random.choice(staff))
def add_story(request): if request.method == 'POST': project_id = request.POST.get('project', None) story = Story(accepted=True) story.order = Story.objects.filter(project=project_id).count()+1 form = StoryForm(request.POST, instance=story) if form.is_valid(): form.save() return redirect('project_page', project_id=project_id ) else: request.session['error'] = { 'ref':'add_error', 'name':request.POST.get('name', ''), 'time':request.POST.get('time', ''), 'user':request.POST.get('user', ''), } return redirect('project_page', project_id=project_id) else: raise Http404
def setUp(self): """set up""" self.story = Story( title="Story1", content="This is a <b>story</b>", access=Story.PRIVATE ) self.story.save() self.contest = Contest( max_wordcount=10, expiry_date=timezone.now() + timezone.timedelta(7), start_date=timezone.now(), title="Contest1", content="This is a <b>contest</b>", ) self.contest.save() self.form = EnterContestOldStoryForm( data={"story": self.story}, story_wordcount=html_wordcount(self.story.content), contest_max_wordcount=self.contest.max_wordcount, contest_expiry_date=self.contest.expiry_date, )
def NewStory(request): user = request.user file_objs = [] if request.method == "POST": form = NewStoryForm(request.POST, request.FILES) if form.is_valid(): file = request.FILES.get('content') caption = form.cleaned_data.get('caption') story = Story(user=user, content=file, caption=caption) story.save() return redirect('index') else: form = NewStoryForm() context = { 'form': form, } return render(request, 'newstory.html', context)
def main(): # Attempt to retrieve and process the data from the Unoffical Hacker News API for i in range(RETRY_ATTEMPTS + 1): try: response = urlopen(HACKER_NEWS_API_URL) status_code = response.code except HTTPError as e: status_code = e.code print (e.code) # If the service errored, hit it again if status_code != 200: if i <= RETRY_ATTEMPTS: print("An error occured while retrieving the data, retrying (%d)..." % (i+1), file=sys.stderr) print (status_code.__str__()) continue # If everything went ok, try to load the data try: items = json.loads(response.read().decode('utf-8'))['items'] break except ValueError as e: if i <= RETRY_ATTEMPTS: print("An error occurred while loading the data, retrying (%d)..." % i+1, file=sys.stderr) continue else: sys.exit("Too many errors occurred while attempting to retrieve the data") # Add the stories to the database moderator = User.objects.get(username=USERNAME) for item in items: story = Story( title=item['title'], url=item['url'], points=item['points'], moderator=moderator) story.save() story.created_at = created_at(item) story.save()
def setUpClass(cls): cls.u = User(name="test", email="*****@*****.**") cls.u.save() cls.new_title = Story(moderator=cls.u, title="hello world") cls.new_title.save()
# If everything went ok, try to load the data try: items = json.loads(response.content)['items'] break except ValueError, e: if i <= RETRY_ATTEMPTS: print( "An error occurred while loading the data, retrying (%d)..." % i + 1, file=sys.stderr) continue else: sys.exit( "Too many errors occurred while attempting to retrieve the data") # Add the stories to the database moderator = User.objects.get(username=USERNAME) for item in items: story = Story(title=item['title'], url=item['url'], points=item['points'], moderator=moderator) story.save() story.created_at = created_at(item) story.save() if __name__ == '__main__': main()
def handle(self, *args, **options): if options['flush']: old = Story.objects.all() confirm = raw_input( 'This will delete all %d existing stories. Are you sure? [y/N] ' % old.count()) if confirm == 'y': old.delete() # get input_file from stdin input_file = fileinput.input(args) temp_file = tempfile.TemporaryFile() # save to temp storage for json parsing for line in input_file: temp_file.write(line) temp_file.seek(0) with temp_file as jsonfile: stories = json.load(jsonfile) n_s, n_a = (0, 0) for data in stories['data']: story = Story(content=data.get('Content')) author, new_author = Author.objects.get_or_create_user( user__name=data.get('UserName')) if new_author: n_a = n_a + 1 author.part_time = bool(data.get('PartTime')) author.employed = bool(data.get('Employed')) author.employer = data.get('Employer') author.occupation = data.get('Occupation') if author.user.last_name.lower() == "anonymous": author.anonymous = True author.save() story.author = author if data.get('Truncated'): story.truncated = True if data.get('Latitude') and data.get('Longitude'): location, new_location = Location.objects.get_or_create( city=data.get('City'), state=data.get('State')) if new_location and data.get('Latitude') and data.get( 'Longitude'): location.lat = data.get('Latitude') location.lon = data.get('Longitude') location.geocoded = True location.save() story.location = location story.save() if data.get('Timestamp'): story.created_at = data['Timestamp'] else: # old, put it before anything else story.created_at = datetime(2013, 7, 1, 0, 0) story.updated_at = datetime.now() story.save() n_s = n_s + 1 self.stdout.write("imported %d stories by %d authors" % (n_s, n_a))
def handle(self, *args, **options): if options['flush']: old = Story.objects.filter(employer__startswith="Walmart") confirm = raw_input( 'This will delete all %d existing Walmart stories. Are you sure? [y/N] ' % old.count()) if confirm == 'y': old.delete() # get input_file from stdin input_file = fileinput.input(args) temp_file = tempfile.TemporaryFile() # save to temp storage for json parsing for line in input_file: temp_file.write(line) temp_file.seek(0) with temp_file as jsonfile: stories = json.load(jsonfile) n_s, n_a, n_l = (0, 0, 0) for data in stories: try: story, new_story = Story.objects.get_or_create( content=data.get('story')) except Story.MultipleObjectsReturned: duplicates = Story.objects.filter( content=data.get('story')) duplicates.delete() story = Story(content=data.get('story')) first_name = unidecode(data.get('fname')) last_name = unidecode(data.get('lname')) author, new_author = Author.objects.get_or_create_user( first_name=first_name, last_name=last_name) if new_author: n_a = n_a + 1 if data.get('email'): author.user.email = data.get('email') author.user.active = False author.user.save() if data.get('store'): author.employer = "Walmart #" + data.get('store') else: author.employer = "Walmart" if data.get('associate'): author.occupation = "Associate" if data.get('anonymous'): author.anonymous = True author.save() story.author = author if data.get('zip'): zipcode = data.get('zip') # do zip -> city lookup inline zip_lookup = requests.get("http://api.zippopotam.us/us/" + zipcode) print "lookup", zipcode place = zip_lookup.json().get('places', [{}])[0] city = place.get('place name') state = place.get('state abbreviation') lat, lon = place.get('latitude'), place.get('longitude') try: location, new_location = Location.objects.get_or_create( city__iexact=city, state=state) except Location.MultipleObjectsReturned: duplicate_locations = Location.objects.filter( city__iexact=city, state=state) stories_at_location = duplicate_locations.values_list( 'story', flat=True) duplicate_locations.delete() location = Location(city=city, state=state) location.save() for reset_id in stories_at_location: try: reset_story = Story.objects.get(id=reset_id) reset_story.location = location reset_story.save() except Story.DoesNotExist: pass new_location = True if new_location and lat and lon: location.city = city location.state = state location.lat = lat location.lon = lon location.geocoded = True location.save() n_l = n_l + 1 story.location = location story.save() # export date from OurWalmart story.created_at = datetime(2013, 9, 4, 0, 0) story.updated_at = datetime.now() story.save() if new_story: n_s = n_s + 1 self.stdout.write( "imported %d stories by %d authors in %d locations" % (n_s, n_a, n_l))
def setUp(self): """Give 'em a nice story""" self.story = Story(title="My Story title2") self.story.save() self.stats = None
def setUp(self): self.story = Story(title="My Story title")