def get(self, request, uid, type): user = get_user(uid) if user: client = user.cid else: return HttpResponse("There is no such user!") user_info = get_insta_info(user.username) if not (client and user): return HttpResponse("دسترسی مجاز نیست") # TODO: redirect to error page if type == 1: text = 'فالو اکانت {} بلاک شده است.'.format(user.username) elif type == 2: text = 'لایک اکانت {} بلاک شده است.'.format(user.username) elif type == 3: text = 'کامنت اکانت {} بلاک شده است.'.format(user.username) elif type == 4: text = 'آنفالو اکانت {} بلاک شده است.'.format(user.username) else: return HttpResponse("wrong notification type") # TODO: redirect to error page notification = Notification.objects.filter(text=text, type=type) if notification: notification = notification[0] notification.text = text notification.seen = False notification.time = datetime.datetime.now() notification.pic_url = user_info['profile_pic_url'] else: notification = Notification(client=client, text=text, type=type, pic_url=user_info['profile_pic_url']) notification.save() return HttpResponse('done!')
def post(self, request, cid): client = get_client(cid) if not (client and (request.user == client or request.user.is_staff)): return HttpResponse( "دسترسی مجاز نیست") # TODO: redirect to error page if client.is_staff or request.user.is_staff: users = User.objects.all() else: users = User.objects.filter(cid=client) form = TradeForm(request.POST) if form.is_valid(): from user.views import get_user, show_user_charge from_user = form.cleaned_data['from_user'] from_user = get_user(int(from_user)) to_user = form.cleaned_data['to_user'] to_user = get_user(int(to_user)) days = form.cleaned_data['days'] hours = form.cleaned_data['hours'] minutes = form.cleaned_data['minutes'] errors = [] if from_user == to_user: errors.append('مبدا و مقصد نمی تواند شبیه به هم باشد.') seconds = days * 24 * 3600 + hours * 3600 + minutes * 60 if seconds == 0: errors.append('مدت زمان انتقالی نمیتواند صفر باشد.') if seconds > show_user_charge(from_user): errors.append('شارژ اکانت مبدا کافی نیست.') if len(errors) == 0: from_user.charge -= seconds to_user.charge += seconds from_user.save() to_user.save() return redirect('/client/dashboard/') return redirect_to_dashboard(request, tradeMessage=errors) return redirect_to_dashboard(request, tradeMessage=[form.errors])
def add_charge_payment_done(plan, payment): payment.done = True payment.save() from user.views import get_user payment_items = payment.items.get('list', []) for item in payment_items: user = get_user(item['uid']) user.charge += plan.charge if user.status == -300: user.status = 100 user.save()
def get(self, request, fuid, tuid): fuser = get_user(fuid) tuser = get_insta_info2(tuid) if not fuser: return HttpResponse("دسترسی مجاز نیست") # TODO: redirect to error page text = '{} اکانت {} را فالو کرد.'.format(tuser['username'], fuser.username) client = fuser.cid notification = Notification(client=client, text=text, type=0) notification.save() return HttpResponse('done!')
def post(self, request, cid, pid): client = get_client(cid) if not (client and (request.user == client or request.user.is_staff)): return HttpResponse( "دسترسی مجاز نیست") # TODO: redirect to error page plan = get_plan(pid) if not plan: return HttpResponse( "selected plan does not exist") # TODO: redirect to error page selected_users = request.POST.getlist('users[]') if len(selected_users) == 0: # if no user is selected if request.user.is_staff: users = User.objects.all() else: users = User.objects.filter(cid=request.user) from user.views import get_insta_info2 for user in users: # insta_info = get_insta_info2(user.uid) # user.insta_info = insta_info if user.status == 0: update_user_charge(user) return render(request, 'plan/selectuser.html', { 'users': users, 'plan': plan, 'client': client }) users = [] from user.views import get_user for selected_user in selected_users: user = get_user(int(selected_user)) if user: users.append(user) total = len(users) * plan.price return render(request, 'plan/prepay.html', { 'users': users, 'plan': plan, 'total': total, 'client': client })
def get(self, request, cid): client = get_client(cid) if not (client and (request.user == client or request.user.is_staff)): return HttpResponse( "دسترسی مجاز نیست") # TODO: redirect to error page if request.user.is_staff: payments = Payment.objects.all() else: payments = Payment.objects.filter(cid=client) from user.views import get_user for payment in payments: payment_items = payment.items.get('list', []) for item in payment_items: user = get_user(item['uid']) item['user'] = user return render(request, 'client/paymentHistory.html', {'payments': payments})
def post(self, request): if not request.user.is_staff: return HttpResponse( "دسترسی مجاز نیست") # TODO: redirect to error page uid = request.POST.get('id', None) charge = request.POST.get('charge', None) errors = [] if not uid: errors.append('لطفا یک یوزر انتخاب کنید.') elif not charge: errors.append('لطفا شارژ موردنظرتان را وارد کنید.') from user.views import get_user user = get_user(uid) if not user: errors.append('یوزر انتخاب شده معتبر نیست.') if len(errors) == 0: user.charge = charge if user.status == -300: user.status = 100 user.save() users = User.objects.all() return render(request, 'client/manageUserCharges.html', { 'instagramUsers': users, 'errors': errors })
def test_user_from_id_absent(self): self.assertRaises(NotFound, lambda: get_user(0))
def post(self, request, cid): client = get_client(cid) if not (client and (request.user == client or request.user.is_staff)): return HttpResponse( "دسترسی مجاز نیست") # TODO: redirect to error page form = PostForm(request.POST) mode = form.data.get('mode', None) caption = form.data.get('caption', '') users = request.POST.getlist('users[]', None) if users: users = list(map(int, users)) post_date = form.data.get('post_date', None) post_h = form.data.get('post_h', None) post_m = form.data.get('post_m', None) if post_date: post_date = post_date.split('/') post_date = jdatetime.datetime(int(post_date[0]), int(post_date[1]), int(post_date[2]), int(post_h), int(post_m)).togregorian() else: post_date = datetime.datetime.now() photo = request.FILES.get('photo', None) video = request.FILES.get('video', None) errors = [] if len(users) == 0: errors.append('باید حداقل یک یوزر را انتخاب کنید.') if not (photo or video): errors.append('هیچ فایلی فرستاده نشده است.') elif photo and video: errors.append('نمی توانید همزمان هم فیلم و هم عکس بفرستید.') elif video: valid_formats = ['avi', 'mp4', '3gp', 'flv', 'wmv', 'mov'] if not video.name.split('.')[-1] in valid_formats: errors.push('فایل انتخاب شده مورد قبول نیست.') if not mode: errors.append('استوری نمیتواند متن داشته باشد.') if mode == 1 and len(caption) > 0: errors.append('استوری نمیتواند متن داشته باشد.') if len(errors) > 0: if client.is_staff or request.user.is_staff: users = User.objects.all() else: users = User.objects.filter(cid=client) from user.views import get_insta_info2 for user in users: insta_info = get_insta_info2(user.uid) user.insta_info = insta_info return render(request, 'post/request.html', { 'form': form, 'users': users, 'errors': errors }) # if form is valid for user in users: from user.views import get_user user_boject = get_user(user) if photo: x = float(form.data.get('x')) y = float(form.data.get('y')) w = float(form.data.get('width')) h = float(form.data.get('height')) import math __device_ratios = [(3, 4), (2, 3), (5, 8), (3, 5), (9, 16), (10, 16), (40, 71)] __aspect_ratios = [1.0 * x[0] / x[1] for x in __device_ratios] if mode == 0: min_ratio, max_ratio = 4.0 / 5.0, 90.0 / 47.0 else: min_ratio, max_ratio = min(__aspect_ratios), max( __aspect_ratios) while 1.0 * math.ceil(w) / math.ceil(h) <= min_ratio: w += 1 while 1.0 * math.ceil(w) / math.ceil(h) >= max_ratio: w -= 1 post = Post(user=user_boject, mode=mode, photo=photo, post_at=post_date, caption=caption) post.save() from PIL import Image try: image = Image.open(post.photo.file) cropped_image = image.crop((x, y, w + x, h + y)) if w < 320: w1 = 320 h1 = w1 * h / w while 1.0 * math.ceil(w) / math.ceil(h) <= min_ratio: w += 1 while 1.0 * math.ceil(w) / math.ceil(h) >= max_ratio: w -= 1 cropped_image = cropped_image.resize((w1, int(h1)), Image.ANTIALIAS) elif w > 1080: w1 = 1080 h1 = w1 * h / w while 1.0 * math.ceil(w) / math.ceil(h) <= min_ratio: w += 1 while 1.0 * math.ceil(w) / math.ceil(h) >= max_ratio: w -= 1 cropped_image = cropped_image.resize((w1, int(h1)), Image.ANTIALIAS) cropped_image.save(post.photo.file.name) except Exception: return redirect('/client/{}/post/'.format(cid)) elif video: # fs = FileSystemStorage() # filename = fs.save(video.name, video) post = Post(user=user_boject, mode=mode, video=video, post_at=post_date, caption=caption) post.save() return redirect('/client/{}/post/list/'.format(cid))