def publish_post(request, id): form = PostForm(request.POST) if form.is_valid(): post = Post() post.title = form.cleaned_data.get('title') post.content = form.cleaned_data.get('content') post.source_url = form.cleaned_data.get('source_url') post.breach_date = form.cleaned_data.get('breach_date') user = form.cleaned_data.get('created_by_user') if user: post.create_user = User.objects.get(username=user) else: post.create_user = request.user try: post.save() tags = form.cleaned_data.get('tags') post.create_tags(tags) breach_alert = get_object_or_404(Post, title=post.title, breach_date=post.breach_date) send_email(breach_alert.title, breach_alert.slug) if id: delete_google_alert(request, id) return redirect('post', slug=breach_alert.slug) except Error: return HttpResponse(Error) else: return HttpResponse(form.errors)
def upload(request, customer_id): customer = get_object_or_404(Customer, pk=customer_id) if request.method == 'POST' and request.POST['content']: myfile = request.FILES['myfile'] post = Post() post.content = request.POST['content'] post.createtime = timezone.now() post.updatetime = timezone.now() post.customer = Customer.objects.get(id=customer_id) fs = FileSystemStorage() filename = fs.save(myfile.name, myfile) uploaded_file_url = fs.url(filename) # image = Image() post.save() for file in request.FILES.getlist('myfile'): image = Image() post.save() image.imageurl = file image.post = Post.objects.get(id=post.id) image.save() # image.imageurl = filename # image.post = Post.objects.get(id=post.id) # post.save() # image.save() return redirect('/customers/' + 'detail/' + str(customer_id)) return render(request, 'customers/detail.html', {'customer': customer})
def update(self, pk): try: post = Post.objects.get(id=pk) except Post.DoesNotExist: post = Post() post.title = self.data["title"] post.user = User.objects.get(username=self.data["author"]) post.content = self.data["body"] post.save() return post
def update(self, pk): try: post = Post.objects.get(id=pk) except Post.DoesNotExist: post = Post() post.title = self.data['title'] post.user = User.objects.get(username=self.data['author']) post.content = self.data['body'] post.save() return post
def addPost(title, content, tags, id_category): p = Post() p.title = title p.content = content p.category_id = id_category p.author_id = 1 p.status = 'published' p.save() for x in tags.split(','): new_tag_obj, created = Tag.objects.get_or_create(name=x) p.tags.add(new_tag_obj.name)
def test_posts_view_renders_template_title(self): """Test post view response content contains post title.""" this_user = self.users[0] this_post = Post() this_post.author = this_user this_post.content = 'tornado fire crocodile' this_post.title = 'marc ben benny built this site' this_post.save() self.client.force_login(this_user) response = self.client.get('/posts/', follow=True) self.assertContains(response, 'marc ben benny built this site')
def inject_posts(self): logging.info('Injecting posts...') with open('utils/dbinjector/posts.csv', newline='') as csv_file: csv_reader = csv.reader(csv_file, delimiter='|') for row in csv_reader: post = Post() post.user = User.objects.get(username=row[0]) post.subforum = Subforum.objects.get(name=row[1]) post.title = row[2] post.content = row[3] post.save()
def test_posts_view_renders_correct_template(self): """Test post view response renders the post.html template.""" this_user = self.users[0] this_post = Post() this_post.author = this_user this_post.content = 'tornado fire crocodile' this_post.title = 'marc ben benny built this site' this_post.save() self.client.force_login(this_user) response = self.client.get('/posts/', follow=True) self.assertTemplateUsed(response, "base.html") self.assertTemplateUsed(response, "posts/posts.html")
def test_posts_render_on_other_profile(self): """Test profile view response content contains post content.""" this_user = self.users[0] this_post = Post() this_post.author = this_user this_post.content = 'tornado fire crocodile' this_post.title = 'marc ben benny built this site' this_post.save() self.client.force_login(self.users[1]) response = self.client.get( '/profile/' + str(this_user.username), follow=True) self.assertContains(response, 'tornado fire crocodile')
def test_view_other_user_post_on_posts_view(self): """Test other user posts' content shows on posts page.""" this_user = self.users[0] this_post = Post() this_post.author = this_user this_post.content = 'tornado fire crocodile' this_post.title = 'marc ben benny built this site' this_post.save() other_user = self.users[1] self.client.force_login(other_user) response = self.client.get('/posts/', follow=True) self.assertContains(response, 'tornado fire crocodile') self.assertContains(response, 'marc ben benny built this site')
def stock_backtest(request): if request.method == 'POST': all_backtests = [] body = json.loads(request.body.decode('utf-8')) test = False array = [] for m in range(1, 2): index = m * 23 * 16 item = '[' + date_2016()[index:] + ',' + date_2017()[0:index - 1] + ']' # item = '[' + date_2018()[23*16:23*22-1] + ']' array.append(item) # for n in range(0, len(array)): for n in range(0, 1): date_array = json.loads(array[n]) if 'test' in body: test = True time_period = body.get('time_period') position_stock = body.get('position_stock') percent = body.get('percent') all_backtests.append( backtest(date_array, time_period, position_stock, percent / 100 - 1)) else: for i in range(5, 19): for j in range(0, 5): for k in range(105, 110): # print(i, j) title = array[0][2:22] + '|' + array[0][ -22:-2] + 'time_period' + str( i) + 'position_stock' + str( j) + 'percent' + str( k) + 'percent_sell' + str(0.97) filtered_posts = Post.objects.filter(title=title) if len(filtered_posts) == 0: all_backtests.append( backtest(date_array, i, j, k / 100 - 1)) post = Post() post.title = title post.content = json.dumps(all_backtests) post.save() if all_backtests: return JsonResponse({ 'data': all_backtests[0], 'data1': all_backtests }) return JsonResponse({'data': [], 'data1': []})
def test_comment_content_renders_on_posts_view(self): """Test comment comntent is in post request.""" this_user = self.users[0] self.client.force_login(this_user) this_post = Post() this_post.author = this_user this_post.content = 'tornado fire crocodile' this_post.title = 'marc ben benny built this site' this_post.save() this_comment = Comment() this_comment.by_user = this_user this_comment.on_post = this_post this_comment.comment = 'this comment' this_comment.save() response = self.client.post( '/posts/' + str(this_post.id), follow=True) self.assertContains(response, 'this comment')
def post_publish(request): if(request.method == 'POST'): form = PostForm(request.POST) if(form.is_valid()): ctx = BlogInfoContext(request, { 'form': form }) post = Post() post.content = form['content'].value() post.title = form['title'].value() post.published = datetime.datetime.now() post.save() return HttpResponseRedirect("/") else: form = PostForm() return render_to_response('post_create.html',{ 'form': form })
def test_create_post(self): # Create the post post = Post() # Set the attributes post.title = 'My first post' post.content = 'This is my first blog post' post.publish = timezone.now() # Save it post.save() # Check we can find it all_posts = Post.objects.all() self.assertEquals(len(all_posts), 1) only_post = all_posts[0] self.assertEquals(only_post, post) # Check attribute self.assertEquals(only_post.title, 'My first post') self.assertEquals(only_post.content, 'This is my first blog post')
def test_posts_view_preview(self): """A post of more than 40 chars is shortened.""" this_user = self.users[0] this_post = Post() this_post.author = this_user this_post.content = ( '11111111111111111111' + '11111111111111111111' + '11111111111111111111') this_post.title = 'marc ben benny built this site' this_post.save() self.client.force_login(self.users[1]) response = self.client.get( '/posts/', follow=True) self.assertContains( response, ('11111111111111111111' + '11111111111111111111' + '...') )
def create_new_post(request): # POST to http://service/author/posts # Create a post to the currently authenticated user # First get the information out of the request body size = len(request.body) #body = json.load(body) # body = dict(x.split("=") for x in body.split("&")) #post = body['post'] post = request.POST new_post = Post() # Post ID's are created automatically in the database # new_post.id = post['id'] #: "de305d54-75b4-431b-adb2-eb6b9e546013", #: "A post title about a post about web dev", new_post.title = post['title'] # Source and origin are the same, and depend on the ID so we wait until after the post gets saved to # generate this URLS # new_post.source = post['source'] #: "http://lastplaceigotthisfrom.com/posts/yyyyy" # new_post.origin = post['origin'] #: "http://whereitcamefrom.com/posts/zzzzz" new_post.description = post[ 'description'] # : "This post discusses stuff -- brief", # If the post is an image, the content would have been provided as a file along with the upload if len(request.FILES) > 0: file_type = request.FILES['file'].content_type allowed_file_type_map = { 'image/png': Post.TYPE_PNG, 'image/jpg': Post.TYPE_JPEG } if file_type not in allowed_file_type_map.keys(): return JsonResponse({ 'success': 'false', 'msg': f'You uploaded an image with content type: {file_type}, but only one of {allowed_file_type_map.keys()} is allowed' }) new_post.contentType = allowed_file_type_map[ file_type] # : "text/plain" new_post.content = base64.b64encode( request.FILES['file'].read()).decode('utf-8') else: new_post.contentType = post['contentType'] # : "text/plain", new_post.content = post['content'] #: "stuffs", new_post.author = request.user # the authenticated user # Categories added after new post is saved new_post.count = 0 #: 1023, initially the number of comments is zero new_post.size = size #: 50, the actual size of the post in bytes # This is not a property that gets stored, but rather a link to the comments of this post that should # be generated on the fly. # new_post.next = post['next'] #: "http://service/posts/{post_id}/comments", # We do not permit adding comments at the same time a post is created, so this field is not created # new_post.comments = post['comments'] #: LIST OF COMMENT, current_tz = pytz.timezone('America/Edmonton') timezone.activate(current_tz) #: "2015-03-09T13:07:04+00:00", new_post.published = str(datetime.datetime.now()) new_post.visibility = post['visibility'].upper() #: "PUBLIC", new_post.unlisted = True if 'unlisted' in post and post[ 'unlisted'] == 'true' else False #: true new_post.save() # Now we can set source and origin new_post.source = settings.HOSTNAME + "/posts/" + str(new_post.id.hex) new_post.origin = settings.HOSTNAME + "/posts/" + str(new_post.id.hex) new_post.save() # Take the user uid's passed in and convert them into Authors to set as the visibleTo list uids = post.getlist('visibleTo') visible_authors = Author.objects.filter(uid__in=uids) for author in visible_authors: new_post.visibleTo.add(author) # Categories is commented out because it's not yet in the post data, uncomment once available for category in post['categories'].split('\r\n'): try: # Try connecting to existing categories cat_object = Category.objects.get(name=category) except Category.DoesNotExist as e: cat_object = Category.objects.create( name=category) # Create one if not new_post.categories.add(cat_object) #: LIST, # for key in body.keys(): # print(f'{key}: {body[key]}') if len(request.FILES) > 0: # If they uploaded a file this is an ajax call and we need to return a JSON response return JsonResponse({'success': 'true', 'msg': new_post.id.hex}) else: return redirect("/")
def savepost(request): if request.method == 'POST': title = request.POST.get("title", "") #message = request.POST.get("message", "") message = request.POST.get("message", "") if request.user.id: users_id = request.user.id user = User.objects.get(id=users_id) ac_token = user.ac_token linkedin_id = user.linkedin_id if request.POST.get('sent_on'): post_obj = Post() sent_on = request.POST.get('sent_on') user = User.objects.get(id=users_id) post_obj.content = message post_obj.title = title post_obj.author = user post_obj.sent_on = sent_on post_obj.type = 0 # personal profile =0 = page = 2 group = 3 post_obj.status = 0 # pending 0 sent 1 post_obj.save() return render(request, 'dashboard.html') else: api_url = "https://api.linkedin.com/v2/ugcPosts" #"author": "urn:li:person:" + linkedin_id, #"author": "urn:li:organization:" + linkedin_id, post_data = { "author": "urn:li:person:"+linkedin_id, "lifecycleState": "PUBLISHED", "specificContent": { "com.linkedin.ugc.ShareContent": { "shareCommentary": { "text": message }, "shareMediaCategory": "NONE" }, }, "visibility": { "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC" }, } headers = {'X-Restli-Protocol-Version': '2.0.0', 'Content-Type': 'application/json', 'Authorization': f'Bearer {ac_token}'} response = requests.post(api_url, headers=headers, json=post_data) if response.status_code == 201: print("Success") post_obj = Post() user = User.objects.get(id=users_id) post_obj.content = message post_obj.title = title post_obj.author = user post_obj.type = 0 # personal profile =0 = page = 2 group = 3 post_obj.status = 1 # sent post_obj.save() return render(request, 'dashboard.html') else: print(response.content) return render(request, 'addpost.html') else: return render(request, 'login.html') return render(request, 'addpost.html')
def initialize_calendar(data): post_new_years = Post() post_new_years.author = Account.objects.get(username = data['username']) post_new_years.content = 'New Year\'s Day' post_new_years.start_time = datetime.date(2015, 1, 1) post_new_years.day_of_week = 'Thursday' post_new_years.is_holiday = True post_new_years.save() post_mlk = Post() post_mlk.author = Account.objects.get(username = data['username']) post_mlk.content = 'Martin Luther King Day' post_mlk.start_time = datetime.date(2015, 1, 19) post_mlk.day_of_week = 'Monday' post_mlk.is_holiday = True post_mlk.save() post_groundhog_day = Post() post_groundhog_day.author = Account.objects.get(username = data['username']) post_groundhog_day.content = 'Groundhog Day' post_groundhog_day.start_time = datetime.date(2015, 2, 1) post_groundhog_day.day_of_week = 'Monday' post_groundhog_day.is_holiday = True post_groundhog_day.save() post_rosa_parks = Post() post_rosa_parks.author = Account.objects.get(username = data['username']) post_rosa_parks.content = 'Rosa Parks Day' post_rosa_parks.start_time = datetime.date(2015, 2, 4) post_rosa_parks.day_of_week = 'Wednesday' post_rosa_parks.is_holiday = True post_rosa_parks.save() post_lincolns_bday = Post() post_lincolns_bday.author = Account.objects.get(username = data['username']) post_lincolns_bday.content = 'Lincoln\'s Birthday' post_lincolns_bday.start_time = datetime.date(2015, 2, 12) post_lincolns_bday.day_of_week = 'Thursday' post_lincolns_bday.is_holiday = True post_lincolns_bday.save() post_valentine = Post() post_valentine.author = Account.objects.get(username = data['username']) post_valentine.content = 'Valentine\'s Day' post_valentine.start_time = datetime.date(2015, 2, 14) post_valentine.day_of_week = 'Saturday' post_valentine.is_holiday = True post_valentine.save() post_presidents = Post() post_presidents.author = Account.objects.get(username = data['username']) post_presidents.content = 'President\'s Day' post_presidents.start_time = datetime.date(2015, 2, 16) post_presidents.day_of_week = 'Monday' post_presidents.is_holiday = True post_presidents.save() post_ash = Post() post_ash.author = Account.objects.get(username = data['username']) post_ash.content = 'Ash Wednesday Day' post_ash.start_time = datetime.date(2015, 2, 18) post_ash.day_of_week = 'Wednesday' post_ash.is_holiday = True post_ash.save() post_chinese = Post() post_chinese.author = Account.objects.get(username = data['username']) post_chinese.content = 'Chinese New Year' post_chinese.start_time = datetime.date(2015, 2, 19) post_chinese.day_of_week = 'Thursday' post_chinese.is_holiday = True post_chinese.save() post_texas = Post() post_texas.author = Account.objects.get(username = data['username']) post_texas.content = 'Texas Independence Day' post_texas.start_time = datetime.date(2015, 3, 2) post_texas.day_of_week = 'Monday' post_texas.is_holiday = True post_texas.save() post_employee = Post() post_employee.author = Account.objects.get(username = data['username']) post_employee.content = 'Employee Appreciation Day' post_employee.start_time = datetime.date(2015, 3, 6) post_employee.day_of_week = 'Friday' post_employee.is_holiday = True post_employee.save() post_daylight = Post() post_daylight.author = Account.objects.get(username = data['username']) post_daylight.content = 'Daylight Saving\'s Time' post_daylight.start_time = datetime.date(2015, 3, 6) post_daylight.day_of_week = 'Friday' post_daylight.is_holiday = True post_daylight.save() post_patrick = Post() post_patrick.author = Account.objects.get(username = data['username']) post_patrick.content = 'St. Patrick\'s Day' post_patrick.start_time = datetime.date(2015, 3, 17) post_patrick.day_of_week = 'Tuesday' post_patrick.is_holiday = True post_patrick.save() post_spring = Post() post_spring.author = Account.objects.get(username = data['username']) post_spring.content = 'Spring Solstice' post_spring.start_time = datetime.date(2015, 3, 20) post_spring.day_of_week = 'Friday' post_spring.is_holiday = True post_spring.save() post_friday = Post() post_friday.author = Account.objects.get(username = data['username']) post_friday.content = 'Good Friday' post_friday.start_time = datetime.date(2015, 4, 3) post_friday.day_of_week = 'Friday' post_friday.is_holiday = True post_friday.save() post_passover = Post() post_passover.author = Account.objects.get(username = data['username']) post_passover.content = 'Passover' post_passover.start_time = datetime.date(2015, 4, 4) post_passover.day_of_week = 'Saturday' post_passover.is_holiday = True post_passover.save() post_easter = Post() post_easter.author = Account.objects.get(username = data['username']) post_easter.content = 'Easter Sunday' post_easter.start_time = datetime.date(2015, 4, 5) post_easter.day_of_week = 'Sunday' post_easter.is_holiday = True post_easter.save() post_tax = Post() post_tax.author = Account.objects.get(username = data['username']) post_tax.content = 'Tax Day' post_tax.start_time = datetime.date(2015, 4, 15) post_tax.day_of_week = 'Wednesday' post_tax.is_holiday = True post_tax.save() post_cinco = Post() post_cinco.author = Account.objects.get(username = data['username']) post_cinco.content = 'Cinco de Mayo' post_cinco.start_time = datetime.date(2015, 5, 5) post_cinco.day_of_week = 'Tuesday' post_cinco.is_holiday = True post_cinco.save() post_mother = Post() post_mother.author = Account.objects.get(username = data['username']) post_mother.content = 'Mother\'s Day' post_mother.start_time = datetime.date(2015, 5, 15) post_mother.day_of_week = 'Sunday' post_mother.is_holiday = True post_mother.save() post_armed = Post() post_armed.author = Account.objects.get(username = data['username']) post_armed.content = 'Armed Forces Day' post_armed.start_time = datetime.date(2015, 5, 16) post_armed.day_of_week = 'Saturday' post_armed.is_holiday = True post_armed.save() post_memorial = Post() post_memorial.author = Account.objects.get(username = data['username']) post_memorial.content = 'Memorial Day' post_memorial.start_time = datetime.date(2015, 5, 25) post_memorial.day_of_week = 'Monday' post_memorial.is_holiday = True post_memorial.save() post_dday = Post() post_dday.author = Account.objects.get(username = data['username']) post_dday.content = 'D-Day Day' post_dday.start_time = datetime.date(2015, 6, 6) post_dday.day_of_week = 'Saturday' post_dday.is_holiday = True post_dday.save() post_flag = Post() post_flag.author = Account.objects.get(username = data['username']) post_flag.content = 'Flag Day' post_flag.start_time = datetime.date(2015, 6, 14) post_flag.day_of_week = 'Sunday' post_flag.is_holiday = True post_flag.save() post_summer = Post() post_summer.author = Account.objects.get(username = data['username']) post_summer.content = 'Summer Starts' post_summer.start_time = datetime.date(2015, 6, 21) post_summer.day_of_week = 'Sunday' post_summer.is_holiday = True post_summer.save() post_father = Post() post_father.author = Account.objects.get(username = data['username']) post_father.content = 'Father\'s Day' post_father.start_time = datetime.date(2015, 6, 21) post_father.day_of_week = 'Sunday' post_father.is_holiday = True post_father.save() post_independence = Post() post_independence.author = Account.objects.get(username = data['username']) post_independence.content = 'Independence Day' post_independence.start_time = datetime.date(2015, 7, 4) post_independence.day_of_week = 'Saturday' post_independence.is_holiday = True post_independence.save() post_labor = Post() post_labor.author = Account.objects.get(username = data['username']) post_labor.content = 'Labor Day' post_labor.start_time = datetime.date(2015, 9, 7) post_labor.day_of_week = 'Monday' post_labor.is_holiday = True post_labor.save() post_patriot = Post() post_patriot.author = Account.objects.get(username = data['username']) post_patriot.content = 'Patriot Day' post_patriot.start_time = datetime.date(2015, 9, 11) post_patriot.day_of_week = 'Friday' post_patriot.is_holiday = True post_patriot.save() post_constitution = Post() post_constitution.author = Account.objects.get(username = data['username']) post_constitution.content = 'Constitution Day' post_constitution.start_time = datetime.date(2015, 9, 17) post_constitution.day_of_week = 'Thursday' post_constitution.is_holiday = True post_constitution.save() post_fall = Post() post_fall.author = Account.objects.get(username = data['username']) post_fall.content = 'Autumnal Equinox' post_fall.start_time = datetime.date(2015, 9, 23) post_fall.day_of_week = 'Wednesday' post_fall.is_holiday = True post_fall.save() post_columbus = Post() post_columbus.author = Account.objects.get(username = data['username']) post_columbus.content = 'Columbus Day' post_columbus.start_time = datetime.date(2015, 10, 12) post_columbus.day_of_week = 'Monday' post_columbus.is_holiday = True post_columbus.save() post_halloween = Post() post_halloween.author = Account.objects.get(username = data['username']) post_halloween.content = 'Halloween' post_halloween.start_time = datetime.date(2015, 10, 31) post_halloween.day_of_week = 'Saturday' post_halloween.is_holiday = True post_halloween.save() post_light = Post() post_light.author = Account.objects.get(username = data['username']) post_light.content = 'Daylight Saving Time Ends' post_light.start_time = datetime.date(2015, 11, 1) post_light.day_of_week = 'Sunday' post_light.is_holiday = True post_light.save() post_election = Post() post_election.author = Account.objects.get(username = data['username']) post_election.content = 'Election Day' post_election.start_time = datetime.date(2015, 11, 3) post_election.day_of_week = 'Tuesday' post_election.is_holiday = True post_election.save() post_thanksgiving = Post() post_thanksgiving.author = Account.objects.get(username = data['username']) post_thanksgiving.content = 'Thanksgiving' post_thanksgiving.start_time = datetime.date(2015, 11, 26) post_thanksgiving.day_of_week = 'Tuesday' post_thanksgiving.is_holiday = True post_thanksgiving.save() post_presidents = Post() post_presidents.author = Account.objects.get(username = data['username']) post_presidents.content = 'President\'s Day' post_presidents.start_time = datetime.date(2015, 11, 27) post_presidents.day_of_week = 'Friday' post_presidents.is_holiday = True post_presidents.save() post_black = Post() post_black.author = Account.objects.get(username = data['username']) post_black.content = 'Black Friday' post_black.start_time = datetime.date(2015, 11, 27) post_black.day_of_week = 'Friday' post_black.is_holiday = True post_black.save() post_monday = Post() post_monday.author = Account.objects.get(username = data['username']) post_monday.content = 'Cyber Monday' post_monday.start_time = datetime.date(2015, 11, 30) post_monday.day_of_week = 'Monday' post_monday.is_holiday = True post_monday.save() post_han = Post() post_han.author = Account.objects.get(username = data['username']) post_han.content = 'Hanukkah' post_han.start_time = datetime.date(2015, 12, 7) post_han.day_of_week = 'Monday' post_han.is_holiday = True post_han.save() post_pearl = Post() post_pearl.author = Account.objects.get(username = data['username']) post_pearl.content = 'Pearl Harbor Remembrance Day' post_pearl.start_time = datetime.date(2015, 12, 7) post_pearl.day_of_week = 'Monday' post_pearl.is_holiday = True post_pearl.save() post_winter = Post() post_winter.author = Account.objects.get(username = data['username']) post_winter.content = 'Winter Solstice' post_winter.start_time = datetime.date(2015, 12, 22) post_winter.day_of_week = 'Tuesday' post_winter.is_holiday = True post_winter.save() post_xmas_eve = Post() post_xmas_eve.author = Account.objects.get(username = data['username']) post_xmas_eve.content = 'Christmas Eve' post_xmas_eve.start_time = datetime.date(2015, 12, 24) post_xmas_eve.day_of_week = 'Thursday' post_xmas_eve.is_holiday = True post_xmas_eve.save() post_xmas = Post() post_xmas.author = Account.objects.get(username = data['username']) post_xmas.content = 'Christmas Day' post_xmas.start_time = datetime.date(2015, 12, 25) post_xmas.day_of_week = 'Friday' post_xmas.is_holiday = True post_xmas.save() post_nye = Post() post_nye.author = Account.objects.get(username = data['username']) post_nye.content = 'New Year\'s Eve' post_nye.start_time = datetime.date(2015, 12, 31) post_nye.day_of_week = 'Thursday' post_nye.is_holiday = True post_nye.save()
def publicPosts(request): '''List all public posts on the server''' if request.method == 'GET': # Default if not given page_num = request.GET.get('page', DEFAULT_PAGE_NUM) page_size = request.GET.get('size', DEFAULT_PAGE_SIZE) # Get all local and global comments, combine and paginate results all_posts = Post.objects.filter(visibility='PUBLIC').order_by('-published') #for post in all_posts: # print post.comments #num_comments = len(all_posts[0]['comments']) #print 'Comments: ' #print num_comments # Need this here or else the pagination bitches about it being unicode page_num = int(page_num) # Get the right page pages = Paginator(all_posts, page_size) page = pages.page(page_num+1) data = page.object_list response_obj = {} response_obj['query'] = 'posts' response_obj['count'] = len(all_posts) response_obj['size'] = page_size if page.has_next(): response_obj['next'] = settings.LOCAL_HOST + 'api/posts?page=' + str(page_num + 1) + '&size=' + str(page_size) if page.has_previous(): response_obj['previous'] = settings.LOCAL_HOST + 'api/posts?page=' + str(page_num - 1) + '&size=' + str(page_size) serializer = PostSerializer(data, many=True) response_obj['posts'] = serializer.data return Response(response_obj) elif request.method == 'POST': '''This method POSTS ''' auth = request.data['author'] try: author = Author.objects.get(author_id = auth) print(author) except Exception, e: print 'No Author!' return Response("Not a valid author...", status=status.HTTP_400_BAD_REQUEST) try: post = Post(author = author) post.title = request.data['title'] post.description = request.data['description'] post.contentType = request.data['contentType'] post.content = request.data['content'] post.visibility = request.data['visibility'] post.categories = request.data['categories'] post.set_source() post.set_origin() post.save() #post.set_origin() return Response("Post Successfully Added.", status=status.HTTP_201_CREATED) except: return Response("Post Not Added", status=status.HTTP_400_BAD_REQUEST)
def repeat_events(event): start_time = event.start_time end_repeat_date = event.end_repeat repeat_type = event.repeat num_repeat = find_repeat(start_time, end_repeat_date, repeat_type) for x in range(1, num_repeat + 1): temp_event = Post() temp_event.author = event.author temp_event.content = event.content temp_event.notification = event.notification temp_event.notify_when = event.notify_when temp_event.location_event = event.location_event temp_event.description_event = event.description_event temp_event.begin_time = event.begin_time temp_event.end_time = event.end_time temp_event.end_repeat = event.end_repeat temp_event.need_repeat = False temp_event.start_time = event.start_time temp_event.not_all_day = event.not_all_day temp_event.repeat = event.repeat if repeat_type == 'Daily': temp_event.start_time = temp_event.start_time + datetime.timedelta(days=x) temp_event.notify_when = temp_event.notify_when + datetime.timedelta(days=x) print 'datetime.timedelta' + str(datetime.timedelta(days=x)) elif repeat_type == 'Monthly': temp_event.start_time = temp_event.start_time + relativedelta( months = +x ) temp_event.notify_when = temp_event.notify_when + relativedelta( months = +x ) elif repeat_type == 'Weekly': temp_event.start_time = temp_event.start_time + datetime.timedelta(days=7) * x temp_event.notify_when = temp_event.notify_when + datetime.timedelta(days=7) * x if temp_event.start_time.weekday() == 0: temp_event.day_of_week = 'Monday' elif temp_event.start_time.weekday() == 1: temp_event.day_of_week = 'Tuesday' elif temp_event.start_time.weekday() == 2: temp_event.day_of_week = 'Wednesday' elif temp_event.start_time.weekday() == 3: temp_event.day_of_week = 'Thursday' elif temp_event.start_time.weekday() == 4: temp_event.day_of_week = 'Friday' elif temp_event.start_time.weekday() == 5: temp_event.day_of_week = 'Saturday' elif temp_event.start_time.weekday() == 6: temp_event.day_of_week = 'Sunday' print 'Temp_Event Date: ' + str(temp_event.start_time) print 'Temp Event Day of Week: ' + str(temp_event.day_of_week) temp_event.show_date = str(temp_event.start_time)[5:7] + "/" + str(temp_event.start_time)[8:10] + "/" + str(temp_event.start_time)[0:4] temp_event.show_begin_time = str(temp_event.begin_time)[11:16] temp_event.show_end_time = str(temp_event.begin_time)[11:16] temp_event.is_date_set = True temp_event.is_week_set = True if temp_event.day_of_week == 'Sunday': temp_event.week_num = temp_event.start_time.isocalendar()[1] + 1 else: temp_event.week_num = temp_event.start_time.isocalendar()[1] print 'in repeat py day of week: ' + str(temp_event.week_num) temp_event.save()
def repeat_events(event): start_time = event.start_time end_repeat_date = event.end_repeat repeat_type = event.repeat num_repeat = find_repeat(start_time, end_repeat_date, repeat_type) for x in range(1, num_repeat + 1): temp_event = Post() temp_event.author = event.author temp_event.content = event.content temp_event.notification = event.notification temp_event.notify_when = event.notify_when temp_event.location_event = event.location_event temp_event.description_event = event.description_event temp_event.begin_time = event.begin_time temp_event.end_time = event.end_time temp_event.end_repeat = event.end_repeat temp_event.need_repeat = False temp_event.start_time = event.start_time temp_event.not_all_day = event.not_all_day temp_event.repeat = event.repeat if repeat_type == 'Daily': temp_event.start_time = temp_event.start_time + datetime.timedelta( days=x) temp_event.notify_when = temp_event.notify_when + datetime.timedelta( days=x) print 'datetime.timedelta' + str(datetime.timedelta(days=x)) elif repeat_type == 'Monthly': temp_event.start_time = temp_event.start_time + relativedelta( months=+x) temp_event.notify_when = temp_event.notify_when + relativedelta( months=+x) elif repeat_type == 'Weekly': temp_event.start_time = temp_event.start_time + datetime.timedelta( days=7) * x temp_event.notify_when = temp_event.notify_when + datetime.timedelta( days=7) * x if temp_event.start_time.weekday() == 0: temp_event.day_of_week = 'Monday' elif temp_event.start_time.weekday() == 1: temp_event.day_of_week = 'Tuesday' elif temp_event.start_time.weekday() == 2: temp_event.day_of_week = 'Wednesday' elif temp_event.start_time.weekday() == 3: temp_event.day_of_week = 'Thursday' elif temp_event.start_time.weekday() == 4: temp_event.day_of_week = 'Friday' elif temp_event.start_time.weekday() == 5: temp_event.day_of_week = 'Saturday' elif temp_event.start_time.weekday() == 6: temp_event.day_of_week = 'Sunday' print 'Temp_Event Date: ' + str(temp_event.start_time) print 'Temp Event Day of Week: ' + str(temp_event.day_of_week) temp_event.show_date = str(temp_event.start_time)[5:7] + "/" + str( temp_event.start_time)[8:10] + "/" + str( temp_event.start_time)[0:4] temp_event.show_begin_time = str(temp_event.begin_time)[11:16] temp_event.show_end_time = str(temp_event.begin_time)[11:16] temp_event.is_date_set = True temp_event.is_week_set = True if temp_event.day_of_week == 'Sunday': temp_event.week_num = temp_event.start_time.isocalendar()[1] + 1 else: temp_event.week_num = temp_event.start_time.isocalendar()[1] print 'in repeat py day of week: ' + str(temp_event.week_num) temp_event.save()