def process_group_topic(topic, topic_len_threshold, user_id=None, unique=None, how_long='short', group_id=None):
	"""
	Processes topic setting for OpenGroupCreateForm(), ChangePrivateGroupTopicForm() and ChangeGroupTopicForm() (e.g.validation etc.)

	unique is 'None' if the public mehfil is yet to be created
	"""
	topic_change_frozen = is_topic_change_frozen(group_id) if group_id else False
	if topic_change_frozen:
		raise forms.ValidationError('Sorry! Ye mehfil report ho chuki hai aur investigation puri honay tak topic change nahi kiya ja sakta')
	else:
		ttl = topic_change_rate_limited(user_id,unique)
		if ttl:
			raise forms.ValidationError('Ap topic {} baad change kar sakein ge'.format(human_readable_time(ttl)))
		else:
			len_topic = len(topic)
			if not topic:
				raise forms.ValidationError('Topic rakhna zaruri hai')
			elif len_topic < 4:
				raise forms.ValidationError('Topic itna chota nahi ho sakta')
			elif len_topic > topic_len_threshold:
				raise forms.ValidationError('Topic {0} chars se lamba nahi hota. Ap ne {1} chars likhey'.format(topic_len_threshold,len_topic))
			elif topic.isdigit():
				raise forms.ValidationError('Topic mein sirf numbers nahi ho saktey')
			validate_topic_chars(topic, user_id)
			spot_gibberish_or_repeating_text_in_topic(topic,len_topic, user_id)
			uni_str = uniform_string(topic)
			if uni_str:
				if uni_str.isspace():
					log_topic_changing_attempt(user_id)
					raise forms.ValidationError('Ziyada khali spaces daal di hain')
				else:
					log_topic_changing_attempt(user_id)
					raise forms.ValidationError('"%s" - is terhan bar bar letters repeat nahi karein' % uni_str)
			log_topic_change(user_id,unique,how_long=how_long)
def process_group_rules(rules, rules_len_threshold, user_id=None, unique=None, group_id=None):
	"""
	Processes rules setting for OpenGroupCreateForm() and ChangeGroupRulesForm() (e.g.validation etc.)

	unique is 'None' if the public mehfil is yet to be created
	"""
	rules_change_frozen = is_rules_change_frozen(group_id) if group_id else False
	if rules_change_frozen:
		raise forms.ValidationError('Sorry! Ye mehfil report ho chuki hai aur investigation puri honay tak rules change nahi kiye ja saktey')
	else:
		ttl = rules_change_rate_limited(user_id,unique)
		if ttl:
			raise forms.ValidationError('Ap rules {} baad change kar sakein ge'.format(human_readable_time(ttl)))
		else:
			len_rules = len(rules)
			if not rules:
				raise forms.ValidationError('Rules likhna zaruri hain')
			elif len_rules < 4:
				raise forms.ValidationError('Rules itnay chotay nahi ho saktay')
			elif len_rules > rules_len_threshold:
				raise forms.ValidationError('Rules {0} chars se lambay nahi hota. Ap ne {1} chars likhey'.format(rules_len_threshold,len_rules))
			elif rules.isdigit():
				raise forms.ValidationError('Rules mein sirf numbers nahi ho saktey')
			validate_rules_chars(rules, user_id)
			spot_gibberish_or_repeating_text_in_rules(rules,len_rules, user_id)
			uni_str = uniform_string(rules)
			if uni_str:
				if uni_str.isspace():
					log_rules_changing_attempt(user_id)
					raise forms.ValidationError('Ziyada khali spaces daal di hain')
				else:
					log_rules_changing_attempt(user_id)
					raise forms.ValidationError('"%s" - is terhan bar bar letters repeat nahi karein' % uni_str)
			log_rules_change(user_id,unique)
