def create_post(request): try: image = request.POST['post-image'] except MultiValueDictKeyError: image = request.FILES['post-image'] except MultiValueDictKeyError: image = None if not request.POST['post_title']: return HttpResponseRedirect('/newsfeed') elif not request.POST['post_text'] and not image: return HttpResponseRedirect('/newsfeed') else: new_post = Post(post_text=request.POST['post_text'], post_title=request.POST['post_title'], timestamp=timezone.now(), owner=request.user) if image: new_post.image_path = request.FILES['post-image'] new_post.save() tag_string = request.POST['tags'] if tag_string: tag_list = [x.strip() for x in tag_string.split(',')] # tag_list = tag_string.split(',') for x in tag_list: new_tag = Tag(tag=x, post_id=new_post) new_tag.save() old_pref = Preferences.objects.filter(owner=request.user).filter(tag=x) if old_pref: old_pref[0].num = old_pref[0].num + 1 old_pref[0].save() else: new_pref = Preferences(owner=request.user, tag=x, num=1) new_pref.save() return HttpResponseRedirect('/newsfeed')
def post_tag(request): if request.method == 'POST': new_post = Post.objects.order_by('-timestamp')[0] try: tag_string = request.POST['tags'] except MultiValueDictKeyError: tag_string = None if tag_string: tag_list = [x.strip() for x in tag_string.split(',')] # tag_list = tag_string.split(',') for x in tag_list: new_tag = Tag(tag=x, post_id=new_post) new_tag.save() old_pref = Preferences.objects.filter(owner=request.user).get(tag=x) if old_pref: old_pref.num = old_pref.num + 1 old_pref.save() else: new_pref = Preferences(owner=request.user, tag=x, num=1) new_pref.save() return HttpResponseRedirect('/newsfeed')