def edit_rezome(request): person = Person.objects.get(username = request.user.username) if request.method == 'POST': form = RezomeForm(request.POST) if form.is_valid(): rezome = form.cleaned_data['rezome'] rezome = rezome.replace('<', '<').replace('>', '>').replace('\n', '<br/>') # Clean the rezome and recognize line breaks person.rezome = rezome person.save() return HttpResponseRedirect('/user/' + person.username + '/rezome/') else: form = RezomeForm(initial = {'rezome': person.rezome.replace('<br/>', '\n').replace('<', '<').replace('>', '>')}) context = { 'form': form, } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'edit_rezome.html', context)
def profile_detail(request, username): person = get_object_or_404(Person, username = username) # Get Person programming_availability = False if len(person.programming.all()) > 0: programming_availability = True posts = Post.objects.filter(author = person) # Get Persons posts count_posts = [post.title for post in posts] post_availability = False if len(count_posts) > 0: post_availability = True context = { 'person': person, 'posts': posts, 'post_availability': post_availability, 'programming_availability': programming_availability, 'current_url': str(request.path).replace('/', '%2F'), } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'profile_detail.html', context)
def all_persons(request, page): if request.user.is_authenticated: try: user = Person.objects.get(username = request.user.username) except: user = Person(username = request.user.username, name = request.user.first_name) user.save() else: user = None persons = Person.objects.all() # Get all persons paginate = Paginator(persons, 3) context = { 'persons': paginate.page(page), 'current_url': str('/user/all/%3Fpage=1/').replace('/', '%2F'), } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'all_persons.html', context)
def delete_post_comment(request, username, post_id, comment_id): comment = get_object_or_404(PostComment, id=comment_id) # Get the Comment post = get_object_or_404(Post, id=post_id) # Get the Post # If registered user is comment author if comment.author.username == request.user.username: comment.delete() # Delete it post.comments = int(post.comments) - 1 post.save() return HttpResponseRedirect('/user/' + username + '/post/' + str(post.id)) # Or if registered user is comments post author elif post.author.username == request.user.username: comment.delete() # Delete it post.comments = int(post.comments) - 1 post.save() return HttpResponseRedirect('/user/' + username + '/post/' + str(post.id)) # If registered user not post author else: mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'forbidden.html')
def cloud_index(request): person = Person.objects.get(username=request.user.username) # If user has cloud try: # Load the cloud cloud = Cloud.objects.get(owner=person) # If there is no cloud for user except: # Creat a new cloud for user cloud = Cloud(owner=person) cloud.save() # Get user's cloud uploaded files files = File.objects.filter(cloud=cloud).order_by('-id') context = { 'person': person, 'cloud': cloud, 'files': files, } # Add authenticated user and it's new notifications to context mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) # Show "cloud index" page template to user return render(request, 'cloud/cloud_index.html', context)
def edit_post(request, post_id): post = Post.objects.get(id=post_id) # Get the Post # If registered user is post author if post.author.username == request.user.username: # If form method == POST if request.method == 'POST': form = PostForm(request.POST) # Get form if form.is_valid(): title = form.cleaned_data['title'] # Read title body = form.cleaned_data['body'] # Read body cover = form.cleaned_data['cover'] # Read cover short_description = form.cleaned_data[ 'short_description'] # Read short description category = form.cleaned_data['category'] # Read category body = mskf.translate_to_html(body) # Give new data to post post.title = title post.body = body post.cover = cover post.short_description = short_description post.category = category post.save() # Save it return HttpResponseRedirect('/user/' + post.author.username + '/post/' + str(post.id)) # If form method == GET else: # Give form to user form = PostForm( initial={ 'title': post.title, 'body': mskf.translate_to_raw(post.body), 'cover': post.cover, 'short_description': post.short_description, 'category': post.category, }) context = { 'form': form, 'post': post, } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'edit_post.html', context) # If registered user not post author else: mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'forbidden.html')
def wellcome(request): context = {} # Add authenticated user and it's new notifications to context mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) # Show "wellcome" page template to user return render(request, 'wellcome.html', context)
def rezome_detail(request, username): person = Person.objects.get(username = username) context = { 'person': person, } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'rezome_detail.html', context)
def all_notifications(request, page): person = Person.objects.get(username = request.user.username) notifications = Notification.objects.filter(givver = person).order_by('-id') paginate = Paginator(notifications, 3) context = { 'notifications': paginate.page(page), } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'all_notifications.html', context)
def friends(request, page): person = Person.objects.get(username = request.user.username) paginate = Paginator(person.following.all(), 3) context = { 'persons': paginate.page(page), 'current_url': str('/user/friends/%3Fpage=1/').replace('/', '%2F'), } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'followed_persons.html', context)
def new_notifications(request): person = Person.objects.get(username = request.user.username) notifications = Notification.objects.filter(givver = person, done = False).order_by('-id') context = { 'notifications': notifications, } for notification in notifications: notification.done = True notification.save() mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'new_notifications.html', context)
def delete_post(request, username, post_id): person = Person.objects.get(username=username) post = get_object_or_404(Post, author=person, id=post_id) # Get the Post # If registered user is post author if post.author.username == request.user.username: post.delete() # Delete it return HttpResponseRedirect('/' + 'user/' + username) # If registered user not post author else: context = {} mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'forbidden.html', context)
def followings(request, username, page): person = Person.objects.get(username = username) following_available = False if len(person.following.all()) > 0: following_available = True paginate = Paginator(person.following.all(), 3) context = { 'persons': paginate.page(page), 'person': person, 'current_url': str('/user/friends/%3Fpage=1/').replace('/', '%2F'), 'following_available': following_available, } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'followings.html', context)
def new_post(request): person = Person.objects.get(username=request.user.username) # If form method == POST if request.method == 'POST': form = PostForm(request.POST) # Get form if form.is_valid(): title = form.cleaned_data['title'] # Read title body = form.cleaned_data['body'] # Read body cover = form.cleaned_data['cover'] # Read cover short_description = form.cleaned_data[ 'short_description'] # Read short description category = form.cleaned_data['category'] # Read category body = mskf.translate_to_html(body) # Create a Post model with form data post = Post(title = title,\ body = body,\ author = person,\ cover = cover,\ short_description = short_description,\ category = category) post.save() # Save it return HttpResponseRedirect('/user/' + post.author.username + '/post/' + str(post.id)) # If form method == GET else: form = PostForm() # Give form to user context = { 'form': form, } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'new_post.html', context)
def friends_posts(request, page): person = Person.objects.get(username = request.user.username) posts = [] all_posts = Post.objects.all().order_by('-publish_time') for post in all_posts: if post.author in person.following.all(): post.body = post.body.replace('<br/>', ' ') posts.append(post) paginate = Paginator(posts, 3) context = { 'person': person, 'posts': paginate.page(page), } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'friends_posts.html', context)
def posts(request, page): # Try to load authenticated user try: person = get_object_or_404(Person, username=request.user.username) except: person = None # Load all posts order by there publish time posts = Post.objects.all().order_by('-publish_time') paginate = Paginator(posts, 3) # Paginate by 3 # Load banner try: ad = Ad.objects.filter(type='صفحه اول') ad = choice(ad) while ad.available_views == '0': ad.delete() ad = choice(Ad.objects.filter(type='صفحه اول')) ad.available_views = int(ad.available_views) - 1 ad.save() except: ad = None context = { 'person': person, 'posts': paginate.page(page), 'ad': ad, } # Add authenticated user and it's new notifications to context mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) # Show "index" page template to user return render(request, 'index.html', context)
def all_posts(request, username, page): person = Person.objects.get(username=username) # Get the Person posts = Post.objects.filter(author=person).order_by( '-publish_time') # Get the Posts try: user = Person.objects.get(username=request.user.username) except: user = None paginate = Paginator(posts, 3) context = { 'posts': paginate.page(page), 'person': person, } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'all_posts.html', context)
def signup(request): # If form method == POST if request.method == 'POST': form = SignUpForm(request.POST) # Get Form # If form valid if form.is_valid(): username = form.cleaned_data['username'] # Read username # Create a user with form data user = form.save(commit=False) user.backend = 'django.contrib.auth.backends.ModelBackend' user.save() # Create a Person model for user person = Person(username=username, name=username) person.save() # Login current user login(request, user) # Redirect user to "wellcome" page return HttpResponseRedirect('/wellcome/') # If form method == GET else: form = SignUpForm() # Give form to user context = { 'form': form, } # Add authenticated user and it's new notifications to context mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) # Show "signup" page template to user return render(request, 'registration/signup.html', context)
def upload_file(request): person = Person.objects.get(username=request.user.username) cloud = Cloud.objects.get(owner=person) # If request method == POST if request.method == 'POST': form = FileUploadForm(request.POST, request.FILES) # If form valid if form.is_valid(): image = form.cleaned_data['image'] compress = form.cleaned_data['compress'] file = File(cloud=cloud, file=image) file.save() # If user want to compress the image if compress == True: mskf.compress(file.file.path) # calculate file size filesize = file.file.size / 1000 if filesize < 100: filesize = 0.1 elif filesize < 200: filesize = 0.2 elif filesize < 300: filesize = 0.3 elif filesize < 400: filesize = 0.4 elif filesize < 500: filesize = 0.5 elif filesize < 600: filesize = 0.6 elif filesize < 700: filesize = 0.7 elif filesize < 800: filesize = 0.8 elif filesize < 900: filesize = 0.9 else: filesize = round(filesize / 100) / 10 # Calculate cloud's used space cloud.used_space = float(cloud.used_space) cloud.used_space = round((cloud.used_space + filesize) * 10) / 10 cloud.used_percent = round( float(cloud.used_space) * 1000 / float(cloud.space)) / 10 cloud.save() # Redirect user to "cloud" page return HttpResponseRedirect('/cloud/') # If request method == GET else: form = FileUploadForm() # Give the form to user # Check cloud's available space available_space = float(cloud.space) - float(cloud.used_space) if available_space >= 10.0: available_space = True context = { 'form': form, 'person': person, 'cloud': cloud, 'available_space': available_space, } # Add authenticated user and it's new notifications to context mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) # Show "upload file" page template to user return render(request, 'cloud/upload_file.html', context)
def post_detail(request, username, post_id): person = Person.objects.get(username=username) # Get the Person post = get_object_or_404(Post, author=person, id=post_id) # Get the Post comments = PostComment.objects.filter(place=post).order_by( 'id') # Get the Comments len_comments = len(comments) # Get length of comments try: user = Person.objects.get(username=request.user.username) except: user = None post.views = int(post.views) + 1 post.save() authenticated_user = None if request.user.is_authenticated: authenticated_user = Person.objects.get(username=request.user.username) authenticated_user.viewed_posts.add(post) authenticated_user.save() # For comment # If form method == POST if request.method == 'POST': print('post') form = CommentForm(request.POST) # Get form if form.is_valid(): print('valid') mode = form.cleaned_data['mode'] # Read mode text = form.cleaned_data['text'] # Read body text = text.replace('<', '<').replace('>', '>').replace( '\n', '<br/>') # Clean the body and recognize line breaks # If mode == comment if mode == 'comment': # Create a Comment model with form data comment = PostComment(place=post, author=user, text=text, replay=None) comment.save() # Save it post.comments = int(post.comments) + 1 post.save() notif = Notification( givver=post.author, message= '<a href="/user/{0}/">{1}</a> نظری روی مطلب «<a href="/user/{2}/post/{3}/">{4}</a>» شما ارسال کرد' .format(user.username, user.name, post.author.username, post.id, post.title, comment.id), notif_type='comment') notif.save() elif mode == 'hello': return HttpResponse(json.dumps({'message': 'hello'})) else: comment = PostComment.objects.get(id=mode) comment.replay = text comment.save() notif = Notification( givver=comment.author, message= '<a href="/user/{0}/">{1}</a> پاسخی به <a href="/user/{2}/post/{3}/#comments">نظر</a> شما داد' .format(person.username, person.name, post.author.username, post.id), notif_type='replay') notif.save() context = { 'post': post, 'comments': comments, 'len_comments': len_comments, 'form': form, 'current_url': str(request.path).replace('/', '%2F'), } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) mskf.add_3_ads_to_context(context) return HttpResponseRedirect('/user/' + post.author.username + '/post/' + str(post.id)) else: print(form.cleaned_data['text']) # If form method == GET else: form = CommentForm() # Give form to user post_body = mskf.get_repo_data(post.body) context = { 'post': post, 'post_body': post_body, 'comments': comments, 'len_comments': len_comments, 'form': form, 'person': user, 'current_url': str(request.path).replace('/', '%2F'), } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) mskf.add_3_ads_to_context(context) return render(request, 'post_detail.html', context)
def edit_profile(request): person = Person.objects.get(username = request.user.username) # Get the Person try: github_login = request.user.social_auth.get(provider = 'github') except UserSocialAuth.DoesNotExist: github_login = None try: gitlab_login = request.user.social_auth.get(provider = 'gitlab') except UserSocialAuth.DoesNotExist: gitlab_login = None # If form method == POST if request.method == 'POST': form = ProfileEditForm(request.POST, request.FILES) # Get Form # If form valid if form.is_valid(): avatar = form.cleaned_data['avatar'] # Read name name = form.cleaned_data['name'] # Read name public_email = form.cleaned_data['public_email'] # Read public_name mobile = form.cleaned_data['mobile'] # Read mobile description = form.cleaned_data['description'] # Read description year_of_born = form.cleaned_data['year_of_born'] # Read description gender = form.cleaned_data['gender'] # Read gender work = form.cleaned_data['work'] programming = form.cleaned_data['programming'] # Read programming github = form.cleaned_data['github'] # Read github gitlab = form.cleaned_data['gitlab'] # Read gitlab stackowerflow = form.cleaned_data['stackowerflow'] # Read stackowerflow linkedin = form.cleaned_data['linkedin'] # Read linkedin dev = form.cleaned_data['dev'] # Read dev website = form.cleaned_data['website'] # Read website PROGRAMMING_CHOICES = { '_C_': 'C', '_C++_': 'C++', '_C#_': 'C#', '_Objective-C_': 'Objective-C', '_Java_': 'Java', '_JavaScript_': 'JavaScript', '_Python_': 'Python', '_PHP_': 'PHP', '_HTML_': 'HTML', '_CSS_': 'CSS', '_Perl_': 'Perl', '_Swift_': 'Swift', '_Kotlin_': 'Kotlin', '_Go_': 'Go', '_Ruby_': 'Ruby', '_Basic_': 'Basic', '_Pascal_': 'Pascal', '_Lua_': 'Lua', '_R_': 'R', '_Rust_': 'Rust', '_TypeScript_': 'TypeScript', } person.programming.clear() try: for p_lang in PROGRAMMING_CHOICES: if p_lang in programming: programming_language = Programming.objects.get(language = PROGRAMMING_CHOICES[p_lang]) person.programming.add(programming_language) except: pass # Give new data to person if avatar != None: if avatar == False: person.avatar = None else: person.avatar = avatar person.name = name person.public_email = public_email person.mobile = mobile person.description = description person.year_of_born = year_of_born person.gender = gender person.work = work person.github = github person.gitlab = gitlab person.stackowerflow = stackowerflow person.linkedin = linkedin person.dev = dev person.website = website person.save() # Save it return HttpResponseRedirect('/user/' + person.username) # If form method == GET else: p_langs = [] for p_lang in person.programming.all(): p_langs.append('_{}_'.format(p_lang)) # Give form to user form = ProfileEditForm(initial = { 'avatar': person.avatar, 'name': person.name, 'public_email': person.public_email, 'mobile': person.mobile, 'description': person.description, 'year_of_born': person.year_of_born, 'gender': person.gender, 'work': person.work, 'programming': p_langs, 'github': person.github, 'gitlab': person.gitlab, 'stackowerflow': person.stackowerflow, 'linkedin': person.linkedin, 'dev': person.dev, 'website': person.website, }) context = { 'form': form, 'github_login': github_login, 'gitlab_login': gitlab_login, 'person': person, } mskf.add_notification_availability_to_context(request, context) mskf.add_authenticated_user_to_context(request, context) return render(request, 'edit_profile.html', context)