def process_group_rules(rules, rules_len_threshold, user_id=None, unique=None, group_id=None):
	"""
	Processes rules setting for OpenGroupCreateForm() and ChangeGroupRulesForm() (e.g.validation etc.)

	unique is 'None' if the public mehfil is yet to be created
	"""
	rules_change_frozen = is_rules_change_frozen(group_id) if group_id else False
	if rules_change_frozen:
		raise forms.ValidationError('Sorry! Ye mehfil report ho chuki hai aur investigation puri honay tak rules change nahi kiye ja saktey')
	else:
		ttl = rules_change_rate_limited(user_id,unique)
		if ttl:
			raise forms.ValidationError('Ap rules {} baad change kar sakein ge'.format(human_readable_time(ttl)))
		else:
			len_rules = len(rules)
			if not rules:
				raise forms.ValidationError('Rules likhna zaruri hain')
			elif len_rules < 4:
				raise forms.ValidationError('Rules itnay chotay nahi ho saktay')
			elif len_rules > rules_len_threshold:
				raise forms.ValidationError('Rules {0} chars se lambay nahi hota. Ap ne {1} chars likhey'.format(rules_len_threshold,len_rules))
			elif rules.isdigit():
				raise forms.ValidationError('Rules mein sirf numbers nahi ho saktey')
			validate_rules_chars(rules, user_id)
			spot_gibberish_or_repeating_text_in_rules(rules,len_rules, user_id)
			uni_str = uniform_string(rules)
			if uni_str:
				if uni_str.isspace():
					log_rules_changing_attempt(user_id)
					raise forms.ValidationError('Ziyada khali spaces daal di hain')
				else:
					log_rules_changing_attempt(user_id)
					raise forms.ValidationError('"%s" - is terhan bar bar letters repeat nahi karein' % uni_str)
			log_rules_change(user_id,unique)
def process_group_topic(topic, topic_len_threshold, user_id=None, unique=None, how_long='short', group_id=None):
	"""
	Processes topic setting for OpenGroupCreateForm(), ChangePrivateGroupTopicForm() and ChangeGroupTopicForm() (e.g.validation etc.)

	unique is 'None' if the public mehfil is yet to be created
	"""
	topic_change_frozen = is_topic_change_frozen(group_id) if group_id else False
	if topic_change_frozen:
		raise forms.ValidationError('Sorry! Ye mehfil report ho chuki hai aur investigation puri honay tak topic change nahi kiya ja sakta')
	else:
		ttl = topic_change_rate_limited(user_id,unique)
		if ttl:
			raise forms.ValidationError('Ap topic {} baad change kar sakein ge'.format(human_readable_time(ttl)))
		else:
			len_topic = len(topic)
			if not topic:
				raise forms.ValidationError('Topic rakhna zaruri hai')
			elif len_topic < 4:
				raise forms.ValidationError('Topic itna chota nahi ho sakta')
			elif len_topic > topic_len_threshold:
				raise forms.ValidationError('Topic {0} chars se lamba nahi hota. Ap ne {1} chars likhey'.format(topic_len_threshold,len_topic))
			elif topic.isdigit():
				raise forms.ValidationError('Topic mein sirf numbers nahi ho saktey')
			validate_topic_chars(topic, user_id)
			spot_gibberish_or_repeating_text_in_topic(topic,len_topic, user_id)
			uni_str = uniform_string(topic)
			if uni_str:
				if uni_str.isspace():
					log_topic_changing_attempt(user_id)
					raise forms.ValidationError('Ziyada khali spaces daal di hain')
				else:
					log_topic_changing_attempt(user_id)
					raise forms.ValidationError('"%s" - is terhan bar bar letters repeat nahi karein' % uni_str)
			log_topic_change(user_id,unique,how_long=how_long)
	def clean_price(self):
		"""
		Go through all the disallowed scenarios one-by-one
		"""
		price, own_id, own_uname, time_now, score, group_id, group_owner_id, is_mob_verified = self.cleaned_data["price"], self.user_id, self.user_uname, \
		self.time_now, self.score, self.group_id, self.group_owner_id, self.is_mob_verified
		is_public = False if retrieve_group_privacy(group_id) == '1' else True
		if not is_mob_verified:
			# user's not verified their mobile number
			raise forms.ValidationError("Sorry! Mehfil ki ownership sirf verified users ko mil sakti hai")
		elif group_owner_id == str(own_id):
			# already group owner
			raise forms.ValidationError("Ap already is mehfil ke owner hain")
		else:
			try:
				price = int(price)
			except (ValueError, TypeError):
				raise forms.ValidationError("Sirf number likhein")
			##################################################################
			##################################################################
			# if re-instating score, uncomment the 2 lines below, and comment the 2 lines after that
			# if price > score:
			#     raise forms.ValidationError('Sorry! Ap ne {} points offer kiye, lekin apka score sirf {} points hai'.format(price,score))
			if price > 0:
				raise forms.ValidationError('Sorry! Mehfil ka owner banney ke liye price offer nahi ki ja sakti')
			##################################################################
			##################################################################
			elif is_public and price < PUBLIC_GROUP_MIN_SELLING_PRICE:
				raise forms.ValidationError('Sorry! Offer kam az kam {} points honi chahiye'.format(int(PUBLIC_GROUP_MIN_SELLING_PRICE)))
			elif is_public and price > PUBLIC_GROUP_MAX_SELLING_PRICE:
				raise forms.ValidationError('Sorry! Offer {} points se ziyada nahi honi chahiye'.format(int(PUBLIC_GROUP_MAX_SELLING_PRICE)))
			else:
				try:
					join_date = User.objects.only('date_joined').get(id=own_id).date_joined
				except User.DoesNotExist:
					# this user does not exist thus data incomplete
					raise forms.ValidationError("Sorry! Apki tashkhees nahi ho saki")
				young_age_ttl = (USER_AGE_AFTER_WHICH_PUBLIC_MEHFIL_CAN_BE_CREATED - (time_now - convert_to_epoch(join_date)))
				user_is_freshman = True if young_age_ttl > 0 else False
				if user_is_freshman:
					# user too young to own a public mehfil - old users' "young_age_ttl" statistic would be highly negative
					raise forms.ValidationError("Sorry! Ap ye {} tak nahi kar saktey kiyunke apko Damadam join kiye ziyada time nahi guzra".\
						format(human_readable_time(young_age_ttl)))
				elif time_now - retrieve_group_creation_time(group_id) < GROUP_AGE_AFTER_WHICH_IT_CAN_BE_TRANSFERRED:
					# mehfil too young to be transferred (i.e. less than 10 days old)
					raise forms.ValidationError("Sorry! Is mehfil ko create huay itna time nahi guzra ke ownership transfer ki ja sakey")
				elif is_public and retrieve_group_category(group_id) == '2' and own_uname not in FEMALES:
					raise forms.ValidationError("Sorry! Is mehfil ki ownership sirf pink stars ke liya hai")
				else:
					data = is_group_member_and_rules_signatory(group_id, own_id)
					is_mem, is_sig = data[0], data[1]
					if is_mem and not is_sig:
						raise forms.ValidationError("Sorry! Ap ye nahi kar saktey kiyun ke ap ne mehfil ke rules nahi accept kiye huay")
					elif not is_mem:
						raise forms.ValidationError("Sorry! Ap ye nahi kar saktey kiyun ke ap mehfil ke member nahi")
					else:
						user_ttl, ttl_type = group_ownership_transfer_blocked_by_rate_limit(group_id, group_owner_id, own_id)
						if user_ttl:
							if ttl_type == 'owner':
								# cannot proceed since mehfil owner is rate-limited (and can't accept your request)
								raise forms.ValidationError("Sorry! Ye owner {} tak aisi koi request receive nahi kar sakta kiyunke is ne recently mehfil ka lain dain kiya hai".format(human_readable_time(user_ttl)))
							else:
								# cannot proceed since you are rate-limited (and can't send your request)
								raise forms.ValidationError("Sorry! Ap {} tak aisi koi request send nahi kar saktey kiyunke ap ne recently mehfil ka lain dain kiya hai".format(human_readable_time(user_ttl)))        
						else:
							ttl = ownership_request_rate_limit(group_id, own_id)
							if ttl:
								# the user already sent a transfer request, and must wait for a week before doing so again
								raise forms.ValidationError("Sorry! Ap is mehfil owner ko dubara ownership transfer ki request {} tak send nahi kar saktey".format(human_readable_time(ttl)))
							else:
								return price
	def clean(self):
		data = self.cleaned_data
		text, user_id, section_id, section, image, secret_key_from_form, writer_id = data.get("text"), self.user_id, data.get("gp"), 'pub_grp', \
		data.get('image'), data.get('sk'), data.get('wid')
		secret_key_from_session = get_and_delete_text_input_key(self.user_id,section_id,'pub_grp')
		text = text.strip() if text else text # make function sophisticated https://stackoverflow.com/questions/1546226/simple-way-to-remove-multiple-spaces-in-a-string
		if secret_key_from_form != secret_key_from_session:
			raise forms.ValidationError('Page expire ho gaya hai, dubara post karein')
		elif text:
			if repetition_found(section=section,section_id=section_id,user_id=user_id, target_text=text):
				raise forms.ValidationError('Milti julti baatien nahi post karein')
			else:
				rate_limited, reason = is_limited(user_id,section='pub_grp',with_reason=True)
				if rate_limited > 0:
					raise forms.ValidationError('Ap public mehfils mein likhne se {0} tak banned ho. Reason: {1}'.format(human_readable_time(rate_limited),reason))
				elif not self.is_mob_verified:
					raise forms.ValidationError('Yahan post karney ke liye apna mobile number verify karwain')
				else:
					text_len = len(text)
					if text_len < 6:
						if many_short_messages(user_id,section,section_id):
							raise forms.ValidationError('Har thori deir baad yahan choti baat nahi likhein')
						else:
							log_short_message(user_id,section,section_id)
					elif text_len > PUBLIC_GROUP_REPLY_LENGTH:
						raise forms.ValidationError('tip: itni barri baat nahi likh sakte')
					data["text"] = text
					return data
		else:
			if image:
				rate_limited, reason = is_limited(user_id,section='pub_grp',with_reason=True)
				if rate_limited > 0:
					raise forms.ValidationError('Ap public mehfils mein likhne se {0} tak banned ho. Reason: {1}'.format(human_readable_time(rate_limited),reason))
				elif not self.is_mob_verified:
					raise forms.ValidationError('Yahan post karney ke liye apna mobile number verify karwain')
				else:
					data["text"] = random.choice(["... ... ...",".. .. ..","... .. ...",".. ... ..","... ... ..",".. ... ...",". ... .",\
						". . . . .",".. .. .. ..",".... . ....","... .... ..."]) # for aesthetic reasons
					return data
			else:
				if writer_id and writer_id != -1:
					raise forms.ValidationError('Pehlay yahan message likhein, phir us nickname ke agey "@" ka nishan dabain jisko yeh message bhejna hai')
				else:
					raise forms.ValidationError('tip: likhna zaruri hai')
Example #7
0
	def clean_direct_response(self):
		"""
		Ensure the direct reply is valid

		Obj types include:
		'3' - text post
		'4' - img post
		'5' - public mef
		'6' - private mef
		'7' - 1on1
		"""
		direct_response, obj_type, parent_obj_id, sender_id, receiver_id = self.cleaned_data["direct_response"].strip(), self.obj_type, \
		self.parent_obj_id, self.sender_id, self.receiver_id

		# NOTE: receiver_id is own_id, sender_id is target_id (for the purposes of this function)
		
		##########################################################
		if parent_obj_id and sender_id and receiver_id and obj_type in ('3','4','5','6','7'):
			direct_response = strip_zero_width_characters(direct_response)
			if not direct_response or direct_response.isspace():
				raise forms.ValidationError('Reply likhna zaruri hai')
			len_reply = len(direct_response)
			if len_reply > MAX_HOME_REPLY_SIZE:
				raise forms.ValidationError('Reply {} chars se lamba nahi likhein, ap ne {} chars likhe'.format(MAX_HOME_REPLY_SIZE, len_reply))
			
			##########################################################
			# only applicable to replies under posts
			if obj_type in ('3','4'):

				is_over_speeding, rate_limited_for = determine_direct_response_rate(reply_len=len_reply, replier_id=receiver_id, \
					time_now=self.time_now)

				# this person is already rate limited - show them an error
				if rate_limited_for:
					raise forms.ValidationError('Andha dhund nahi likhein! Ap dubara reply kar sakein ge {}'.\
						format(future_time.future_time(rate_limited_for)))		
				
				# check whether this person ought to be rate limited
				elif is_over_speeding:
					rate_limited, time_length = impose_reply_rate_limit(replier_id=receiver_id)
					
					# this user has been rate-limited due to frequent over-speeding
					if rate_limited:
						raise forms.ValidationError('Andha dhund nahi likhein! Ap dubara reply kar sakein ge {}'.\
							format(future_time.future_time(time_length)))

			##########################################################
			# only applicable to mehfils
			elif obj_type in ('5','6'):
			
				section = 'pub_grp' if obj_type == '5' else 'prv_grp'
 				if repetition_found(section=section,section_id=parent_obj_id,user_id=receiver_id, target_text=direct_response):
					raise forms.ValidationError('Milti julti baatien nahi likhein')
				else:
					rate_limited, reason = is_limited(receiver_id,section=section,with_reason=True)
					if rate_limited > 0:
						raise forms.ValidationError('Ap mehfil mein likhne se {0} tak banned ho. Reason: {1}'.\
							format(human_readable_time(rate_limited),reason))
			
			##########################################################
			exists, hide_status = direct_response_exists(obj_type=obj_type, parent_obj_id=parent_obj_id, sender_id=sender_id, \
				receiver_id=receiver_id, with_hide_status=True)
			if exists:
				return direct_response, hide_status
			else:
				return direct_response, False
		else:
			raise forms.ValidationError('Phir se koshish karein')
	def clean_price(self):
		"""
		Go through all the disallowed scenarios one-by-one
		"""
		price, own_id, own_uname, time_now, score, group_id, group_owner_id, is_mob_verified = self.cleaned_data["price"], self.user_id, self.user_uname, \
		self.time_now, self.score, self.group_id, self.group_owner_id, self.is_mob_verified
		is_public = False if retrieve_group_privacy(group_id) == '1' else True
		if not is_mob_verified:
			# user's not verified their mobile number
			raise forms.ValidationError("Sorry! Mehfil ki ownership sirf verified users ko mil sakti hai")
		elif group_owner_id == str(own_id):
			# already group owner
			raise forms.ValidationError("Ap already is mehfil ke owner hain")
		else:
			try:
				price = int(price)
			except (ValueError, TypeError):
				raise forms.ValidationError("Sirf number likhein")
			##################################################################
			##################################################################
			# if re-instating score, uncomment the 2 lines below, and comment the 2 lines after that
			# if price > score:
			#     raise forms.ValidationError('Sorry! Ap ne {} points offer kiye, lekin apka score sirf {} points hai'.format(price,score))
			if price > 0:
				raise forms.ValidationError('Sorry! Mehfil ka owner banney ke liye price offer nahi ki ja sakti')
			##################################################################
			##################################################################
			elif is_public and price < PUBLIC_GROUP_MIN_SELLING_PRICE:
				raise forms.ValidationError('Sorry! Offer kam az kam {} points honi chahiye'.format(int(PUBLIC_GROUP_MIN_SELLING_PRICE)))
			elif is_public and price > PUBLIC_GROUP_MAX_SELLING_PRICE:
				raise forms.ValidationError('Sorry! Offer {} points se ziyada nahi honi chahiye'.format(int(PUBLIC_GROUP_MAX_SELLING_PRICE)))
			else:
				try:
					join_date = User.objects.only('date_joined').get(id=own_id).date_joined
				except User.DoesNotExist:
					# this user does not exist thus data incomplete
					raise forms.ValidationError("Sorry! Apki tashkhees nahi ho saki")
				young_age_ttl = (USER_AGE_AFTER_WHICH_PUBLIC_MEHFIL_CAN_BE_CREATED - (time_now - convert_to_epoch(join_date)))
				user_is_freshman = True if young_age_ttl > 0 else False
				if user_is_freshman:
					# user too young to own a public mehfil - old users' "young_age_ttl" statistic would be highly negative
					raise forms.ValidationError("Sorry! Ap ye {} tak nahi kar saktey kiyunke apko Damadam join kiye ziyada time nahi guzra".\
						format(human_readable_time(young_age_ttl)))
				elif time_now - retrieve_group_creation_time(group_id) < GROUP_AGE_AFTER_WHICH_IT_CAN_BE_TRANSFERRED:
					# mehfil too young to be transferred (i.e. less than 10 days old)
					raise forms.ValidationError("Sorry! Is mehfil ko create huay itna time nahi guzra ke ownership transfer ki ja sakey")
				elif is_public and retrieve_group_category(group_id) == '2' and own_uname not in FEMALES:
					raise forms.ValidationError("Sorry! Is mehfil ki ownership sirf pink stars ke liya hai")
				else:
					data = is_group_member_and_rules_signatory(group_id, own_id)
					is_mem, is_sig = data[0], data[1]
					if is_mem and not is_sig:
						raise forms.ValidationError("Sorry! Ap ye nahi kar saktey kiyun ke ap ne mehfil ke rules nahi accept kiye huay")
					elif not is_mem:
						raise forms.ValidationError("Sorry! Ap ye nahi kar saktey kiyun ke ap mehfil ke member nahi")
					else:
						user_ttl, ttl_type = group_ownership_transfer_blocked_by_rate_limit(group_id, group_owner_id, own_id)
						if user_ttl:
							if ttl_type == 'owner':
								# cannot proceed since mehfil owner is rate-limited (and can't accept your request)
								raise forms.ValidationError("Sorry! Ye owner {} tak aisi koi request receive nahi kar sakta kiyunke is ne recently mehfil ka lain dain kiya hai".format(human_readable_time(user_ttl)))
							else:
								# cannot proceed since you are rate-limited (and can't send your request)
								raise forms.ValidationError("Sorry! Ap {} tak aisi koi request send nahi kar saktey kiyunke ap ne recently mehfil ka lain dain kiya hai".format(human_readable_time(user_ttl)))        
						else:
							ttl = ownership_request_rate_limit(group_id, own_id)
							if ttl:
								# the user already sent a transfer request, and must wait for a week before doing so again
								raise forms.ValidationError("Sorry! Ap is mehfil owner ko dubara ownership transfer ki request {} tak send nahi kar saktey".format(human_readable_time(ttl)))
							else:
								return price
	def clean(self):
		data = self.cleaned_data
		text, user_id, section_id, section, image, secret_key_from_form = data.get("text"), self.user_id, data.get("gp"), 'pub_grp', \
		data.get('image'), data.get('sk')#, data.get('wid')
		secret_key_from_session = get_and_delete_text_input_key(self.user_id,section_id,'pub_grp')
		text = text.strip() if text else text # make function sophisticated https://stackoverflow.com/questions/1546226/simple-way-to-remove-multiple-spaces-in-a-string
		if secret_key_from_form != secret_key_from_session:
			raise forms.ValidationError('Page expire ho gaya hai, dubara post karein')
		elif text:
			if repetition_found(section=section,section_id=section_id,user_id=user_id, target_text=text):
				raise forms.ValidationError('Milti julti baatien nahi post karein')
			else:
				rate_limited, reason = is_limited(user_id,section='pub_grp',with_reason=True)
				if rate_limited > 0:
					raise forms.ValidationError('Ap public mehfils mein likhne se {0} tak banned ho. Reason: {1}'.format(human_readable_time(rate_limited),reason))
				elif not self.is_mob_verified:
					raise forms.ValidationError('Yahan post karney ke liye apna mobile number verify karwain')
				else:
					text_len = len(text)
					if text_len < 6:
						if many_short_messages(user_id,section,section_id):
							raise forms.ValidationError('Har thori deir baad yahan choti baat nahi likhein')
						else:
							log_short_message(user_id,section,section_id)
					elif text_len > PUBLIC_GROUP_REPLY_LENGTH:
						raise forms.ValidationError('tip: itni barri baat nahi likh sakte')
					data["text"] = text
					return data
		else:
			if image:
				rate_limited, reason = is_limited(user_id,section='pub_grp',with_reason=True)
				if rate_limited > 0:
					raise forms.ValidationError('Ap public mehfils mein likhne se {0} tak banned ho. Reason: {1}'.format(human_readable_time(rate_limited),reason))
				elif not self.is_mob_verified:
					raise forms.ValidationError('Yahan post karney ke liye apna mobile number verify karwain')
				else:
					data["text"] = random.choice(["... ... ...",".. .. ..","... .. ...",".. ... ..","... ... ..",".. ... ...",". ... .",\
						". . . . .",".. .. .. ..",".... . ....","... .... ..."]) # for aesthetic reasons
					return data
			else:
				raise forms.ValidationError('Likhna zaruri hai')